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 03dbd1e

Browse files
Added Leetcode easy problems
1 parent 7b9d0e8 commit 03dbd1e

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
# CONTAINS DUPLICATE II
3+
4+
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
5+
6+
Example 1:
7+
8+
Input: nums = [1,2,3,1], k = 3
9+
Output: true
10+
11+
Example 2:
12+
13+
Input: nums = [1,0,1,1], k = 1
14+
Output: true
15+
16+
Example 3:
17+
18+
Input: nums = [1,2,3,1,2,3], k = 2
19+
Output: false
20+
"""
21+
22+
class Solution:
23+
def containsNearbyDuplicate(self, nums, k: int) -> bool:
24+
25+
d={}
26+
27+
for i,v in enumerate(nums):
28+
if v in d:
29+
if abs(d[v]-i)>k:
30+
d[v]=i
31+
else:
32+
return True
33+
else:
34+
d[v]=i
35+
36+
return False
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
# IMPLEMENT STACKS USING QUEUES
3+
4+
Implement a last in first out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal queue (push, top, pop, and empty).
5+
6+
Implement the MyStack class:
7+
8+
void push(int x) Pushes element x to the top of the stack.
9+
int pop() Removes the element on the top of the stack and returns it.
10+
int top() Returns the element on the top of the stack.
11+
boolean empty() Returns true if the stack is empty, false otherwise.
12+
Notes:
13+
14+
You must use only standard operations of a queue, which means only push to back, peek/pop from front, size, and is empty operations are valid.
15+
Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue), as long as you use only a queue's standard operations.
16+
17+
Example 1:
18+
19+
Input
20+
["MyStack", "push", "push", "top", "pop", "empty"]
21+
[[], [1], [2], [], [], []]
22+
Output
23+
[null, null, null, 2, 2, false]
24+
25+
Explanation
26+
MyStack myStack = new MyStack();
27+
myStack.push(1);
28+
myStack.push(2);
29+
myStack.top(); // return 2
30+
myStack.pop(); // return 2
31+
myStack.empty(); // return False
32+
33+
Constraints:
34+
35+
1 <= x <= 9
36+
At most 100 calls will be made to push, pop, top, and empty.
37+
All the calls to pop and top are valid.
38+
39+
Follow-up: Can you implement the stack such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer. You can use more than two queues.
40+
"""
41+
42+
class MyStack:
43+
44+
def __init__(self):
45+
"""
46+
Initialize your data structure here.
47+
"""
48+
self.q = []
49+
self.q2 = []
50+
51+
def push(self, x: int) -> None:
52+
"""
53+
Push element x onto stack.
54+
"""
55+
while self.q:
56+
self.q2.append(self.q.pop(0))
57+
58+
self.q.append(x)
59+
60+
while self.q2:
61+
self.q.append(self.q2.pop(0))
62+
63+
64+
def pop(self) -> int:
65+
"""
66+
Removes the element on top of the stack and returns that element.
67+
"""
68+
return self.q.pop(0)
69+
70+
def top(self) -> int:
71+
"""
72+
Get the top element.
73+
"""
74+
return self.q[0]
75+
76+
def empty(self) -> bool:
77+
"""
78+
Returns whether the stack is empty.
79+
"""
80+
return len(self.q) == 0
81+
82+
83+
# Your MyStack object will be instantiated and called as such:
84+
# obj = MyStack()
85+
# obj.push(x)
86+
# param_2 = obj.pop()
87+
# param_3 = obj.top()
88+
# param_4 = obj.empty()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
# INVERT A BINARY TREE
3+
4+
Invert a binary tree.
5+
6+
Example:
7+
8+
Input:
9+
10+
4
11+
- -
12+
2 7
13+
- - - -
14+
1 3 6 9
15+
Output:
16+
17+
4
18+
- -
19+
7 2
20+
- - - -
21+
9 6 3 1
22+
23+
Trivia:
24+
This problem was inspired by this original tweet by Max Howell:
25+
26+
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
27+
"""
28+
29+
# Definition for a binary tree node.
30+
class TreeNode:
31+
def __init__(self, val=0, left=None, right=None):
32+
self.val = val
33+
self.left = left
34+
self.right = right
35+
36+
class Solution:
37+
def invertTree(self, root: TreeNode) -> TreeNode:
38+
if not root:
39+
return None
40+
41+
if not root.left and not root.right:
42+
return root
43+
44+
temp = self.invertTree(root.right)
45+
root.right = self.invertTree(root.left)
46+
root.left = temp
47+
48+
return root

0 commit comments

Comments
(0)

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