立个flag,1-100题每天分配10题,不会就先空着(5,8)。
1. 61:旋转链表
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head:
return head
num = 0
head1 = head
while head1:
num = num + 1
head1 = head1.next
k = k % num
if k == 0:
return head
slow = fast = ListNode()
slow.next = head
while k:
fast = fast.next
k = k - 1
while fast.next:
slow = slow.next
fast = fast.next
new_head = slow.next
slow.next = None
fast.next = head
return new_head
2. 62:不同路径
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = []
for i in range(m):
dp.append([0] * n)
for i in range(m):
dp[i][0] = 1
for j in range(n):
dp[0][j] = 1
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]
3. 63:不同路径2
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
if obstacleGrid[0][0] == 1:
return 0
dp = []
m = len(obstacleGrid)
n = len(obstacleGrid[0])
for i in range(m):
dp.append([0] * n)
dp[0][0] = 1
for i in range(1, m):
if obstacleGrid[i][0] != 1 and dp[i - 1][0] == 1:
dp[i][0] = 1
for j in range(1, n):
if obstacleGrid[0][j] != 1 and dp[0][j - 1] == 1:
dp[0][j] = 1
for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] != 1:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
return dp[-1][-1]
4. 64:最小路径和
class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
dp = []
m = len(grid)
n = len(grid[0])
for i in range(m):
dp.append([0] * n)
dp[0][0] = grid[0][0]
for i in range(1, m):
dp[i][0] = dp[i - 1][0] + grid[i][0]
for j in range(1, n):
dp[0][j] = dp[0][j - 1] + grid[0][j]
for i in range(1, m):
for j in range(1, n):
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
return dp[-1][-1]
5. 65:有效数字
6. 66:加一
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
add = 1
for i in range(len(digits) - 1, -1, -1):
if digits[i] != 9:
digits[i] = digits[i] + add
add = 0
break
else:
digits[i] = 0
if add:
digits.insert(0, 1)
return digits
7. 67:二进制求和
class Solution:
def addBinary(self, a: str, b: str) -> str:
res = []
pointer1 = len(a) - 1
pointer2 = len(b) - 1
add = 0
while pointer1 >= 0 and pointer2 >= 0:
sum = int(a[pointer1]) + int(b[pointer2]) + add
res.append(str(sum % 2))
add = sum // 2
pointer1 = pointer1 - 1
pointer2 = pointer2 - 1
while pointer1 >= 0:
sum = int(a[pointer1]) + add
res.append(str(sum % 2))
add = sum // 2
pointer1 = pointer1 - 1
while pointer2 >= 0:
sum = int(b[pointer2]) + add
res.append(str(sum % 2))
add = sum // 2
pointer2 = pointer2 - 1
if add:
res.append(str(add))
return "".join(res[::-1])
8. 68:文本左右对齐
9. 69:x的平方根
class Solution:
def mySqrt(self, x: int) -> int:
left = 0
right = x
while left <= right:
mid = left + (right - left) // 2
if mid * mid == x:
return mid
elif mid * mid < x:
left = mid + 1
elif mid * mid > x:
right = mid - 1
return right
10. 70:爬楼梯
class Solution:
def climbStairs(self, n: int) -> int:
dp = [0] * n
for i in range(n):
if i == 0:
dp[i] = 1
elif i == 1:
dp[i] = 2
else:
dp[i] = dp[i-1] + dp[i-2]
return dp[-1]