Skip to content

Commit 2fb6c2a

Browse files
committed
10.9
1 parent 4e5c2cd commit 2fb6c2a

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

159.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""
3+
@param nums: a rotated sorted array
4+
@return: the minimum number in the array
5+
"""
6+
def findMin(self, nums):
7+
# write your code here
8+
if len(nums) == 0:
9+
return None
10+
if len(nums) == 1:
11+
return nums[0]
12+
left = 0
13+
right = len(nums) - 1
14+
while right - left > 1:
15+
if nums[left] < nums[right]:
16+
return nums[left]
17+
mid = (left + right) // 2
18+
if nums[mid] > nums[left]:
19+
left = mid
20+
else:
21+
right = mid
22+
return min(nums[right], nums[left])
23+
24+
print(Solution.findMin(1, [4,5,6,7,0,1,2]))

365.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
"""
3+
@param: num: An integer
4+
@return: An integer
5+
"""
6+
def countOnes(self, num):
7+
# write your code here
8+
count = 0
9+
if num >= 0:
10+
while num != 0:
11+
count += num%2
12+
num = num >> 1
13+
return count
14+
else:
15+
return Solution.countOnes(1, 2**32 + num)
16+
17+
print(Solution.countOnes(1, -178))

38.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
"""
3+
@param matrix: A list of lists of integers
4+
@param target: An integer you want to search in matrix
5+
@return: An integer indicate the total occurrence of target in the given matrix
6+
"""
7+
def searchMatrix(self, matrix, target):
8+
# write your code here
9+
if len(matrix) == 0:
10+
return 0
11+
if len(matrix[0]) == 0:
12+
return 0
13+
res = 0
14+
n, m = len(matrix[0]), len(matrix)
15+
x, y = 0, 0
16+
while x != n and y != m:
17+
if matrix[y][x] == target:
18+
res += 1
19+
for i in range(x + 1, n):
20+
if matrix[y][i] == target:
21+
res += 1
22+
break
23+
for j in range(y + 1, m):
24+
if matrix[j][x] == target:
25+
res += 1
26+
break
27+
x += 1
28+
y += 1
29+
return res
30+
31+
# print(Solution.searchMatrix(1, [[1,3,5,7],[2,4,7,8],[3,5,9,10]], 3))
32+
print(Solution.searchMatrix(1, [[3,4]], 3))
33+
print(Solution.searchMatrix(1, [[1,3,5,7],[3,11,16,20],[23,30,34,50]], 3))

0 commit comments

Comments
 (0)