Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ef01f3

Browse files
committed
leetcode
1 parent 914f2a9 commit 6ef01f3

File tree

147 files changed

+6451
-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.

147 files changed

+6451
-0
lines changed

‎leetcode/Add_Binary.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Given two binary strings, return their sum (also a binary string).
2+
#
3+
# The input strings are both non-empty and contains only characters 1 or 0.
4+
#
5+
# Example 1:
6+
#
7+
# Input: a = "11", b = "1"
8+
# Output: "100"
9+
10+
11+
# Example 2:
12+
#
13+
# Input: a = "1010", b = "1011"
14+
# Output: "10101"
15+
16+
17+
class Solution:
18+
def addBinary(self, a, b):
19+
x, y = int(a, 2), int(b, 2)
20+
21+
while y:
22+
answer = x ^ y
23+
carry = (x & y) << 1
24+
x, y = answer, carry
25+
26+
return bin(x)
27+
28+
29+
if __name__ == "__main__":
30+
a = "1010"
31+
b = "1011"
32+
print(Solution().addBinary(a, b))

‎leetcode/Add_Strings.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
2+
#
3+
# Note:
4+
#
5+
# The length of both num1 and num2 is < 5100.
6+
# Both num1 and num2 contains only digits 0-9.
7+
# Both num1 and num2 does not contain any leading zero.
8+
# You must not use any built-in BigInteger library or convert the inputs to integer directly.
9+
10+
11+
class Solution:
12+
def addStrings(self, num1, num2):
13+
14+
if len(num1) < len(num2):
15+
return self.sumNums(num1, num2)
16+
else:
17+
return self.sumNums(num2, num1)
18+
19+
def sumNums(self, low, high):
20+
dict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
21+
22+
i = len(low) - 1
23+
j = len(high) - 1
24+
res = []
25+
carry = 0
26+
while j >= 0:
27+
28+
while i >= 0:
29+
add = dict[low[i]] + dict[high[j]] + carry
30+
res = [str(add % 10)] + res
31+
carry = add // 10
32+
i -= 1
33+
j -= 1
34+
if j >= 0:
35+
add = dict[high[j]] + carry
36+
carry = add // 10
37+
res = [str(add % 10)] + res
38+
j -= 1
39+
if carry > 0:
40+
res = [str(carry)] + res
41+
42+
return "".join(res)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# You are given two non-empty linked lists representing two non-negative integers.
2+
# The most significant digit comes first and each of their nodes contain a single digit.
3+
# Add the two numbers and return it as a linked list.
4+
#
5+
# You may assume the two numbers do not contain any leading zero, except the number 0 itself.
6+
#
7+
# Follow up:
8+
# What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
9+
#
10+
# Example:
11+
#
12+
# Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
13+
# Output: 7 -> 8 -> 0 -> 7
14+
15+
# Definition for singly-linked list.
16+
17+
18+
class ListNode:
19+
def __init__(self, x):
20+
self.val = x
21+
self.next = None
22+
23+
24+
class Solution:
25+
26+
def addTwoNumbers(self, l1, l2):
27+
28+
def ll_to_l(l):
29+
num = ""
30+
while l:
31+
num += str(l.val)
32+
l = l.next
33+
return int(num)
34+
35+
n1 = ll_to_l(l1)
36+
n2 = ll_to_l(l2)
37+
n = n1 + n2
38+
n = str(n)
39+
head = ListNode(int(n[0]))
40+
newnode = head
41+
42+
for i in range(1, len(n)):
43+
new = ListNode(int(n[i]))
44+
newnode.next = new
45+
newnode = newnode.next
46+
return head
47+
48+
49+
if __name__ == "__main__":
50+
l1 = ListNode(7)
51+
l1.next = ListNode(2)
52+
l1.next.next = ListNode(4)
53+
l1.next.next.next = ListNode(3)
54+
55+
l2 = ListNode(5)
56+
l2.next = ListNode(6)
57+
l2.next.next = ListNode(4)
58+
# Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
59+
# Output: 7 -> 8 -> 0 -> 7
60+
ans = Solution().addTwoNumbers(l1, l2)
61+
while ans:
62+
print(ans.val)
63+
ans = ans.next

‎leetcode/Add_Two_Linked_List_Reverse.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# You are given two non-empty linked lists representing two non-negative integers.
2+
# The digits are stored in reverse order and each of their nodes contain a single digit.
3+
# Add the two numbers and return it as a linked list.
4+
#
5+
# You may assume the two numbers do not contain any leading zero, except the number 0 itself.
6+
#
7+
# Example:
8+
#
9+
# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
10+
# Output: 7 -> 0 -> 8
11+
# Explanation: 342 + 465 = 807.
12+
13+
14+
class ListNode(object):
15+
def __init__(self, x):
16+
self.val = x
17+
self.next = None
18+
19+
20+
class Solution(object):
21+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
22+
23+
list1 = l1
24+
list2 = l2
25+
res = []
26+
carry = 0
27+
28+
while list1 is not None and list2 is not None:
29+
currSum = list1.val + list2.val + carry
30+
res.append(currSum % 10)
31+
carry = currSum // 10
32+
33+
list1 = list1.next
34+
list2 = list2.next
35+
36+
while list1 is not None:
37+
currSum = list1.val + carry
38+
res.append(currSum % 10)
39+
carry = currSum // 10
40+
list1 = list1.next
41+
42+
while list2 is not None:
43+
currSum = list2.val + carry
44+
res.append(currSum % 10)
45+
carry = currSum // 10
46+
list2 = list2.next
47+
48+
if carry:
49+
res.append(carry)
50+
51+
newnode = ListNode(0)
52+
node = newnode
53+
for num in res:
54+
node.next = ListNode(num)
55+
node = node.next
56+
57+
return newnode.next
58+
59+
60+
if __name__ == "__main__":
61+
l1 = ListNode(2)
62+
l1.next = ListNode(4)
63+
l1.next.next = ListNode(3)
64+
65+
l2 = ListNode(5)
66+
l2.next = ListNode(6)
67+
l2.next.next = ListNode(4)
68+
69+
ans = Solution().addTwoNumbers(l1, l2)
70+
print(ans.next.next.val)
71+
print(ans.next.val)
72+
print(ans.val)

