Skip to content

Commit 6a30f74

Browse files
committedDec 22, 2020
Added Leetcode easy problems
1 parent b15127d commit 6a30f74

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
 

‎Leetcode/easy/min-stack.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
# MIN STACK
3+
4+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
5+
6+
push(x) -- Push element x onto stack.
7+
pop() -- Removes the element on top of the stack.
8+
top() -- Get the top element.
9+
getMin() -- Retrieve the minimum element in the stack.
10+
11+
12+
Example 1:
13+
14+
Input
15+
["MinStack","push","push","push","getMin","pop","top","getMin"]
16+
[[],[-2],[0],[-3],[],[],[],[]]
17+
18+
Output
19+
[null,null,null,null,-3,null,0,-2]
20+
21+
Explanation
22+
MinStack minStack = new MinStack();
23+
minStack.push(-2);
24+
minStack.push(0);
25+
minStack.push(-3);
26+
minStack.getMin(); // return -3
27+
minStack.pop();
28+
minStack.top(); // return 0
29+
minStack.getMin(); // return -2
30+
31+
32+
Constraints:
33+
34+
Methods pop, top and getMin operations will always be called on non-empty stacks.
35+
"""
36+
37+
class MinStack:
38+
39+
def __init__(self):
40+
"""
41+
initialize your data structure here.
42+
"""
43+
self.stk=[]
44+
self.min=[float('inf')]
45+
46+
def push(self, x: int) -> None:
47+
self.stk.append(x)
48+
if x <= self.min[-1]: self.min.append(x)
49+
50+
def pop(self) -> None:
51+
x=self.stk.pop()
52+
if x == self.min[-1]: self.min.pop()
53+
return self.stk
54+
55+
def top(self) -> int:
56+
return self.stk[-1]
57+
58+
def getMin(self) -> int:
59+
return self.min[-1]

‎Leetcode/easy/path-sum.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
# PATH SUM
3+
4+
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
5+
6+
Note: A leaf is a node with no children.
7+
8+
Example:
9+
10+
Given the below binary tree and sum = 22,
11+
12+
5
13+
- -
14+
4 8
15+
- - -
16+
11 13 4
17+
- - -
18+
7 2 1
19+
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
20+
"""
21+
22+
# Definition for a binary tree node.
23+
class TreeNode:
24+
def __init__(self, val=0, left=None, right=None):
25+
self.val = val
26+
self.left = left
27+
self.right = right
28+
29+
class Solution:
30+
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
31+
if not root:
32+
return False
33+
34+
if not root.left and not root.right:
35+
if sum - root.val == 0:
36+
return True
37+
else:
38+
return False
39+
40+
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)

‎Leetcode/easy/single-number.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
# SINGLE NUMBER
3+
4+
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
5+
6+
Follow up: Could you implement a solution with a linear runtime complexity and without using extra memory?
7+
8+
9+
Example 1:
10+
11+
Input: nums = [2,2,1]
12+
Output: 1
13+
14+
Example 2:
15+
16+
Input: nums = [4,1,2,1,2]
17+
Output: 4
18+
19+
Example 3:
20+
21+
Input: nums = [1]
22+
Output: 1
23+
24+
Constraints:
25+
26+
1 <= nums.length <= 3 * 104
27+
-3 * 104 <= nums[i] <= 3 * 104
28+
Each element in the array appears twice except for one element which appears only once.
29+
"""
30+
31+
class Solution:
32+
def singleNumber(self, nums) -> int:
33+
res = 0
34+
for x in nums:
35+
res = res ^ x
36+
return res

0 commit comments

Comments
 (0)
Please sign in to comment.