Skip to content

Commit 0097f4b

Browse files
committed
Adding files
Adding solutions to problems already completed.
1 parent 1871991 commit 0097f4b

28 files changed

+871
-1
lines changed

001_two-sum.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
3+
def twoSum(self, nums, target):
4+
"""
5+
:type nums: List[int]
6+
:type target: int
7+
:rtype: List[int]
8+
"""
9+
differences = {}
10+
for i, n in enumerate(nums):
11+
if n in differences:
12+
return differences[nums[i]], i
13+
else:
14+
differences[target - n] = i
15+
return -1, -1
16+
17+
18+
print(Solution().twoSum([2, 7, 11, 15], 9))

002_add-two-numbers.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Definition for singly-linked list.
2+
class ListNode(object):
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
8+
class Solution(object):
9+
def addTwoNumbers(self, l1, l2):
10+
"""
11+
:type l1: ListNode
12+
:type l2: ListNode
13+
:rtype: ListNode
14+
"""
15+
carry = 0
16+
head = current = ListNode(0)
17+
while l1 or l2 or carry:
18+
if l1:
19+
val1 = l1.val
20+
l1 = l1.next
21+
else:
22+
val1 = 0
23+
if l2:
24+
val2 = l2.val
25+
l2 = l2.next
26+
else:
27+
val2 = 0
28+
carry, step_val = divmod(val1 + val2 + carry, 10)
29+
current.next = ListNode(step_val)
30+
current = current.next
31+
32+
return head.next
33+
34+
35+
def CreateList(num_list):
36+
head = n = ListNode(0)
37+
for l in num_list:
38+
n.next = ListNode(l)
39+
n = n.next
40+
return head.next
41+
42+
l1 = CreateList([2, 4, 3])
43+
44+
l2 = CreateList([5, 6, 4])
45+
46+
Solution().addTwoNumbers(l1, l2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
3+
def lengthOfLongestSubstring(self, s):
4+
"""
5+
:type s: str
6+
:rtype: int
7+
"""
8+
substring_list = []
9+
substring = []
10+
for char in s:
11+
if char not in substring:
12+
substring.append(char)
13+
else:
14+
substring_list.append("".join(substring))
15+
substring = substring[substring.index(char) + 1:len(substring)]
16+
substring.append(char)
17+
substring_list.append("".join(substring))
18+
return len(max(substring_list, key=len))
19+
20+
print(Solution().lengthOfLongestSubstring("bpfbhmipx"))

005_longest-palindromic-substring.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
3+
def longestPalindrome(self, s):
4+
"""
5+
:type s: str
6+
:rtype: str
7+
"""
8+
9+
for i in range(len(s) - 1, 0, -1):
10+
for j in range(0, len(s) - i):
11+
substring = s[j:j + i + 1]
12+
if substring == substring[::-1]:
13+
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"))
30+
31+
print(Solution().longestPalindrome("esbtzjaaijqkgmtaajpsdfiqtvxsgfvijpxrvxgfumsuprzlyvhclgkhccmcnquukivlpnjlfteljvykbddtrpmxzcrdqinsnlsteonhcegtkoszzonkwjevlasgjlcquzuhdmmkhfniozhuphcfkeobturbuoefhmtgcvhlsezvkpgfebbdbhiuwdcftenihseorykdguoqotqyscwymtjejpdzqepjkadtftzwebxwyuqwyeegwxhroaaymusddwnjkvsvrwwsmolmidoybsotaqufhepinkkxicvzrgbgsarmizugbvtzfxghkhthzpuetufqvigmyhmlsgfaaqmmlblxbqxpluhaawqkdluwfirfngbhdkjjyfsxglsnakskcbsyafqpwmwmoxjwlhjduayqyzmpkmrjhbqyhongfdxmuwaqgjkcpatgbrqdllbzodnrifvhcfvgbixbwywanivsdjnbrgskyifgvksadvgzzzuogzcukskjxbohofdimkmyqypyuexypwnjlrfpbtkqyngvxjcwvngmilgwbpcsseoywetatfjijsbcekaixvqreelnlmdonknmxerjjhvmqiztsgjkijjtcyetuygqgsikxctvpxrqtuhxreidhwcklkkjayvqdzqqapgdqaapefzjfngdvjsiiivnkfimqkkucltgavwlakcfyhnpgmqxgfyjziliyqhugphhjtlllgtlcsibfdktzhcfuallqlonbsgyyvvyarvaxmchtyrtkgekkmhejwvsuumhcfcyncgeqtltfmhtlsfswaqpmwpjwgvksvazhwyrzwhyjjdbphhjcmurdcgtbvpkhbkpirhysrpcrntetacyfvgjivhaxgpqhbjahruuejdmaghoaquhiafjqaionbrjbjksxaezosxqmncejjptcksnoq"))

006_zigzag-conversion.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution(object):
2+
def convert(self, s, numRows):
3+
"""
4+
:type s: str
5+
:type numRows: int
6+
:rtype: str
7+
"""
8+
converted = [''] * numRows
9+
row = 0
10+
i = 0
11+
down = True
12+
if numRows > 1:
13+
while i < len(s):
14+
converted[row] += s[i]
15+
if down and row != (numRows - 1):
16+
row += 1
17+
elif down and row == (numRows - 1):
18+
down = not down
19+
row -= 1
20+
elif not down and row == 0:
21+
down = not down
22+
row += 1
23+
elif not down:
24+
row -= 1
25+
i += 1
26+
else:
27+
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+
'''
46+
return ''.join(converted)
47+
48+
49+
print(Solution().convert("PAYPALISHIRING", 4))

007_reverse-integer.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
sys.maxint = 32
3+
4+
class Solution(object):
5+
6+
def reverse(self, x):
7+
"""
8+
:type x: int
9+
:rtype: int
10+
"""
11+
rev = 0
12+
negative = False
13+
if x < 0:
14+
x = 0 - x
15+
negative = True
16+
17+
while x != 0:
18+
x, quotient = divmod(x, 10)
19+
try:
20+
rev = rev * 10 + quotient
21+
except OverflowError:
22+
return 0
23+
pass
24+
if negative:
25+
rev = 0 - rev
26+
if rev > 2147483647 or rev <-2147483648:
27+
rev = 0
28+
return int(rev)
29+
30+
print(Solution().reverse(123))
31+
print(Solution().reverse(-123))
32+
print(Solution().reverse(2147483651))

008_string-to-integer-(atoi).py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution(object):
2+
def myAtoi(self, str):
3+
"""
4+
:type str: str
5+
:rtype: int
6+
"""
7+
str = str.strip()
8+
num_dict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
9+
value = 0
10+
try:
11+
if str[0] == '-':
12+
neg = True
13+
str = str[1:]
14+
elif str[0] == '+':
15+
neg = False
16+
str = str[1:]
17+
else:
18+
neg = False
19+
except IndexError:
20+
return value
21+
for s in str:
22+
if s in num_dict:
23+
value = value * 10 + num_dict[s]
24+
else:
25+
break
26+
27+
if neg:
28+
value = 0 - value
29+
30+
if value > 2147483647:
31+
return 2147483647
32+
33+
if value < -2147483648:
34+
return -2147483648
35+
36+
return value
37+
38+
v = Solution().myAtoi(' -124a2324')
39+
print(v, type(v))

009_palindrome-number.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def isPalindrome(self, x):
3+
"""
4+
:type x: int
5+
:rtype: bool
6+
"""
7+
if x >= 0 and x < 2147483647:
8+
num_range = 1
9+
while x > num_range:
10+
num_range *= 10
11+
while x >= 10 or num_range > 10:
12+
l, x = divmod(x, num_range / 10)
13+
x, r = divmod(x, 10)
14+
print(num_range, r, l, x)
15+
if l != r:
16+
return False
17+
num_range /= 100
18+
return True
19+
20+
return False
21+
22+
print(Solution().isPalindrome(1000021))

011_container-with-most-water.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def maxArea(self, height):
3+
"""
4+
:type height: List[int]
5+
:rtype: int
6+
"""
7+
i = 0
8+
j = len(height) - 1
9+
area = 0
10+
while i < j:
11+
area = max(area, (min(height[i], height[j]) * (j - i)))
12+
if height[i] < height[j]:
13+
i += 1
14+
else:
15+
j -= 1
16+
17+
return area
18+
19+
print(Solution().maxArea([2, 3, 5, 12, 4, 2, 4, 1]))

012_integer-to-roman.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution(object):
2+
def intToRoman(self, num):
3+
"""
4+
:type num: int
5+
:rtype: str
6+
"""
7+
roman_num = ""
8+
roman_dict = {1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M'}
9+
dec_roman_list = [1000, 500, 100, 50, 10, 5, 1]
10+
roman_index = 0
11+
max_roman = dec_roman_list[roman_index]
12+
13+
while num:
14+
if num >= max_roman:
15+
roman_num += roman_dict[max_roman]
16+
num = num - max_roman
17+
else:
18+
roman_index += 1
19+
max_roman = dec_roman_list[roman_index]
20+
roman_num = roman_num.replace("DCCCC", "CM") # 900
21+
roman_num = roman_num.replace("CCCC", "CD") # 400
22+
roman_num = roman_num.replace("LXXXX", "XC") # 90
23+
roman_num = roman_num.replace("XXXX", "XL") # 40
24+
roman_num = roman_num.replace("VIIII", "IX") # 9
25+
roman_num = roman_num.replace("IIII", "IV") # 4
26+
return roman_num
27+
28+
print(Solution().intToRoman(149))

013_roman-to-integer.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution(object):
2+
def romanToInt(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
8+
value = 0
9+
i = 0
10+
while i < len(s):
11+
12+
try:
13+
val_i = roman_dict[s[i]]
14+
except IndexError:
15+
val_i = 0
16+
except KeyError:
17+
val_i = 0
18+
19+
try:
20+
val_plus = roman_dict[s[i + 1]]
21+
except IndexError:
22+
val_plus = 0
23+
except KeyError:
24+
val_plus = 0
25+
26+
if val_i >= val_plus:
27+
value = value + val_i
28+
i += 1
29+
else:
30+
value = value + val_plus - val_i
31+
i += 2
32+
return value
33+
34+
35+
print(Solution().romanToInt("CVI"))

014_longest-common-prefix.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def longestCommonPrefix(self, strs):
3+
"""
4+
:type strs: List[str]
5+
:rtype: str
6+
"""
7+
common = []
8+
if strs:
9+
shortest = min(strs, key=len)
10+
for i in range(len(shortest)):
11+
for s in strs:
12+
if s[i] == strs[0][i]:
13+
pass
14+
else:
15+
return "".join(common)
16+
common.append(strs[0][i])
17+
return "".join(common)
18+
19+
print(Solution().longestCommonPrefix(["aaa", "aaaaa", "aaaaaaa"]))

0 commit comments

Comments
 (0)