#暴力法 classSolution: defsortedSquares(self, nums: List[int]) -> List[int]: returnsorted([x * x for x in nums])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#双指针 classSolution: defsortedSquares(self, nums: List[int]) -> List[int]: n = len(nums) res = [0] * n i, j = 0, n - 1 for k inrange(n - 1, -1, -1): p, q = nums[i] ** 2, nums[j] ** 2 if p > q: res[k] = p i += 1 else: res[k] = q j -= 1 k -= 1 return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#双指针 classSolution: defsortedSquares(self, nums: List[int]) -> List[int]: n = len(nums) res = [0] * n i, j, k = 0, n - 1, n - 1 while i <= j: p, q = nums[i] ** 2, nums[j] ** 2 if p > q: res[k] = p i += 1 else: res[k] = q j -= 1 k -= 1 return res
Go:
1 2 3 4 5 6 7 8
//暴力法 funcsortedSquares(nums []int) []int { for i := 0; i < len(nums); i++ { nums[i] = nums[i] * nums[i] } sort.Ints(nums) return nums }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//双指针 funcsortedSquares(nums []int) []int { n := len(nums) ans := make([]int, n) i, j := 0, n-1 for pos := n - 1; pos >= 0; pos-- { if v, w := nums[i]*nums[i], nums[j]*nums[j]; v > w { ans[pos] = v i++ } else { ans[pos] = w j-- } } return ans }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//双指针 funcsortedSquares(nums []int) []int { n := len(nums) i, j, k := 0, n-1, n-1 ans := make([]int, n) for i <= j { lm, rm := nums[i]*nums[i], nums[j]*nums[j] if lm > rm { ans[k] = lm i++ } else { ans[k] = rm j-- } k-- } return ans }
#滑动窗口 classSolution: defminSubArrayLen(self, target: int, nums: List[int]) -> int: ifnot nums orsum(nums) < target: return0 left = right = tmp = 0 l = len(nums) res = l + 1 while right < l: tmp += nums[right] while tmp >= target: res = min(res, right - left + 1) tmp -= nums[left] left += 1 right += 1 return res
funcminSubArrayLen(target int, nums []int)int { l := len(nums) left, right, tmp := 0, 0, 0 res := l + 1 for right < l { tmp += nums[right] for tmp >= target { if m := right - left + 1; m < res { res = m } tmp -= nums[left] left += 1 } right += 1 } if res == l+1 { return0 } return res }
classSolution: defspiralOrder(self, matrix: List[List[int]]) -> List[int]: dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] m, n = len(matrix), len(matrix[0]) res = [] row, col, d = 0, 0, 0 for i inrange(m * n): res.append(matrix[row][col]) matrix[row][col] = True dx, dy = dirs[d] r, c = row + dx, col + dy if r < 0or r >= m or c < 0or c >= n or matrix[r][c] == True: d = (d + 1) % 4 dx, dy = dirs[d] row, col = row + dx, col + dy return res
funcspiralOrder(matrix [][]int) []int { dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} m, n := len(matrix), len(matrix[0]) res := make([]int, 0, m*n) visited := make([][]bool, m) for i := 0; i < m; i++ { visited[i] = make([]bool, n) } row, col, dir := 0, 0, 0 for i := 0; i < m*n; i++ { res = append(res, matrix[row][col]) visited[row][col] = true r, c := row+dirs[dir][0], col+dirs[dir][1] if r < 0 || r >= m || c < 0 || c >= n || visited[r][c] { dir = (dir + 1) % 4 } row, col = row+dirs[dir][0], col+dirs[dir][1] } return res }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//由于题目明确说明矩阵元素大小不超过100,可将原矩阵访问过的元素置为101 funcspiralOrder(matrix [][]int) []int { dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} m, n := len(matrix), len(matrix[0]) res := make([]int, 0, m*n) row, col, dir := 0, 0, 0 for i := 0; i < m*n; i++ { res = append(res, matrix[row][col]) matrix[row][col] = 101 r, c := row+dirs[dir][0], col+dirs[dir][1] if r < 0 || r >= m || c < 0 || c >= n || matrix[r][c] == 101 { dir = (dir + 1) % 4 } row, col = row+dirs[dir][0], col+dirs[dir][1] } return res }
方法二:当 num <= tar 时,始终按照 从左到右 从上到下 从右到左 从下到上 填入顺序循环,每次填入后: 执行 num += 1:得到下一个需要填入的数字; 更新边界:例如从左到右填完后,上边界 t += 1,相当于上边界向内缩 1。
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defgenerateMatrix(self, n: int) -> List[List[int]]: dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] matrix = [[0] * n for _ inrange(n)] row, col, dirIdx = 0, 0, 0 for i inrange(n * n): matrix[row][col] = i + 1 dx, dy = dirs[dirIdx] r, c = row + dx, col + dy if r < 0or r >= n or c < 0or c >= n or matrix[r][c] > 0: dirIdx = (dirIdx + 1) % 4 dx, dy = dirs[dirIdx] row, col = row + dx, col + dy
classSolution: defgenerateMatrix(self, n: int) -> [[int]]: l, r, t, b = 0, n - 1, 0, n - 1 matrix = [[0] * n for _ inrange(n)] num, tar = 1, n * n while num <= tar: for i inrange(l, r + 1): matrix[t][i] = num num += 1 t += 1 for i inrange(t, b + 1): matrix[i][r] = num num += 1 r -= 1 for i inrange(r, l - 1, -1): matrix[b][i] = num num += 1 b -= 1 for i inrange(b, t - 1, -1): matrix[i][l] = num num += 1 l += 1 return matrix
Go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
funcgenerateMatrix(n int) [][]int { dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} matrix := make([][]int, n) for i := range matrix { matrix[i] = make([]int, n) } row, col, dir := 0, 0, 0 for i := 0; i < n*n; i++ { matrix[row][col] = i + 1 dx, dy := dirs[dir][0], dirs[dir][1] if r, c := row+dx, col+dy; r < 0 || r >= n || c < 0 || c >= n || matrix[r][c] > 0 { dir = (dir + 1) % 4 dx, dy = dirs[dir][0], dirs[dir][1] } row, col = row+dx, col+dy } return matrix }
funcgenerateMatrix(n int) [][]int { l, r, t, b := 0, n-1, 0, n-1 matrix := make([][]int, n) for i := 0; i < n; i++ { matrix[i] = make([]int, n) } num, tar := 1, n*n for num <= tar { for i := l; i <= r; i++ { matrix[t][i] = num num++ } t++ for i := t; i <= b; i++ { matrix[i][r] = num num++ } r-- for i := r; i >= l; i-- { matrix[b][i] = num num++ } b-- for i := b; i >= t; i-- { matrix[i][l] = num num++ } l++ } return matrix }