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 df8b948

Browse files
Added Leetcode easy problems
1 parent 03dbd1e commit df8b948

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
# IMPLEMENT QUEUES USING STACKS
3+
4+
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
5+
6+
Implement the MyQueue class:
7+
8+
void push(int x) Pushes element x to the back of the queue.
9+
int pop() Removes the element from the front of the queue and returns it.
10+
int peek() Returns the element at the front of the queue.
11+
boolean empty() Returns true if the queue is empty, false otherwise.
12+
Notes:
13+
14+
You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
15+
Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
16+
Follow-up: Can you implement the queue 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.
17+
18+
Example 1:
19+
20+
Input
21+
["MyQueue", "push", "push", "peek", "pop", "empty"]
22+
[[], [1], [2], [], [], []]
23+
Output
24+
[null, null, null, 1, 1, false]
25+
26+
Explanation
27+
MyQueue myQueue = new MyQueue();
28+
myQueue.push(1); // queue is: [1]
29+
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
30+
myQueue.peek(); // return 1
31+
myQueue.pop(); // return 1, queue is [2]
32+
myQueue.empty(); // return false
33+
34+
Constraints:
35+
36+
1 <= x <= 9
37+
At most 100 calls will be made to push, pop, peek, and empty.
38+
All the calls to pop and peek are valid.
39+
"""
40+
41+
class MyQueue:
42+
43+
def __init__(self):
44+
"""
45+
Initialize your data structure here.
46+
"""
47+
self.stack1 = []
48+
self.stack2 = []
49+
50+
def push(self, x: int) -> None:
51+
"""
52+
Push element x to the back of queue.
53+
"""
54+
while self.stack1:
55+
self.stack2.append(self.stack1.pop())
56+
57+
self.stack1.append(x)
58+
59+
while self.stack2:
60+
self.stack1.append(self.stack2.pop())
61+
62+
def pop(self) -> int:
63+
"""
64+
Removes the element from in front of queue and returns that element.
65+
"""
66+
return self.stack1.pop()
67+
68+
69+
def peek(self) -> int:
70+
"""
71+
Get the front element.
72+
"""
73+
return self.stack1[-1]
74+
75+
def empty(self) -> bool:
76+
"""
77+
Returns whether the queue is empty.
78+
"""
79+
return len(self.stack1) == 0
80+
81+
82+
# Your MyQueue object will be instantiated and called as such:
83+
# obj = MyQueue()
84+
# obj.push(x)
85+
# param_2 = obj.pop()
86+
# param_3 = obj.peek()
87+
# param_4 = obj.empty()

‎Leetcode/easy/power-of-two.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
# POWER OF TWO
3+
4+
Given an integer n, return true if it is a power of two. Otherwise, return false.
5+
6+
An integer n is a power of two, if there exists an integer x such that n == 2x.
7+
8+
Example 1:
9+
10+
Input: n = 1
11+
Output: true
12+
Explanation: 20 = 1
13+
14+
Example 2:
15+
16+
Input: n = 16
17+
Output: true
18+
Explanation: 24 = 16
19+
20+
Example 3:
21+
22+
Input: n = 3
23+
Output: false
24+
25+
Example 4:
26+
27+
Input: n = 4
28+
Output: true
29+
30+
Example 5:
31+
32+
Input: n = 5
33+
Output: false
34+
35+
Constraints:
36+
37+
-231 <= n <= 231 - 1
38+
"""
39+
40+
class Solution:
41+
def isPowerOfTwo(self, n: int) -> bool:
42+
43+
while n > 1:
44+
n /= 2
45+
46+
return n == 1

‎Leetcode/easy/summary-ranges.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
# SUMMARY RANGES
3+
4+
You are given a sorted unique integer array nums.
5+
6+
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.
7+
8+
Each range [a,b] in the list should be output as:
9+
10+
"a->b" if a != b
11+
"a" if a == b
12+
13+
Example 1:
14+
15+
Input: nums = [0,1,2,4,5,7]
16+
Output: ["0->2","4->5","7"]
17+
Explanation: The ranges are:
18+
[0,2] --> "0->2"
19+
[4,5] --> "4->5"
20+
[7,7] --> "7"
21+
22+
Example 2:
23+
24+
Input: nums = [0,2,3,4,6,8,9]
25+
Output: ["0","2->4","6","8->9"]
26+
Explanation: The ranges are:
27+
[0,0] --> "0"
28+
[2,4] --> "2->4"
29+
[6,6] --> "6"
30+
[8,9] --> "8->9"
31+
32+
Example 3:
33+
34+
Input: nums = []
35+
Output: []
36+
37+
Example 4:
38+
39+
Input: nums = [-1]
40+
Output: ["-1"]
41+
42+
Example 5:
43+
44+
Input: nums = [0]
45+
Output: ["0"]
46+
47+
Constraints:
48+
49+
0 <= nums.length <= 20
50+
-231 <= nums[i] <= 231 - 1
51+
All the values of nums are unique.
52+
nums is sorted in ascending order.
53+
"""
54+
55+
class Solution:
56+
def summaryRanges(self, nums) -> List[str]:
57+
res = []
58+
i = 0
59+
while i < len(nums):
60+
start = nums[i]
61+
end = nums[i]
62+
j = i + 1
63+
while j < len(nums) and nums[j] - 1 == nums[j - 1]:
64+
end = nums[j]
65+
j += 1
66+
67+
if start == end:
68+
res.append(str(start))
69+
else:
70+
res.append(str(start)+"->"+str(end))
71+
i = j
72+
73+
return res

0 commit comments

Comments
(0)

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