Skip to content

Commit b4e6d9a

Browse files
committed
added more problems
1 parent 0196e44 commit b4e6d9a

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
count = 0
4+
while n:
5+
n &= n - 1
6+
count += 1
7+
return count
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def hammingDistance(self, x: int, y: int) -> int:
3+
x = x ^ y
4+
y = 0
5+
6+
while x:
7+
y += 1
8+
x = x & (x - 1)
9+
10+
return y
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def reverseBits(self, n: int) -> int:
3+
res = 0
4+
for _ in range(32):
5+
res = (res << 1) + (n & 1)
6+
n >>= 1
7+
return res
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def generate(self, numRows):
3+
result = []
4+
5+
for row in range(1, numRows+1):
6+
row_arr = [1] * row
7+
for i in range(1, row-1):
8+
row_arr[i] = result[row-2][i-1] + result[row-2][i]
9+
result.append(row_arr)
10+
11+
return result
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
if len(s) % 2 != 0:
4+
return False
5+
6+
stack = []
7+
opens = ('(', '{', '[')
8+
matches = (('(', ')'), ('{', '}'), ('[', ']'))
9+
10+
for paren in s:
11+
if paren in opens:
12+
stack.append(paren)
13+
else:
14+
if stack:
15+
last_open = stack.pop()
16+
match = (last_open, paren)
17+
if match not in matches:
18+
return False
19+
else:
20+
return False
21+
22+
return len(stack) == 0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def missingNumber(self, nums):
3+
i = 0
4+
n = len(nums)
5+
6+
while i < n:
7+
j = nums[i]
8+
if nums[i] < n and nums[i] != nums[j]:
9+
nums[i], nums[j] = nums[j], nums[i]
10+
else:
11+
i += 1
12+
13+
for i, num in enumerate(nums):
14+
if i != num:
15+
return i
16+
17+
return n
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def missingNumber(self, nums):
3+
i = 0
4+
n = len(nums)
5+
6+
while i < n:
7+
j = nums[i]
8+
if nums[i] < n and nums[i] != nums[j]:
9+
nums[i], nums[j] = nums[j], nums[i]
10+
else:
11+
i += 1
12+
13+
for i, num in enumerate(nums):
14+
if i != num:
15+
return i
16+
17+
return n

0 commit comments

Comments
 (0)