# 61
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head or not head.next or k == 0: return head
l, r = head, head
count = 1
while r.next:
r = r.next
count += 1
k %= count
if k == 0: return head
#r.next = head
for _ in range(count-k-1):
l = l.next
new_head = l.next
l.next = None
r.next = head
return new_head
# 62 数学 and 动态规划
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
return comb(m + n - 2, n - 1)
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [[1] * (n) for _ in range (m)]
for i in range(1 , m):
for j in range(1 , n):
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
return dp[-1][-1]
# 63 跟上面一样
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
if not obstacleGrid or not obstacleGrid[0]: return 0
row = len(obstacleGrid)
col = len(obstacleGrid[0])
dp = [[0] * col for _ in range(row)]
dp[0][0] = 1 if obstacleGrid[0][0] == 0 else 0
for j in range(1, col):
if obstacleGrid[0][j] == 0:
dp[0][j] = dp[0][j-1]
for i in range(1, row):
if obstacleGrid[i][0] == 0:
dp[i][0] = dp[i-1][0]
for i in range(1, row):
for j in range(1, col):
if obstacleGrid[i][j] != 1:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
return dp[-1][-1]
# 64 跟62 , 63基本一样
class Solution:
def minPathSum(self,grid):
if not grid or not grid[0]:
return 0
m = len(grid)
n = len(grid[0])
for i in range(1 , m):
grid[i][0] = grid[i - 1][0] + grid[i][0]
for j in range(1 , n):
grid[0][j] = grid[0][j - 1] + grid[0][j]
for i in range(1 , m):
for j in range(1 , n):
grid[i][j] = min(grid[i][j] + grid[i - 1][j] , grid[i][j] + grid[i][j - 1])
return grid[-1][-1]
# 65
class Solution:
def isNumber(self, s: str) -> bool:
s = s.strip()
numbers = [str(i) for i in range(10)]
n = len(s)
e_show_up, dot_show_up, num_show_up, num_after_e = False, False, False, False
for i in range(n):
c = s[i]
if c in numbers:
num_show_up = True
num_after_e = True
elif c in ('+', '-'):
if i > 0 and s[i-1] != 'e':
return False
elif c == '.':
if dot_show_up or e_show_up:
return False
dot_show_up = True
elif c == 'e' or c =='E':
if e_show_up or not num_show_up:
return False
e_show_up = True
num_show_up = False
else:
return False
return num_show_up and num_after_e
# 66
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
x = 1
for i in range(len(digits) - 1 , -1 , -1):
x , y = divmod(digits[i] + x , 10)
digits[i] = y
if x:
digits.insert(0 , 1)
return digits
# 67
class Solution:
def addBinary(self, a,b):
i = len(a) - 1
j = len(b) - 1
res = ''
x = 0
while i >= 0 or j >= 0 or x:
tmp1 = int(a[i]) if i >= 0 else 0
tmp2 = int(b[j]) if j >= 0 else 0
x , y = divmod(tmp1 + tmp2 + x , 2)
res = str(y) + res
i -= 1
j -= 1
return res
# 68 不想写了 太难了。。。。
# 69
class Solution:
def mySqrt(self, x: int) -> int:
if x < 1:
return 0
if x < 4:
return 1
l , r = 0 , x
while 1:
mid = l + (r - l)//2
if mid ** 2 <= x and (mid + 1) ** 2 > x:
return mid
elif mid ** 2 < x:
l = mid
else:
r = mid
# 70 斐波拉契数列套了件衣服
class Solution:
def climbStairs(self, n):
a , b = 1 , 1
for i in range(n-1):
a , b = a + b , a
return a