Skip to content

Commit 5e1c45c

Browse files
committed
Add solution 0031、0987、1675
1 parent 0881edf commit 5e1c45c

35 files changed

+1205
-219
lines changed

README.md

Lines changed: 172 additions & 172 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode
2+
3+
func nextPermutation(nums []int) {
4+
i, j := 0, 0
5+
for i = len(nums) - 2; i >= 0; i-- {
6+
if nums[i] < nums[i+1] {
7+
break
8+
}
9+
}
10+
if i >= 0 {
11+
for j = len(nums) - 1; j > i; j-- {
12+
if nums[j] > nums[i] {
13+
break
14+
}
15+
}
16+
swap(&nums, i, j)
17+
}
18+
reverse(&nums, i+1, len(nums)-1)
19+
}
20+
21+
func reverse(nums *[]int, i, j int) {
22+
for i < j {
23+
swap(nums, i, j)
24+
i++
25+
j--
26+
}
27+
}
28+
29+
func swap(nums *[]int, i, j int) {
30+
(*nums)[i], (*nums)[j] = (*nums)[j], (*nums)[i]
31+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question31 struct {
9+
para31
10+
ans31
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para31 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans31 struct {
22+
one []int
23+
}
24+
25+
func Test_Problem31(t *testing.T) {
26+
27+
qs := []question31{
28+
29+
{
30+
para31{[]int{1, 2, 3}},
31+
ans31{[]int{1, 3, 2}},
32+
},
33+
34+
{
35+
para31{[]int{3, 2, 1}},
36+
ans31{[]int{1, 2, 3}},
37+
},
38+
39+
{
40+
para31{[]int{1, 1, 5}},
41+
ans31{[]int{1, 5, 1}},
42+
},
43+
44+
{
45+
para31{[]int{1}},
46+
ans31{[]int{1}},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 31------------------------\n")
51+
for _, q := range qs {
52+
_, p := q.ans31, q.para31
53+
fmt.Printf("【input】:%v ", p)
54+
nextPermutation(p.nums)
55+
fmt.Printf("【output】:%v\n", p.nums)
56+
}
57+
fmt.Printf("\n\n\n")
58+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [31. Next Permutation](https://leetcode.com/problems/next-permutation/)
2+
3+
4+
## 题目
5+
6+
Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers.
7+
8+
If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).
9+
10+
The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [1,2,3]
16+
Output: [1,3,2]
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: nums = [3,2,1]
23+
Output: [1,2,3]
24+
```
25+
26+
**Example 3:**
27+
28+
```
29+
Input: nums = [1,1,5]
30+
Output: [1,5,1]
31+
```
32+
33+
**Example 4:**
34+
35+
```
36+
Input: nums = [1]
37+
Output: [1]
38+
```
39+
40+
**Constraints:**
41+
42+
- `1 <= nums.length <= 100`
43+
- `0 <= nums[i] <= 100`
44+
45+
## 题目大意
46+
47+
实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。必须 原地 修改,只允许使用额外常数空间。
48+
49+
## 解题思路
50+
51+
- 题目有 3 个问题需要解决。如何找到下一个排列。不存在下一个排列的时候如何生成最小的排列。如何原地修改。先解决第一个问题,如何找到下一个排列。下一个排列是找到一个大于当前排序的字典序,且变大的幅度最小。那么只能将较小的数与较大数做一次原地交换。并且较小数的下标要尽量靠右,较大数也要尽可能小。原地交换以后,还需要将较大数右边的数按照升序重新排列。这样交换以后,才能生成下一个排列。以排列 [8,9,6,10,7,2] 为例:能找到的符合条件的一对「较小数」与「较大数」的组合为 6 与 7,满足「较小数」尽量靠右,而「较大数」尽可能小。当完成交换后排列变为 [8,9,7,10,6,2],此时我们可以重排「较小数」右边的序列,序列变为 [8,9,7,2,6,10]
52+
- 第一步:在 `nums[i]` 中找到 `i` 使得 `nums[i] < nums[i+1]`,此时较小数为 `nums[i]`,并且 `[i+1, n)` 一定为下降区间。第二步:如果找到了这样的 `i` ,则在下降区间 `[i+1, n)` 中从后往前找到第一个 `j` ,使得 `nums[i] < nums[j]` ,此时较大数为 `nums[j]`。第三步,交换 `nums[i]``nums[j]`,此时区间 `[i+1, n)` 一定为降序区间。最后原地交换 `[i+1, n)` 区间内的元素,使其变为升序,无需对该区间进行排序。
53+
- 如果第一步找不到符合条件的下标 `i`,说明当前序列已经是一个最大的排列。那么应该直接执行第三步,生成最小的排列。
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
func nextPermutation(nums []int) {
61+
i, j := 0, 0
62+
for i = len(nums) - 2; i >= 0; i-- {
63+
if nums[i] < nums[i+1] {
64+
break
65+
}
66+
}
67+
if i >= 0 {
68+
for j = len(nums) - 1; j > i; j-- {
69+
if nums[j] > nums[i] {
70+
break
71+
}
72+
}
73+
swap(&nums, i, j)
74+
}
75+
reverse(&nums, i+1, len(nums)-1)
76+
}
77+
78+
func reverse(nums *[]int, i, j int) {
79+
for i < j {
80+
swap(nums, i, j)
81+
i++
82+
j--
83+
}
84+
}
85+
86+
func swap(nums *[]int, i, j int) {
87+
(*nums)[i], (*nums)[j] = (*nums)[j], (*nums)[i]
88+
}
89+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package leetcode
2+
3+
import (
4+
"sort"
5+
6+
"github.com/halfrost/LeetCode-Go/structures"
7+
)
8+
9+
// TreeNode define
10+
type TreeNode = structures.TreeNode
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* type TreeNode struct {
15+
* Val int
16+
* Left *TreeNode
17+
* Right *TreeNode
18+
* }
19+
*/
20+
21+
type node struct {
22+
x, y, val int
23+
}
24+
25+
func verticalTraversal(root *TreeNode) [][]int {
26+
nodes := []*node{}
27+
inorder(root, 0, 0, &nodes)
28+
sort.Slice(nodes, func(i, j int) bool {
29+
if nodes[i].y == nodes[j].y {
30+
if nodes[i].x < nodes[j].x {
31+
return true
32+
} else if nodes[i].x > nodes[j].x {
33+
return false
34+
}
35+
return nodes[i].val < nodes[j].val
36+
}
37+
return nodes[i].y < nodes[j].y
38+
})
39+
res, currY, currColumn := [][]int{}, nodes[0].y, []int{nodes[0].val}
40+
for i := 1; i < len(nodes); i++ {
41+
if currY == nodes[i].y {
42+
currColumn = append(currColumn, nodes[i].val)
43+
} else {
44+
res = append(res, currColumn)
45+
currColumn = []int{nodes[i].val}
46+
currY = nodes[i].y
47+
}
48+
}
49+
res = append(res, currColumn)
50+
return res
51+
}
52+
53+
func inorder(root *TreeNode, x, y int, nodes *[]*node) {
54+
if root != nil {
55+
*nodes = append(*nodes, &node{x, y, root.Val})
56+
inorder(root.Left, x+1, y-1, nodes)
57+
inorder(root.Right, x+1, y+1, nodes)
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question987 struct {
11+
para987
12+
ans987
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para987 struct {
18+
one []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans987 struct {
24+
one [][]int
25+
}
26+
27+
func Test_Problem987(t *testing.T) {
28+
29+
qs := []question987{
30+
31+
{
32+
para987{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
33+
ans987{[][]int{{9}, {3, 15}, {20}, {7}}},
34+
},
35+
36+
{
37+
para987{[]int{1, 2, 3, 4, 5, 6, 7}},
38+
ans987{[][]int{{4}, {2}, {1, 5, 6}, {3}, {7}}},
39+
},
40+
41+
{
42+
para987{[]int{1, 2, 3, 4, 6, 5, 7}},
43+
ans987{[][]int{{4}, {2}, {1, 5, 6}, {3}, {7}}},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 987------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans987, q.para987
51+
fmt.Printf("【input】:%v ", p)
52+
root := structures.Ints2TreeNode(p.one)
53+
fmt.Printf("【output】:%v \n", verticalTraversal(root))
54+
}
55+
fmt.Printf("\n\n\n")
56+
}

0 commit comments

Comments
 (0)