Coding Practice (04/08)

Date: 2021/4/8

Source: Leetcode No.45 跳跃游戏 II

Language: Python 3

Coding:

 1 class Solution:
 2     def jump(self, nums: List[int]) -> int:
 3         left, right = 0, len(nums)-1
 4         step = 0
 5         while right > left:
 6             for i in range(right-1, left-1, -1):
 7                 if right - i <= nums[i]:
 8                     temp = i
 9             right = temp
10             step += 1
11         return step

 

Date: 2021/4/8

Source: Leetcode No.48 旋转图像

Language: Python 3

Coding:

 1 class Solution:
 2     def rotate(self, matrix: List[List[int]]) -> None:
 3         """
 4         Do not return anything, modify matrix in-place instead.
 5         """
 6         n = len(matrix)
 7         for i in range(n//2):
 8             temp = matrix[i]
 9             matrix[i] = matrix[n-1-i]
10             matrix[n-1-i]= temp
11         for i in range(n):
12             for j in range(i,n):
13                 temp = matrix[i][j]
14                 matrix[i][j] = matrix[j][i]
15                 matrix[j][i] = temp

 

 

Date: 2021/4/8

Source: Leetcode No.153 寻找旋转排序数组中的最小值

Language: Python 3

Coding:

 1 class Solution:
 2     def findMin(self, nums: List[int]) -> int:
 3         n = len(nums)
 4         low, high = 0, n-1
 5         while low < high:
 6             mid = (low + high) // 2 
 7             if nums[mid] > nums[high]:
 8                 low = mid + 1
 9             else:
10                 high = mid
11         return nums[low]

 

上一篇:第三


下一篇:API 文档简洁之美,只需三步开启