mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-24 10:37:33 +08:00
update solution 0031 & 0048
This commit is contained in:
@ -57,6 +57,7 @@ Output: [1]
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
// 解法一
|
||||
func nextPermutation(nums []int) {
|
||||
i, j := 0, 0
|
||||
for i = len(nums) - 2; i >= 0; i-- {
|
||||
@ -86,6 +87,47 @@ func reverse(nums *[]int, i, j int) {
|
||||
func swap(nums *[]int, i, j int) {
|
||||
(*nums)[i], (*nums)[j] = (*nums)[j], (*nums)[i]
|
||||
}
|
||||
|
||||
// 解法二
|
||||
// [2,(3),6,5,4,1] -> 2,(4),6,5,(3),1 -> 2,4, 1,3,5,6
|
||||
func nextPermutation1(nums []int) {
|
||||
var n = len(nums)
|
||||
var pIdx = checkPermutationPossibility(nums)
|
||||
if pIdx == -1 {
|
||||
reverse(&nums, 0, n-1)
|
||||
return
|
||||
}
|
||||
|
||||
var rp = len(nums) - 1
|
||||
// start from right most to leftward,find the first number which is larger than PIVOT
|
||||
for rp > 0 {
|
||||
if nums[rp] > nums[pIdx] {
|
||||
swap(&nums, pIdx, rp)
|
||||
break
|
||||
} else {
|
||||
rp--
|
||||
}
|
||||
}
|
||||
// Finally, Reverse all elements which are right from pivot
|
||||
reverse(&nums, pIdx+1, n-1)
|
||||
}
|
||||
|
||||
// checkPermutationPossibility returns 1st occurrence Index where
|
||||
// value is in decreasing order(from right to left)
|
||||
// returns -1 if not found(it's already in its last permutation)
|
||||
func checkPermutationPossibility(nums []int) (idx int) {
|
||||
// search right to left for 1st number(from right) that is not in increasing order
|
||||
var rp = len(nums) - 1
|
||||
for rp > 0 {
|
||||
if nums[rp-1] < nums[rp] {
|
||||
idx = rp - 1
|
||||
return idx
|
||||
}
|
||||
rp--
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
@ -99,6 +99,7 @@ You have to rotate the image **[in-place](https://en.wikipedia.org/wiki/In-plac
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
// 解法一
|
||||
func rotate(matrix [][]int) {
|
||||
length := len(matrix)
|
||||
// rotate by diagonal 对角线变换
|
||||
@ -114,6 +115,50 @@ func rotate(matrix [][]int) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 解法二
|
||||
func rotate1(matrix [][]int) {
|
||||
n := len(matrix)
|
||||
if n == 1 {
|
||||
return
|
||||
}
|
||||
/* rotate clock-wise = 1. transpose matrix => 2. reverse(matrix[i])
|
||||
|
||||
1 2 3 4 1 5 9 13 13 9 5 1
|
||||
5 6 7 8 => 2 6 10 14 => 14 10 6 2
|
||||
9 10 11 12 3 7 11 15 15 11 7 3
|
||||
13 14 15 16 4 8 12 16 16 12 8 4
|
||||
|
||||
*/
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
// transpose, i=rows, j=columns
|
||||
// j = i+1, coz diagonal elements didn't change in a square matrix
|
||||
for j := i + 1; j < n; j++ {
|
||||
swap(matrix, i, j)
|
||||
}
|
||||
// reverse each row of the image
|
||||
matrix[i] = reverse(matrix[i])
|
||||
}
|
||||
}
|
||||
|
||||
// swap changes original slice's i,j position
|
||||
func swap(nums [][]int, i, j int) {
|
||||
nums[i][j], nums[j][i] = nums[j][i], nums[i][j]
|
||||
}
|
||||
|
||||
// reverses a row of image, matrix[i]
|
||||
func reverse(nums []int) []int {
|
||||
var lp, rp = 0, len(nums) - 1
|
||||
|
||||
for lp < rp {
|
||||
nums[lp], nums[rp] = nums[rp], nums[lp]
|
||||
lp++
|
||||
rp--
|
||||
}
|
||||
return nums
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user