Skip to content

Commit 44cb3ea

Browse files
committed
New and edited solutions.
Added new solutions, edited a couple of old solutions.
1 parent 0097f4b commit 44cb3ea

17 files changed

+187
-70
lines changed

001_two-sum.py

-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ def twoSum(self, nums, target):
1414
differences[target - n] = i
1515
return -1, -1
1616

17-
1817
print(Solution().twoSum([2, 7, 11, 15], 9))

005_longest-palindromic-substring.py

+1-17
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,5 @@ def longestPalindrome(self, s):
1111
substring = s[j:j + i + 1]
1212
if substring == substring[::-1]:
1313
return substring
14-
'''
15-
# attempt 1 : brute force O(n^2)
16-
for i in range(len(s)):
17-
for j in range(i + 1, len(s)):
18-
substring = s[i:j + 1]
19-
if substring == substring[::-1]:
20-
substring_list.append(substring)
21-
try:
22-
return max(substring_list, key=len)
23-
except ValueError:
24-
return s[0]
25-
26-
'''
27-
28-
print(Solution().longestPalindrome("ababad"))
29-
print(Solution().longestPalindrome("mwwfjysbkebpdjyabcfkgprtxpwvhglddhmvaprcvrnuxifcrjpdgnktvmggmguiiquibmtviwjsqwtchkqgxqwljouunurcdtoeygdqmijdympcamawnlzsxucbpqtuwkjfqnzvvvigifyvymfhtppqamlgjozvebygkxawcbwtouaankxsjrteeijpuzbsfsjwxejtfrancoekxgfyangvzjkdskhssdjvkvdskjtiybqgsmpxmghvvicmjxqtxdowkjhmlnfcpbtwvtmjhnzntxyfxyinmqzivxkwigkondghzmbioelmepgfttczskvqfejfiibxjcuyevvpawybcvvxtxycrfbcnpvkzryrqujqaqhoagdmofgdcbhvlwgwmsmhomknbanvntspvvhvccedzzngdywuccxrnzbtchisdwsrfdqpcwknwqvalczznilujdrlevncdsyuhnpmheukottewtkuzhookcsvctsqwwdvfjxifpfsqxpmpwospndozcdbfhselfdltmpujlnhfzjcgnbgprvopxklmlgrlbldzpnkhvhkybpgtzipzotrgzkdrqntnuaqyaplcybqyvidwcfcuxinchretgvfaepmgilbrtxgqoddzyjmmupkjqcypdpfhpkhitfegickfszermqhkwmffdizeoprmnlzbjcwfnqyvmhtdekmfhqwaftlyydirjnojbrieutjhymfpflsfemkqsoewbojwluqdckmzixwxufrdpqnwvwpbavosnvjqxqbosctttxvsbmqpnolfmapywtpfaotzmyjwnd"))
3014

31-
print(Solution().longestPalindrome("esbtzjaaijqkgmtaajpsdfiqtvxsgfvijpxrvxgfumsuprzlyvhclgkhccmcnquukivlpnjlfteljvykbddtrpmxzcrdqinsnlsteonhcegtkoszzonkwjevlasgjlcquzuhdmmkhfniozhuphcfkeobturbuoefhmtgcvhlsezvkpgfebbdbhiuwdcftenihseorykdguoqotqyscwymtjejpdzqepjkadtftzwebxwyuqwyeegwxhroaaymusddwnjkvsvrwwsmolmidoybsotaqufhepinkkxicvzrgbgsarmizugbvtzfxghkhthzpuetufqvigmyhmlsgfaaqmmlblxbqxpluhaawqkdluwfirfngbhdkjjyfsxglsnakskcbsyafqpwmwmoxjwlhjduayqyzmpkmrjhbqyhongfdxmuwaqgjkcpatgbrqdllbzodnrifvhcfvgbixbwywanivsdjnbrgskyifgvksadvgzzzuogzcukskjxbohofdimkmyqypyuexypwnjlrfpbtkqyngvxjcwvngmilgwbpcsseoywetatfjijsbcekaixvqreelnlmdonknmxerjjhvmqiztsgjkijjtcyetuygqgsikxctvpxrqtuhxreidhwcklkkjayvqdzqqapgdqaapefzjfngdvjsiiivnkfimqkkucltgavwlakcfyhnpgmqxgfyjziliyqhugphhjtlllgtlcsibfdktzhcfuallqlonbsgyyvvyarvaxmchtyrtkgekkmhejwvsuumhcfcyncgeqtltfmhtlsfswaqpmwpjwgvksvazhwyrzwhyjjdbphhjcmurdcgtbvpkhbkpirhysrpcrntetacyfvgjivhaxgpqhbjahruuejdmaghoaquhiafjqaionbrjbjksxaezosxqmncejjptcksnoq"))
15+
print(Solution().longestPalindrome("ababad"))

