189. 旋转数组

https://leetcode-cn.com/problems/rotate-array/
我自己能想到的就是python的slicing

  1. class Solution:
  2. def rotate(self, nums: List[int], k: int) -> None:
  3. """
  4. Do not return anything, modify nums in-place instead.
  5. """
  6. if len(nums) < 2:
  7. return nums
  8. k = k%len(nums)
  9. nums[k:],nums[:k] = nums[:-k],nums[-k:]
  10. return nums

这个题看到比较有趣的一个办法就是三次反转法
先把[0, n - k - 1]翻转
然后把[n - k, n - 1]翻转
最后把[0, n - 1]翻转
image.png

剑指 Offer 29. 顺时针打印矩阵

https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/
先来个普通的常规的思路

  1. class Solution:
  2. def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
  3. if not matrix:
  4. return []
  5. left,right,top,bottom = 0, len(matrix[0])-1, 0, len(matrix)-1
  6. res = []
  7. while True:
  8. for i in range(left,right+1):
  9. res.append(matrix[top][i])
  10. top+=1
  11. if top > bottom:break
  12. for i in range(top, bottom+1):
  13. res.append(matrix[i][right])
  14. right-=1
  15. if left > right:break
  16. for i in range(right,left-1,-1):
  17. res.append(matrix[bottom][i])
  18. bottom-=1
  19. if top > bottom:break
  20. for i in range(bottom,top-1,-1):
  21. res.append(matrix[i][left])
  22. left+=1
  23. if left > right:break
  24. return res

再来一个牛逼plus的思路:拿着矩阵每次就从左向右输出最上面的一行,然后逆时针翻转90度,这样下一排刚输出的元素就又到了矩阵的第一行,且顺序是从左向右

  1. class Solution:
  2. def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
  3. res = []
  4. while matrix:
  5. res += matrix.pop(0)
  6. matrix = list(zip(*matrix))[::-1]
  7. return res

剑指 Offer 59 - I. 滑动窗口的最大值

https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof/
image.png

215. 数组中的第K个最大元素

https://leetcode-cn.com/problems/kth-largest-element-in-an-array/

  1. import random
  2. class Solution:
  3. def findKthLargest(self, nums: List[int], k: int) -> int:
  4. def partition(nums,lo,hi):
  5. random_no = random.randint(lo,hi)
  6. nums[hi], nums[random_no] = nums[random_no], nums[hi]
  7. pivot = nums[hi]
  8. left = lo
  9. for i in range(lo,hi):
  10. if nums[i] < pivot:
  11. nums[i], nums[left] = nums[left], nums[i]
  12. left+=1
  13. nums[left], nums[hi] = nums[hi], nums[left]
  14. return left
  15. low = 0
  16. high = len(nums)-1
  17. k = len(nums)-k
  18. while True:
  19. j = partition(nums,low, high)
  20. if j<k:
  21. low=j+1
  22. elif j>k:
  23. high=j-1
  24. else:
  25. return nums[j]

692. 前K个高频单词

https://leetcode-cn.com/problems/top-k-frequent-words/
比215稍微复杂一下,除了比较数字,还需要比较对应的字符串的大小,注意用到cmp_to_key的功能

48. 旋转图像

https://leetcode-cn.com/problems/rotate-image/
可以先逐行翻转,再沿着斜对角翻转
也可以先沿对角线翻转,再逐行翻转 (这个方式代码写起来简单一些)

419. 甲板上的战舰

https://leetcode-cn.com/problems/battleships-in-a-board/
用战舰的头来判断数量

  1. 如果是第一行或者第一列为x, 那么计算一列战舰
  2. 如果在中间出现x,而这个点的左边或者上边是x,那么这肯定是战舰中部,这列战舰在遇到头部的时候已经计算过了,无需再重复计算
    1. class Solution:
    2. def countBattleships(self, board: List[List[str]]) -> int:
    3. count = 0
    4. for i in range(len(board)):
    5. for j in range(len(board[0])):
    6. if board[i][j] == '.': continue
    7. if i > 0 and board[i-1][j] == 'X': continue
    8. if j > 0 and board[i][j-1] == 'X': continue
    9. count+=1
    10. return count

    1314. 矩阵区域和

    https://leetcode-cn.com/problems/matrix-block-sum/
    所有的前缀和,都需要额外加个0前缀和,用来给第一个元素的前缀和用
    前缀和,注意包括和不包括的情况, 前缀和矩阵里的[rows,cols]是开区间,意思到[rows-1,cols-1]之间所有元素的和
    所以在计算answer的时候max_x需要+1,因为max_x的位置需要包括;而min_x的地方不+1
    1. class Solution:
    2. def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]:
    3. rows, cols = len(mat), len(mat[0])
    4. k = K
    5. answer = [[0 for _ in range(cols)] for _ in range(rows)]
    6. pre_sum = [[0 for _ in range(cols+1)] for _ in range(rows+1)]
    7. for i in range(1, rows+1):
    8. for j in range(1, cols+1):
    9. pre_sum[i][j] = pre_sum[i-1][j] + pre_sum[i][j-1] - pre_sum[i-1][j-1]+mat[i-1][j-1]
    10. for i in range(rows):
    11. for j in range(cols):
    12. min_rows = i-k if i-k>=0 else 0
    13. max_rows = i+k if i+k<rows else rows-1
    14. min_cols = j-k if j-k>=0 else 0
    15. max_cols = j+k if j+k<cols else cols-1
    16. answer[i][j] = pre_sum[max_rows+1][max_cols+1] - pre_sum[min_rows][max_cols+1] - pre_sum[max_rows+1][min_cols] + pre_sum[min_rows][min_cols]
    17. return answer