Skip to content

Commit 0074abb

Browse files
committed
initial commit
0 parents  commit 0074abb

File tree

118 files changed

+1586
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+1586
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
download.py

3Sum Closest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def threeSumClosest(self, A, target):
3+
A, result, closest_diff, i = sorted(A), 2147483647, 2147483647, 0
4+
while i < len(A) - 2:
5+
j, k = i + 1, len(A) - 1
6+
while j < k:
7+
diff = A[i] + A[j] + A[k] - target
8+
if diff < 0:
9+
if math.fabs(diff) < math.fabs(closest_diff):
10+
result, closest_diff = A[i] + A[j] + A[k], diff
11+
j += 1
12+
elif diff > 0:
13+
if math.fabs(diff) < math.fabs(closest_diff):
14+
result, closest_diff = A[i] + A[j] + A[k], diff
15+
k -= 1
16+
else:
17+
return target
18+
i += 1
19+
return result

3Sum.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def threeSum(self, A):
3+
A, result, i = sorted(A), [], 0
4+
while i < len(A) - 2:
5+
j, k = i + 1, len(A) - 1
6+
while j < k:
7+
if A[i] + A[j] + A[k] < 0:
8+
j += 1
9+
elif A[i] + A[j] + A[k] > 0:
10+
k -= 1
11+
else:
12+
result.append([A[i], A[j], A[k]])
13+
j, k = j + 1, k - 1
14+
while j < k and A[j] == A[j - 1]:
15+
j += 1
16+
while j < k and A[k] == A[k + 1]:
17+
k -= 1
18+
i += 1
19+
while i < len(A) - 2 and A[i] == A[i - 1]:
20+
i += 1
21+
return result

Add Binary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def addBinary(self, a, b):
3+
return bin(int(a, 2) + int(b, 2)).split('b')[1]

Add Two Numbers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def addTwoNumbers(self, l1, l2):
3+
dummy = ListNode(0)
4+
current, carry = dummy, 0
5+
while l1 != None or l2 != None:
6+
res = carry
7+
if l1 != None:
8+
res += l1.val
9+
l1 = l1.next
10+
if l2 != None:
11+
res += l2.val
12+
l2 = l2.next
13+
carry, res = res / 10, res % 10
14+
current.next = ListNode(res)
15+
current = current.next
16+
if carry == 1:
17+
current.next = ListNode(1)
18+
return dummy.next

Anagrams.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def anagrams(self, strs):
3+
map, result = {}, []
4+
if len(strs) >= 1:
5+
for str in strs:
6+
sorted_str = "".join(sorted(str))
7+
if sorted_str not in map:
8+
map[sorted_str] = [str]
9+
else:
10+
map[sorted_str].append(str)
11+
for word in map:
12+
if len(map[word]) > 1:
13+
result += map[word]
14+
return result

Balanced Binary Tree.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isBalanced(self, root):
3+
return self.getHeight(root) != -1
4+
5+
def getHeight(self, root):
6+
if root == None:
7+
return 0
8+
left_height, right_height = self.getHeight(root.left), self.getHeight(root.right)
9+
if left_height < 0 or right_height < 0 or math.fabs(left_height - right_height) > 1:
10+
return -1
11+
return max(left_height, right_height) + 1

Best Time to Buy and Sell Stock II.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def maxProfit(self, prices):
3+
profit = 0
4+
for i in range(len(prices) - 1):
5+
if (prices[i] < prices[i+1]):
6+
profit += prices[i+1] - prices[i]
7+
return profit
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxProfit(self, prices):
3+
min_price, max_profit = 9223372036854775807, 0
4+
profits_before = [0 for i in range(len(prices))]
5+
for i in range(len(prices)):
6+
min_price = min(prices[i], min_price)
7+
max_profit = max(prices[i] - min_price, max_profit)
8+
profits_before[i] = max_profit
9+
max_price, max_profit = -9223372036854775808, 0
10+
profits_after = [0 for i in range(len(prices))]
11+
for i in reversed(range(len(prices))):
12+
max_price = max(prices[i], max_price)
13+
max_profit = max(max_price - prices[i], max_profit)
14+
profits_after[i] = max_profit
15+
return reduce(lambda acc, i: max(profits_before[i] + profits_after[i], acc), range(len(prices)), 0)

Best Time to Buy and Sell Stock.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maxProfit(self, prices):
3+
min_price = 9223372036854775807
4+
max_profit = 0
5+
for i in range(len(prices)):
6+
min_price = min(prices[i], min_price)
7+
max_profit = max(prices[i] - min_price, max_profit)
8+
return max_profit

0 commit comments

Comments
 (0)