Skip to content

Commit 6300c17

Browse files
committed
添加 problems
1 parent 5345aa4 commit 6300c17

File tree

382 files changed

+17352
-77
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

382 files changed

+17352
-77
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode
2+
3+
var (
4+
letterMap = []string{
5+
" ", //0
6+
"", //1
7+
"abc", //2
8+
"def", //3
9+
"ghi", //4
10+
"jkl", //5
11+
"mno", //6
12+
"pqrs", //7
13+
"tuv", //8
14+
"wxyz", //9
15+
}
16+
res = []string{}
17+
)
18+
19+
func letterCombinations(digits string) []string {
20+
if digits == "" {
21+
return []string{}
22+
}
23+
res = []string{}
24+
findCombination(&digits, 0, "")
25+
return res
26+
}
27+
28+
func findCombination(digits *string, index int, s string) {
29+
if index == len(*digits) {
30+
res = append(res, s)
31+
return
32+
}
33+
num := (*digits)[index]
34+
letter := letterMap[num-'0']
35+
for i := 0; i < len(letter); i++ {
36+
findCombination(digits, index+1, s+string(letter[i]))
37+
}
38+
return
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question17 struct {
9+
para17
10+
ans17
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para17 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans17 struct {
22+
one []string
23+
}
24+
25+
func Test_Problem17(t *testing.T) {
26+
27+
qs := []question17{
28+
29+
question17{
30+
para17{"23"},
31+
ans17{[]string{"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"}},
32+
},
33+
}
34+
35+
fmt.Printf("------------------------Leetcode Problem 17------------------------\n")
36+
37+
for _, q := range qs {
38+
_, p := q.ans17, q.para17
39+
fmt.Printf("【input】:%v 【output】:%v\n", p, letterCombinations(p.s))
40+
}
41+
fmt.Printf("\n\n\n")
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)
2+
3+
4+
## 题目:
5+
6+
Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent.
7+
8+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
9+
10+
![](http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
11+
12+
**Example:**
13+
14+
```c
15+
Input: "23"
16+
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
17+
```
18+
19+
**Note:**
20+
21+
Although the above answer is in lexicographical order, your answer could be in any order you want.
22+
23+
## 题目大意
24+
25+
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
26+
27+
28+
## 解题思路
29+
30+
- DFS 递归深搜即可
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
func generateParenthesis(n int) []string {
4+
if n == 0 {
5+
return []string{}
6+
}
7+
res := []string{}
8+
findGenerateParenthesis(n, n, "", &res)
9+
return res
10+
}
11+
12+
func findGenerateParenthesis(lindex, rindex int, str string, res *[]string) {
13+
if lindex == 0 && rindex == 0 {
14+
*res = append(*res, str)
15+
return
16+
}
17+
if lindex > 0 {
18+
findGenerateParenthesis(lindex-1, rindex, str+"(", res)
19+
}
20+
if rindex > 0 && lindex < rindex {
21+
findGenerateParenthesis(lindex, rindex-1, str+")", res)
22+
}
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question22 struct {
9+
para22
10+
ans22
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para22 struct {
16+
one int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans22 struct {
22+
one []string
23+
}
24+
25+
func Test_Problem22(t *testing.T) {
26+
27+
qs := []question22{
28+
29+
question22{
30+
para22{3},
31+
ans22{[]string{
32+
"((()))",
33+
"(()())",
34+
"(())()",
35+
"()(())",
36+
"()()()",
37+
}},
38+
},
39+
// 如需多个测试,可以复制上方元素。
40+
}
41+
42+
fmt.Printf("------------------------Leetcode Problem 22------------------------\n")
43+
44+
for _, q := range qs {
45+
_, p := q.ans22, q.para22
46+
fmt.Printf("【input】:%v 【output】:%v\n", p, generateParenthesis(p.one))
47+
}
48+
fmt.Printf("\n\n\n")
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)
2+
3+
4+
## 题目
5+
6+
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
7+
8+
For example, given n = 3, a solution set is:
9+
10+
11+
[
12+
"((()))",
13+
"(()())",
14+
"(())()",
15+
"()(())",
16+
"()()()"
17+
]
18+
19+
20+
## 题目大意
21+
22+
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
23+
24+
25+
## 解题思路
26+
27+
- 这道题乍一看需要判断括号是否匹配的问题,如果真的判断了,那时间复杂度就到 O(n * 2^n)了,虽然也可以 AC,但是时间复杂度巨高。
28+
- 这道题实际上不需要判断括号是否匹配的问题。因为在 DFS 回溯的过程中,会让 `(``)` 成对的匹配上的。
29+
30+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package leetcode
2+
3+
import "strconv"
4+
5+
// 解法一 暴力遍历,时间复杂度 O(n^3)
6+
func isValidSudoku(board [][]byte) bool {
7+
// 判断行 row
8+
for i := 0; i < 9; i++ {
9+
tmp := [10]int{}
10+
for j := 0; j < 9; j++ {
11+
cellVal := board[i][j : j+1]
12+
if string(cellVal) != "." {
13+
index, _ := strconv.Atoi(string(cellVal))
14+
if index > 9 || index < 1 {
15+
return false
16+
}
17+
if tmp[index] == 1 {
18+
return false
19+
}
20+
tmp[index] = 1
21+
}
22+
}
23+
}
24+
// 判断列 column
25+
for i := 0; i < 9; i++ {
26+
tmp := [10]int{}
27+
for j := 0; j < 9; j++ {
28+
cellVal := board[j][i]
29+
if string(cellVal) != "." {
30+
index, _ := strconv.Atoi(string(cellVal))
31+
if index > 9 || index < 1 {
32+
return false
33+
}
34+
if tmp[index] == 1 {
35+
return false
36+
}
37+
tmp[index] = 1
38+
}
39+
}
40+
}
41+
// 判断 9宫格 3X3 cell
42+
for i := 0; i < 3; i++ {
43+
for j := 0; j < 3; j++ {
44+
tmp := [10]int{}
45+
for ii := i * 3; ii < i*3+3; ii++ {
46+
for jj := j * 3; jj < j*3+3; jj++ {
47+
cellVal := board[ii][jj]
48+
if string(cellVal) != "." {
49+
index, _ := strconv.Atoi(string(cellVal))
50+
if tmp[index] == 1 {
51+
return false
52+
}
53+
tmp[index] = 1
54+
}
55+
}
56+
}
57+
}
58+
}
59+
return true
60+
}
61+
62+
// 解法二 添加缓存,时间复杂度 O(n^2)
63+
func isValidSudoku_(board [][]byte) bool {
64+
rowbuf, colbuf, boxbuf := make([][]bool, 9), make([][]bool, 9), make([][]bool, 9)
65+
for i := 0; i < 9; i++ {
66+
rowbuf[i] = make([]bool, 9)
67+
colbuf[i] = make([]bool, 9)
68+
boxbuf[i] = make([]bool, 9)
69+
}
70+
// 遍历一次,添加缓存
71+
for r := 0; r < 9; r++ {
72+
for c := 0; c < 9; c++ {
73+
if board[r][c] != '.' {
74+
num := board[r][c] - '0' - byte(1)
75+
if rowbuf[r][num] || colbuf[c][num] || boxbuf[r/3*3+c/3][num] {
76+
return false
77+
}
78+
rowbuf[r][num] = true
79+
colbuf[c][num] = true
80+
boxbuf[r/3*3+c/3][num] = true // r,c 转换到box方格中
81+
}
82+
}
83+
}
84+
return true
85+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question36 struct {
9+
para36
10+
ans36
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para36 struct {
16+
s [][]byte
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans36 struct {
22+
s bool
23+
}
24+
25+
func Test_Problem36(t *testing.T) {
26+
27+
qs := []question36{
28+
29+
question36{
30+
para36{[][]byte{
31+
[]byte{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
32+
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
33+
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
34+
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
35+
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
36+
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
37+
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
38+
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
39+
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
40+
ans36{true},
41+
},
42+
43+
question36{
44+
para36{[][]byte{
45+
[]byte{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
46+
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
47+
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
48+
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
49+
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
50+
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
51+
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
52+
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
53+
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
54+
ans36{false},
55+
},
56+
57+
question36{
58+
para36{[][]byte{
59+
[]byte{'.', '8', '7', '6', '5', '4', '3', '2', '1'},
60+
[]byte{'2', '.', '.', '.', '.', '.', '.', '.', '.'},
61+
[]byte{'3', '.', '.', '.', '.', '.', '.', '.', '.'},
62+
[]byte{'4', '.', '.', '.', '.', '.', '.', '.', '.'},
63+
[]byte{'5', '.', '.', '.', '.', '.', '.', '.', '.'},
64+
[]byte{'6', '.', '.', '.', '.', '.', '.', '.', '.'},
65+
[]byte{'7', '.', '.', '.', '.', '.', '.', '.', '.'},
66+
[]byte{'8', '.', '.', '.', '.', '.', '.', '.', '.'},
67+
[]byte{'9', '.', '.', '.', '.', '.', '.', '.', '.'}}},
68+
ans36{true},
69+
},
70+
}
71+
72+
fmt.Printf("------------------------Leetcode Problem 36------------------------\n")
73+
74+
for _, q := range qs {
75+
_, p := q.ans36, q.para36
76+
fmt.Printf("【input】:%v 【output】:%v\n", p, isValidSudoku_(p.s))
77+
}
78+
fmt.Printf("\n\n\n")
79+
}

0 commit comments

Comments
 (0)