006_zigzag-conversion.py

-18
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,6 @@ def convert(self, s, numRows):
2525
i += 1
2626
else:
2727
return s
28-
'''
29-
col = 0
30-
while row < numRows:
31-
row_text = []
32-
while col < len(s):
33-
row_text.append(s[col])
34-
print(row, col, s[col])
35-
col += 2 * (numRows - 1)
36-
row += 1
37-
col = row
38-
converted = converted + ''.join(row_text)
39-
40-
41-
for i in range(len(s)):
42-
location = i % len(s)
43-
converted.append(s[i])
44-
return ''.join(converted)
45-
'''
4628
return ''.join(converted)
4729

4830

015_3sum.py

+2-5
Large diffs are not rendered by default.

016_3sum-closest.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ def threeSumClosest(self, nums, target):
88
nums.sort()
99
current_sum = nums[0] + nums[1] + nums[2]
1010
for i in range(len(nums)):
11-
# if nums[i] == nums[i - 1] and i > 0:
12-
# continue
1311
left = i + 1
1412
right = len(nums) - 1
1513

@@ -24,6 +22,6 @@ def threeSumClosest(self, nums, target):
2422
print(current_sum)
2523
return current_sum
2624

27-
# print(Solution().threeSumClosest([-1, 2, 1, -4], 1))
25+
print(Solution().threeSumClosest([-1, 2, 1, -4], 1))
2826

2927
print(Solution().threeSumClosest([0, 1, 2], 3))

018_4sum.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def fourSum(self, nums, target):
2222
elif sum < target:
2323
left += 1
2424
else:
25-
# if [nums[i], nums[left], nums[right]] not in results:
2625
results.append([nums[i], nums[j], nums[left], nums[right]])
2726
left += 1
2827
right -= 1
@@ -38,7 +37,7 @@ def fourSum(self, nums, target):
3837
pass
3938
return results
4039

41-
# print(Solution().fourSum([1, 0, -1, 0, -2, 2], 0))
40+
print(Solution().fourSum([1, 0, -1, 0, -2, 2], 0))
4241

4342
print(Solution().fourSum([0, 0, 0, 0], 0))
4443

022_generate-parantheses.py

+3-21
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,6 @@ def paranthesis(open, close, parenstring = ""):
1717
paranthesis(n - 1, n, "(")
1818

1919
return parantheses
20-
'''
21-
if n > 1:
22-
generated = Solution().generateParenthesis(n - 1)
23-
for g in generated:
24-
parantheses.append("(" + g + ")")
25-
parantheses.append("()" + g)
26-
if g + "()" != "()" + g:
27-
parantheses.append(g + "()")
28-
return parantheses
29-
elif n == 1:
30-
return ["()"]
31-
else:
32-
return []
33-
'''
34-
# print(Solution().generateParenthesis(2))
35-
# print(Solution().generateParenthesis(3))
36-
print(Solution().generateParenthesis(4))
37-
38-
# ['()(())()']
39-
40-
# ["(())(())"]
20+
print(Solution().generateParenthesis(2))
21+
print(Solution().generateParenthesis(3))
22+
print(Solution().generateParenthesis(4))

024_swap-nodes-in-pairs.py

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ def swapPairs(self, head):
2828
break
2929

3030
return new_head.next
31-
# a -> b -> c -> d
32-
# b -> a -> d -> c
3331

3432

3533
def CreateList(num_list):

