Skip to content

Commit 66ef42b

Browse files
committed
Python solutions to 344 (iterative + recursive) plus massive renaming and reorganization
1 parent 8c2c63b commit 66ef42b

21 files changed

+64
-1
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
# Leetcode Python Solutions
22
[Click here](https://docs.google.com/spreadsheets/d/1EmRVQ2KEknTREwNd4-2qbSzScHMvgzDT0yGGT5u-yA8/edit?usp=sharing) for the google spreadsheet
3-
organizing the questions by category and linked to the solution I wrote for each of them.
3+
organizing the questions by category and linked to the solution I wrote for each of them. This is always up to date with the README (with the power of a python script and GitHub workflows).
4+
5+
| Number | Question | Hints | Python Solution | C++ Solution |
6+
|:------:|:--------:|-------|:---------------:|:------------:|
7+
| 1 |[Two Sum](https://leetcode.com/problems/two-sum/) | [Python Solution](https://github.com/codethecoffee/leetcode-python-solutions/blob/master/1-Two%20Sum.py) | Use a hash table |
8+
| 56 |[Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[Python Solution](https://github.com/codethecoffee/leetcode-python-solutions/blob/master/56-Merge%20Intervals.py)| Use a stack |
9+
| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [Python Solution](https://github.com/codethecoffee/leetcode-python-solutions/blob/master/66-Plus%20One.py)| Try iterating through each digit backwards. What do you do when the digit is a 9? |
10+
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Python Solution](https://leetcode.com/problems/merge-sorted-array/)| Look at one element from each of the arrays, compare them, and make a decision depending on their relation to each other. |
11+
| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/solution/) | [Python Solution](https://github.com/codethecoffee/leetcode-python-solutions/blob/master/122-Best%20Time%20to%20Buy%20and%20Sell%20Stock.py)| What should we do every time there is a valley followed by a peak in price? |
12+
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string) | |
File renamed without changes.
File renamed without changes.
File renamed without changes.
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Link to problem: https://leetcode.com/problems/reverse-string/
2+
3+
# ITERATIVE SOLUTION
4+
# Time complexity: O(n), Space complexity: O(1)
5+
def reverseString(s):
6+
"""
7+
Do not return anything, modify s in-place instead.
8+
"""
9+
str_len = len(s)
10+
11+
if str_len in {0,1}:
12+
return s
13+
14+
# Initialize two "pointers"; one at the first char, one at the last char
15+
l,r = 0, str_len-1
16+
17+
# Swap the characters at the ends of the current substring
18+
# until l and r completely converge in the middle
19+
while l < r:
20+
old_l, old_r = s[l], s[r]
21+
s[l], s[r] = old_r, old_l
22+
l+=1
23+
r-=1
24+
25+
26+
# RECURSIVE SOLUTION
27+
# Time complexity: O(n), Space complexity: O(n) because of the stack space
28+
29+
def reverseHelper(s, l, r):
30+
"""
31+
Recursive helper function to do the work for reversing the string s
32+
"""
33+
# Base case: l and r have converged
34+
if l >= r:
35+
return
36+
37+
# Swap the two characters in place
38+
old_l, old_r = s[l], s[r]
39+
s[l], s[r] = old_r, old_l
40+
41+
# In recursive call, increment l and decrement r
42+
# so that they inch closer to the middle of string
43+
reverseHelper(s, l+1, r-1)
44+
45+
def reverseString(s):
46+
"""
47+
Do not return anything, modify s in-place instead.
48+
"""
49+
str_len = len(s)
50+
l, r = 0, str_len-1
51+
52+
reverseHelper(s, l, r)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

update_spreadsheet.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Python script that is run with every new commit made to Github repo.
2+
# Automatically populates google spreadsheet with contents of README

0 commit comments

Comments
 (0)