From a01ef32bf76e76a45575f233422c34593497ff51 Mon Sep 17 00:00:00 2001 From: nite Date: Fri, 3 Jul 2026 10:27:25 +0800 Subject: [PATCH] u --- top-100-liked/58/leetcode_test.go | 53 ++++++++++++++++++ top-100-liked/59/leetcode_test.go | 48 ++++++++++++++++ top-100-liked/60/leetcode_test.go | 92 +++++++++++++++++++++++++++++++ top-100-liked/61/leetcode_test.go | 54 ++++++++++++++++++ 4 files changed, 247 insertions(+) create mode 100644 top-100-liked/58/leetcode_test.go create mode 100644 top-100-liked/59/leetcode_test.go create mode 100644 top-100-liked/60/leetcode_test.go create mode 100644 top-100-liked/61/leetcode_test.go diff --git a/top-100-liked/58/leetcode_test.go b/top-100-liked/58/leetcode_test.go new file mode 100644 index 0000000..ee32266 --- /dev/null +++ b/top-100-liked/58/leetcode_test.go @@ -0,0 +1,53 @@ +package top100liked + +import ( + "fmt" + "testing" +) + +// https://leetcode.cn/problems/combination-sum/?envType=study-plan-v2&envId=top-100-liked + +func combinationSum(candidates []int, target int) [][]int { + res := [][]int{} + sum := 0 + r := []int{} + + var helper func(strat int) + helper = func(start int) { + for i := start; i < len(candidates); i++ { + v := candidates[i] + if sum+v == target { + cp := make([]int, len(r)) + copy(cp, r) + cp = append(cp, v) + res = append(res, cp) + continue + } else if sum+v < target { + r = append(r, v) + sum += v + helper(i) + sum -= v + r = r[:len(r)-1] + } + } + } + + helper(0) + return res +} + +func Test(t *testing.T) { + fmt.Printf("%+v\n", combinationSum([]int{2, 3, 6, 7}, 7)) +} + +func Test2(t *testing.T) { + fmt.Printf("%+v\n", combinationSum([]int{2, 3, 5}, 8)) +} + +func Test3(t *testing.T) { + fmt.Printf("%+v\n", combinationSum([]int{2}, 1)) +} + +func Test4(t *testing.T) { + fmt.Printf("%+v\n", combinationSum([]int{4, 2, 8}, 8)) +} diff --git a/top-100-liked/59/leetcode_test.go b/top-100-liked/59/leetcode_test.go new file mode 100644 index 0000000..8d528be --- /dev/null +++ b/top-100-liked/59/leetcode_test.go @@ -0,0 +1,48 @@ +package top100liked + +import ( + "fmt" + "testing" +) + +// https://leetcode.cn/problems/generate-parentheses/description/?envType=study-plan-v2&envId=top-100-liked + +func generateParenthesis(n int) []string { + res := []string{} + r := []byte{} + num_l := 0 + num_r := 0 + + var helper func() + helper = func() { + if num_l < n { + r = append(r, '(') + num_l++ + helper() + num_l-- + r = r[:len(r)-1] + } + if num_l > num_r { + r = append(r, ')') + num_r++ + helper() + num_r-- + r = r[:len(r)-1] + } + if num_l == n && num_r == n { + res = append(res, string(r)) + } + } + + helper() + + return res +} + +func Test(t *testing.T) { + fmt.Printf("%+v\n", generateParenthesis(3)) +} + +func Test2(t *testing.T) { + fmt.Printf("%+v\n", generateParenthesis(1)) +} diff --git a/top-100-liked/60/leetcode_test.go b/top-100-liked/60/leetcode_test.go new file mode 100644 index 0000000..dbf1dd9 --- /dev/null +++ b/top-100-liked/60/leetcode_test.go @@ -0,0 +1,92 @@ +package top100liked + +import ( + "fmt" + "slices" + "testing" +) + +// https://leetcode.cn/problems/word-search/description/?envType=study-plan-v2&envId=top-100-liked + +func exist(board [][]byte, word string) bool { + w := []byte(word) + l := len(word) + m := len(board) + n := len(board[0]) + // 下、右、上、左 + move := [4][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}} + + // 减枝让耗时降低了很多 + + var boardCnt [128]int // 覆盖 ASCII,字母在范围内 + for _, row := range board { + for _, c := range row { + boardCnt[c]++ + } + } + + var wordCnt [128]int + for _, c := range w { + wordCnt[c]++ + } + + // 字符频次可行性预检。 扫一遍 board 统计各字符计数,若 word 中某字符出现次数超过 board 上的数量,直接 return false。 + for _, char := range w { + if boardCnt[char] < wordCnt[char] { + return false + } + } + + // 词序翻转剪枝(算法层面最大收益)。 统计 board 中 word[0] 和 word[l-1] 的出现次数,若首字符比尾字符更常见,就把 word 反转后再搜——从稀有端开始DFS,能在更浅层就剪掉大量无效分支。 + if boardCnt[0] > boardCnt[len(boardCnt)-1] { + slices.Reverse(w) + } + + var helper func(idx, row, col int) bool + helper = func(idx, row, col int) bool { + for _, v := range move { + new_row := row + v[0] + new_col := col + v[1] + if new_row >= 0 && new_row < m && new_col >= 0 && new_col < n && w[idx] == board[new_row][new_col] { + if idx == l-1 { + return true + } + board[new_row][new_col] = '#' + if helper(idx+1, new_row, new_col) { + return true + } + board[new_row][new_col] = w[idx] + } + } + return false + } + + for i, row := range board { + for j, col := range row { + if col == w[0] { + if l == 1 { + return true + } + board[i][j] = '#' + if helper(1, i, j) { + return true + } + board[i][j] = w[0] + } + } + } + + return false +} + +func Test(t *testing.T) { + fmt.Printf("%+v\n", exist([][]byte{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}, "ABCCED")) +} + +func Test2(t *testing.T) { + fmt.Printf("%+v\n", exist([][]byte{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}, "SEE")) +} + +func Test3(t *testing.T) { + fmt.Printf("%+v\n", exist([][]byte{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}, "ABCB")) +} diff --git a/top-100-liked/61/leetcode_test.go b/top-100-liked/61/leetcode_test.go new file mode 100644 index 0000000..17f6140 --- /dev/null +++ b/top-100-liked/61/leetcode_test.go @@ -0,0 +1,54 @@ +package top100liked + +import ( + "fmt" + "testing" +) + +// https://leetcode.cn/problems/palindrome-partitioning/description/?envType=study-plan-v2&envId=top-100-liked + +func partition(s string) [][]string { + res := [][]string{} + path := []string{} + // 动态规划,降低查找回文串的时间复杂度 + dp := make([][]bool, len(s)) + for i := range len(s) { + dp[i] = make([]bool, len(s)) + dp[i][i] = true + } + for length := 2; length <= len(s); length++ { + for i := 0; i+length <= len(s); i++ { + j := i + length - 1 + dp[i][j] = (s[i] == s[j]) && (length <= 2 || dp[i+1][j-1]) + } + } + + var helper func(start int) + helper = func(start int) { + if start == len(s) { + cp := make([]string, len(path)) + copy(cp, path) + res = append(res, cp) + return + } + for end := start; end < len(s); end++ { + if dp[start][end] { + path = append(path, s[start:end+1]) + helper(end + 1) + path = path[:len(path)-1] + } + } + } + + helper(0) + + return res +} + +func Test(t *testing.T) { + fmt.Printf("%+v\n", partition("aab")) +} + +func Test2(t *testing.T) { + fmt.Printf("%+v\n", partition("a")) +}