033_search-in-rotated-sorted-array.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def search(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: int
7+
"""
8+
try:
9+
return nums.index(target)
10+
except ValueError:
11+
return -1
12+
13+
14+
print(Solution().search([4, 5, 6, 7, 0, 1, 2], 8))

034_search-for-a-range.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution(object):
2+
def searchRange(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
l = int(len(nums) / 2)
9+
if nums and l == 0 and nums[l] == target:
10+
return [0, 0]
11+
elif l > 0:
12+
if nums[l] < target:
13+
j, k = Solution().searchRange(nums[l:], target)
14+
if j != -1:
15+
j += l
16+
k += l
17+
elif nums[l] > target:
18+
j, k = Solution().searchRange(nums[:l], target)
19+
else:
20+
j1, k1 = Solution().searchRange(nums[:l], target)
21+
j2, k2 = Solution().searchRange(nums[l:], target)
22+
if j1 != -1:
23+
if j2 != -1:
24+
j, k = j1, k2 + l
25+
else:
26+
j, k = j1, k1
27+
elif j2 != -1:
28+
j, k = j2 + l, k2 + l
29+
else:
30+
j, k = -1, -1
31+
return [j, k]
32+
else:
33+
return [-1, -1]
34+
35+
36+
print(Solution().searchRange([5, 7, 7, 8, 8, 10], 8))
37+
38+
print(Solution().searchRange([], 3))

035_search-insert-position.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ def searchInsert(self, nums, target):
1313
break
1414
return pos
1515

16-
1716
print(Solution().insertPosition([1, 2, 3, 4, 5, 6], 5))

036_valid-sudoku.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def isValidSudoku(self, board):
3+
"""
4+
:type board: List[List[str]]
5+
:rtype: bool
6+
"""
7+
rows = []
8+
cols = []
9+
sqrs = []
10+
for i, row in enumerate(board):
11+
for j, char in enumerate(row):
12+
if char != ".":
13+
rows.append((i, char))
14+
cols.append((j, char))
15+
sqrs.append((int(i / 3), int(j / 3), char))
16+
if len(rows) == len(set(rows)) and len(cols) == len(set(cols)) and len(sqrs) == len(set(sqrs)):
17+
return True
18+
else:
19+
return False
20+
21+
22+
print(Solution().isValidSudoku([[".","8","7","6","5","4","3","2","1"],["2",".",".",".",".",".",".",".","."],["3",".",".",".",".",".",".",".","."],["4",".",".",".",".",".",".",".","."],["5",".",".",".",".",".",".",".","."],["6",".",".",".",".",".",".",".","."],["7",".",".",".",".",".",".",".","."],["8",".",".",".",".",".",".",".","."],["9",".",".",".",".",".",".",".","."]]))

046_permutations.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def permute(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[List[int]]
6+
"""
7+
perms = []
8+
if len(nums) > 1:
9+
for n in range(len(nums)):
10+
below = Solution().permute(nums[:n] + nums[n+1:])
11+
if below:
12+
for bel in below:
13+
bel.insert(0, nums[n])
14+
perms.append(bel)
15+
else:
16+
perms.append(nums)
17+
return perms
18+
19+
print(Solution().permute([1, 1, 2]))

047_permutations-ii.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def permuteUnique(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[List[int]]
6+
"""
7+
perms = []
8+
used = []
9+
if len(nums) > 1:
10+
for n, num in enumerate(nums):
11+
if num in used:
12+
pass
13+
else:
14+
used.append(num)
15+
below = Solution().permuteUnique(nums[:n] + nums[n + 1:])
16+
if below:
17+
for bel in below:
18+
bel.insert(0, num)
19+
perms.append(bel)
20+
else:
21+
perms.append(nums)
22+
return perms
23+
24+
print(Solution().permuteUnique([1,1,2]))

050_pow-x-n.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def myPow(self, x, n):
3+
"""
4+
:type x: float
5+
:type n: int
6+
:rtype: float
7+
"""
8+
return x ** n
9+
10+
print(Solution().myPow(2.03, 3))

088_merge-sorted-array.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def merge(self, nums1, m, nums2, n):
3+
"""
4+
:type nums1: List[int]
5+
:type m: int
6+
:type nums2: List[int]
7+
:type n: int
8+
:rtype: void Do not return anything, modify nums1 in-place instead.
9+
"""
10+
i = 0
11+
j = 0
12+
while i < m and j < n:
13+
if nums1[i] > nums2[j]:
14+
nums1.insert(i, nums2[j])
15+
j += 1
16+
else:
17+
i += 1
18+
19+
while j < n:
20+
nums1.append(nums2[j])
21+
j += 1
22+
23+
print(nums1)
24+
25+
Solution().merge([0, 2, 4], 3, [1, 3, 5, 6], 4)

100_same-tree.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class TreeNode(object):
2+
def __init__(self, x):
3+
self.val = x
4+
self.left = None
5+
self.right = None
6+
7+
class Solution(object):
8+
def isSameTree(self, p, q):
9+
"""
10+
:type p: TreeNode
11+
:type q: TreeNode
12+
:rtype: bool
13+
"""
14+
if p and q:
15+
return p.val == q.val and Solution().isSameTree(p.left, q.left) and Solution().isSameTree(p.right, q.right)
16+
else:
17+
return p is q
18+
19+
tree_a = TreeNode(1)
20+
tree_a.left = TreeNode(2)
21+
tree_a.right = TreeNode(2)
22+
23+
tree_b = TreeNode(1)
24+
tree_b.left = TreeNode(2)
25+
tree_b.right = TreeNode(2)
26+
27+
print(Solution().isSameTree(tree_a, tree_b))

0 commit comments

Comments
 (0)