Skip to content

Commit ccbbf7a

Browse files
committed
added problems
1 parent 05447b7 commit ccbbf7a

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def getSum(self, a: int, b: int) -> int:
3+
mask = 0xFFFFFFFF # 32 bits
4+
while(b != 0):
5+
s = (a ^ b) & mask
6+
c = ((a & b) << 1) & mask
7+
a = s & mask
8+
b = c & mask
9+
10+
max_int = 0x7FFFFFFF
11+
12+
if a < max_int:
13+
return a
14+
else:
15+
return ~(a ^ mask)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ops = {
2+
'+': lambda a, b: a + b,
3+
'-': lambda a, b: a - b,
4+
'*': lambda a, b: a * b,
5+
'/': lambda a, b: int(a / b)
6+
}
7+
8+
class Solution:
9+
def evalRPN(self, tokens: List[str]) -> int:
10+
stack = []
11+
12+
for token in tokens:
13+
if token in ops:
14+
num1 = stack.pop()
15+
num2 = stack.pop()
16+
res = ops[token](num2, num1)
17+
stack.append(res)
18+
else:
19+
stack.append(int(token))
20+
21+
return stack[0]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def majorityElement(self, nums: List[int]) -> int:
3+
count = 0
4+
candidate = nums[0]
5+
6+
for num in nums:
7+
if count == 0:
8+
candidate = num
9+
if num == candidate:
10+
count += 1
11+
else:
12+
count -= 1
13+
14+
return candidate

0 commit comments

Comments
 (0)