递归_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/
/** * 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 }