【发布时间】:2020-09-25 05:47:06
【问题描述】:
问题陈述:
// m is the number, n is upto-length of subsequences
// m = 20125, n =3 should print 201, 202, 205, 212, 215, 225, 012, 015, 125
// m = 20125, n =2 should print 20, 21, 22, 25, 01, 02, 05, 12, 15, 25
// m = 20125, n =1 should print 2, 0, 1, 2, 5
// m = 20125, n =4 should print 2012, 2015, 2125, 0125, 2025
// m = 20125, n =5 should print 20125
以下是在 GoLang 中实现的递归解决方案:
package recursion
import (
"fmt"
"strconv"
)
// m is the number, n is upto-length of subsequences
// m = 20125, n =3 should print 201, 202, 205, 212, 215, 225, 012, 015, 125
// m = 20125, n =2 should print 20, 21, 22, 25, 01, 02, 05, 12, 15, 25
// m = 20125, n =1 should print 2, 0, 1, 2, 5
// m = 20125, n =4 should print 20125
func PrintSubSequence(m int, n int) {
numDigits := digits(m)
if n >= 1 && numDigits >= n { // m != 0
for i := 1; i <= numDigits-n+1; i++ { // tree recurion
firstInvocToIter := true
var slice []string
var findSubSequence func(int, int)
findSubSequence = func(m int, n int) {
if n == 1 { // base case
for m != 0 {
slice = append(slice, strconv.Itoa(m%10))
m = m / 10
}
return
} else {
if firstInvocToIter {
firstInvocToIter = false
findSubSequence(m/tenToThePower(i), n-1)
} else {
findSubSequence(m/10, n-1)
}
for i, value := range slice {
slice[i] = value + strconv.Itoa(m%10)
}
}
}
findSubSequence(m, n) // (20125, 3)
fmt.Println(slice)
}
} else {
return
}
PrintSubSequence(m/10, n)
}
func tenToThePower(times int) int {
number := 1
for times > 0 {
number *= 10
times--
}
return number
}
// Return the number of the digits of positive integer n
func digits(n int) int {
if n <= 0 {
return 0
} else if n < 10 {
return 1
} else {
allButLast, _ := split(n)
return digits(allButLast) + 1
}
}
package main
import (
"github.com/myhub/cs61a/recursion"
)
func main() {
recursion.PrintSubSequence(20125, 2) // prints duplicates as per debugging
recursion.PrintSubSequence(20125, 3) // Works fine
}
recursion.PrintSubSequence(20125, 3) 输出没问题:
[125 025 225]
[015 215]
[205]
[012 212]
[202]
对于recursion.PrintSubSequence(20125, 2) 输出有重复(问题输出):
[25 15 05 25] --> Valid
[15 05 25] --> duplicate
[05 25] --> duplicate
[25] --> duplicate
[12 02 22] --> Valid
[02 22] --> duplicate
[22] --> duplicate
[01 21] --> Valid
[21] --> duplicate
[20] --> Valid
这需要维护一组字符串吗?在集合中包含slice
或
如何处理重复?看起来 n==1 树递归的基本情况有问题?
【问题讨论】:
-
对于 20125 , 25 at 是 subsequence 的两倍,如果您想忽略重复删除重复数字并尝试生成子序列
-
@Eklavya 问题输出中的第一个条目有两次提及 25. 第二个条目呢?哪个是实际重复的...
-
这显然是重复的
-
@Eklavya 所以,我的问题是,如何避免上述代码中的重复项?
-
@Eklavya 我从来没有说过两次 25 是无效的。事实上,我并没有在我的查询中提到这一点。我显示了重复条目的箭头...我不知道,为什么您落后于那两次 25...请发布您对此问题的递归解决方案,将通过
标签: algorithm go recursion data-structures divide-and-conquer