Skip to content

Commit 854f612

Browse files
committed
Merge branch 'main' into GO
2 parents e9852d4 + a96338e commit 854f612

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

Easy/palindrome_number.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def isPalindrome(self, x):
3+
"""
4+
:type x: int
5+
:rtype: bool
6+
"""
7+
strX = str(x)
8+
return strX == strX[::-1]
9+
10+
print(Solution().isPalindrome(121)) #true
11+
print(Solution().isPalindrome(-121)) #false
12+
print(Solution().isPalindrome(10)) #false

Easy/two_sum.py

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

Hard/median_of_two_sorted_arrays.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def findMedianSortedArrays(self, nums1, nums2):
3+
"""
4+
:type nums1: List[int]
5+
:type nums2: List[int]
6+
:rtype: float
7+
"""
8+
if len(nums1) > 1000 or len(nums2) > 1000:
9+
return None
10+
11+
joinArr = sorted(nums1 + nums2)
12+
lenJoinArr = len(joinArr)
13+
14+
if lenJoinArr == 0 or lenJoinArr > 2000:
15+
return None
16+
17+
mid = lenJoinArr // 2
18+
if lenJoinArr % 2 == 1:
19+
return float(joinArr[mid])
20+
else:
21+
return (joinArr[mid - 1] + joinArr[mid]) / 2.0
22+
23+
24+
25+
26+
print(Solution().findMedianSortedArrays([1,3], [2]))
27+
print(Solution().findMedianSortedArrays([1,2], [3,4]))

Medium/add_two_numbers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Definition for singly-linked list.
2+
class ListNode(object):
3+
def __init__(self, val=0, next=None):
4+
self.val = val
5+
self.next = next
6+
7+
class Solution(object):
8+
def addTwoNumbers(self, l1, l2):
9+
node = ListNode()
10+
current = node
11+
carry = 0
12+
13+
while l1 or l2 or carry:
14+
val1 = l1.val if l1 else 0
15+
val2 = l2.val if l2 else 0
16+
17+
s = val1 + val2 + carry
18+
carry = s // 10
19+
current.next = ListNode(s % 10)
20+
current = current.next
21+
22+
if l1:
23+
l1 = l1.next
24+
25+
if l2:
26+
l2 = l2.next
27+
28+
return node.next
29+
30+
print(Solution().addTwoNumbers([2, 4, 3], [5, 6, 4]))
31+
print(Solution().addTwoNumbers([9, 9, 9, 9, 9, 9, 9], [9, 9, 9, 9]))
32+
print(Solution().addTwoNumbers([0], [0]))

0 commit comments

Comments
 (0)