递归_action
import (
"strconv"
"strings"
)
func restoreIpAddresses(s string) []string {
ans, n := []string{}, len(s)
current, currentCharLen := []string{}, 0
ok := func(start, end int) bool {
// 101023 1.01.
// 10101010 1.010.
sub := s[start : end+1]
i, _ := strconv.ParseInt(sub, 10, 0)
left, right := func() (int64, int64) {
l, r := -1, 10
switch len(sub) {
case 2:
l, r = 9, 100
case 3:
l, r = 99, 256
}
return int64(l), int64(r)
}()
return i > left && i < right
}
var backtrack func(start int)
backtrack = func(start int) {
for i := start; i < n; i++ {
for j := i; j < n; j++ {
if len(current) < 4 && ok(i, j) {
current = append(current, s[i:j+1])
currentCharLen += j - i + 1
if currentCharLen == n && len(current) == 4 {
ans = append(ans, strings.Join(current, "."))
}
backtrack(j + 1)
current = current[0 : len(current)-1]
currentCharLen -= j - i + 1
}
}
}
}
backtrack(0)
return ans
}
/*
Restore IP Addresses - LeetCode https://leetcode.com/problems/restore-ip-addresses/
Example 1:
Input: s = "25525511135"
Output: ["255.255.11.135","255.255.111.35"]
Example 2:
Input: s = "0000"
Output: ["0.0.0.0"]
Example 3:
Input: s = "101023"
Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
Constraints:
1 <= s.length <= 20
s consists of digits only.
*/
练习递归的好问题 - 分割回文串 - 力扣(LeetCode) https://leetcode.cn/problems/palindrome-partitioning/solution/-by-followingu-vp10/
package backtracking
func palindromePartitioning(s string) [][]string {
var isPalindrome = func(start, end int) bool {
for start < end {
if s[start] != s[end] {
return false
}
start++
end--
}
return true
}
n := len(s)
isPalindromeReuse := make([][]bool, n)
for i := 0; i < n; i++ {
isPalindromeReuse[i] = make([]bool, n)
for j := i; j < n; j++ {
isPalindromeReuse[i][j] = isPalindrome(i, j)
}
}
ans := [][]string{}
current, currentLen := []string{}, 0
var backtrack func(start int)
backtrack = func(start int) {
for i := start; i < n; i++ {
for j := i; j < n; j++ {
// if isPalindrome(i,j) {
if isPalindromeReuse[i][j] {
current = append(current, s[i:j+1])
currentLen += j - i + 1
if currentLen == n {
ans = append(ans, append([]string(nil), current...))
}
backtrack(j + 1)
current = current[0 : len(current)-1]
currentLen -= j - i + 1
}
}
}
}
backtrack(0)
return ans
}
/*
Palindrome Partitioning - LeetCode https://leetcode.com/problems/palindrome-partitioning/
Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Example 2:
Input: s = "a"
Output: [["a"]]
Constraints:
1 <= s.length <= 16
s contains only lowercase English letters.
*/
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func inorderTraversal(root *TreeNode) []int { if root == nil { return nil } l := []int{} l = append(l, inorderTraversal(root.Left)...) l = append(l, root.Val) l = append(l, inorderTraversal(root.Right)...) return l } func preorderTraversal(root *TreeNode) []int { if root == nil { return nil } l := []int{} l = append(l, root.Val) l = append(l, preorderTraversal(root.Left)...) l = append(l, preorderTraversal(root.Right)...) return l } func postorderTraversal(root *TreeNode) []int { if root == nil { return nil } l := []int{} l = append(l, postorderTraversal(root.Left)...) l = append(l, postorderTraversal(root.Right)...) l = append(l, root.Val) return l } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2021-04-21 delete xx; Local variable 'xx' may point to deallocated memory
2021-04-21 nothing to commit, working tree clean Remote "origin" does not support the LFS locking API. Consider disabling it with:
2021-04-21 A/B 测试 流量正交 流量互斥
2018-04-21 oss
2017-04-21 1*SUM(i) 开源社区
2017-04-21 $$name
2017-04-21 a