‎leetcode/Backspace_String_compare.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Given two strings S and T, return if they are equal when both are typed
2+
# into empty text editors. '#' means a backspace character.
3+
#
4+
# Example 1:
5+
#
6+
# Input: S = "ab#c", T = "ad#c"
7+
# Output: true
8+
# Explanation: Both S and T become "ac".
9+
# Example 2:
10+
#
11+
# Input: S = "ab##", T = "c#d#"
12+
# Output: true
13+
# Explanation: Both S and T become "".
14+
# Example 3:
15+
#
16+
# Input: S = "a##c", T = "#a#c"
17+
# Output: true
18+
# Explanation: Both S and T become "c".
19+
# Example 4:
20+
#
21+
# Input: S = "a#c", T = "b"
22+
# Output: false
23+
# Explanation: S becomes "c" while T becomes "b".
24+
# Note:
25+
#
26+
# 1 <= S.length <= 200
27+
# 1 <= T.length <= 200
28+
# S and T only contain lowercase letters and '#' characters.
29+
# Follow up:
30+
#
31+
# Can you solve it in O(N) time and O(1) space?
32+
33+
34+
class Solution:
35+
def backspaceCompare(self, S, T):
36+
p1 = len(S) - 1
37+
p2 = len(S) - 1
38+
skip1 = 0
39+
skip2 = 0
40+
41+
while p1 >= 0 or p2 >= 0:
42+
43+
while p1 >= 0:
44+
if S[p1] == '#':
45+
skip1 += 1
46+
p1 -= 1
47+
elif skip1 > 0:
48+
p1 -= 1
49+
skip1 -= 1
50+
else:
51+
break
52+
while p2 >= 0:
53+
if T[p2] == '#':
54+
skip2 += 1
55+
p2 -= 1
56+
elif skip2 > 0:
57+
skip2 -= 1
58+
p2 -= 1
59+
else:
60+
break
61+
if (p1 >= 0) != (p2 >= 0):
62+
return False
63+
if S[p1] != T[p2]:
64+
return False
65+
p1 -= 1
66+
p2 -= 1
67+
return True
68+
69+
70+
if __name__ == "__main__":
71+
S = "a##c"
72+
T = "#a#c"
73+
# Time Complexity is O(n) and space is O(1)
74+
print(Solution().backspaceCompare(S, T))

‎leetcode/Balanced_Binary_Tree.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Given a binary tree, determine if it is height-balanced.
2+
#
3+
# For this problem, a height-balanced binary tree is defined as:
4+
#
5+
# a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
6+
#
7+
# Example 1:
8+
#
9+
# Given the following tree [3,9,20,null,null,15,7]:
10+
#
11+
# 3
12+
# / \
13+
# 9 20
14+
# / \
15+
# 15 7
16+
# Return true.
17+
#
18+
# Example 2:
19+
#
20+
# Given the following tree [1,2,2,3,3,null,null,4,4]:
21+
#
22+
# 1
23+
# / \
24+
# 2 2
25+
# / \
26+
# 3 3
27+
# / \
28+
# 4 4
29+
# Return false.
30+
31+
32+
class TreeNode:
33+
def __init__(self, val):
34+
self.val = val
35+
self.left = None
36+
self.right = None
37+
38+
39+
class Solution:
40+
def isBalanced(self, root):
41+
def helper(root):
42+
if not root:
43+
return (True, 0)
44+
45+
leftB, leftH = helper(root.left)
46+
rightB, rightH = helper(root.right)
47+
48+
return (leftB and rightB and abs(leftH - rightH) <= 1, max(leftH, rightH) + 1)
49+
50+
return helper(root)[0]
51+
52+
53+
if __name__ == "__main__":
54+
root = TreeNode(1)
55+
root.left = TreeNode(2)
56+
root.right = TreeNode(2)
57+
root.left.left = TreeNode(3)
58+
root.right.right = TreeNode(3)
59+
root.left.left.left = TreeNode(4)
60+
root.right.right.right = TreeNode(4)
61+
print(Solution().isBalanced(root))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Say you have an array for which the ith element is the price of a given stock on day i.
2+
#
3+
# If you were only permitted to complete at most one transaction (i.e., buy one and sell
4+
# one share of the stock), design an algorithm to find the maximum profit.
5+
#
6+
# Note that you cannot sell a stock before you buy one.
7+
#
8+
# Example 1:
9+
#
10+
# Input: [7,1,5,3,6,4]
11+
# Output: 5
12+
# Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
13+
# Not 7-1 = 6, as selling price needs to be larger than buying price.
14+
# Example 2:
15+
#
16+
# Input: [7,6,4,3,1]
17+
# Output: 0
18+
# Explanation: In this case, no transaction is done, i.e. max profit = 0.
19+
20+
21+
class Solution:
22+
def maxProfit(self, prices):
23+
if not prices:
24+
return 0
25+
Min = prices[0]
26+
res = 0
27+
28+
for price in prices:
29+
if Min > price:
30+
Min = min(Min, price)
31+
res = max(price - Min, res)
32+
33+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /