Ios 打印出字符串的所有组合
iOS 11,Swift 4.0 尝试编写递归函数以显示字符串的所有可能组合。我有这个,但它不完全正确,因为我只有20双,我应该得到24双。我看不出我错过了什么 这个编码哪里出错了Ios 打印出字符串的所有组合,ios,swift,recursion,Ios,Swift,Recursion,iOS 11,Swift 4.0 尝试编写递归函数以显示字符串的所有可能组合。我有这个,但它不完全正确,因为我只有20双,我应该得到24双。我看不出我错过了什么 这个编码哪里出错了 var ans:Set<String>! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib.
var ans:Set<String>!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let str = "ABCD"
ans = []
recursiveString(s2s: str, char2s: 0)
print("\(ans) \(ans.count)")
}
func recursiveSwap(s2x: String, c2x: Int, j2m: Int) {
var anschr = Array(s2x)
let tmpchr = anschr[c2x]
anschr[c2x] = anschr[c2x+j2m]
anschr[c2x+j2m] = tmpchr
print("\(String(anschr))")
ans.insert(String(anschr))
if (c2x + j2m + 1) < s2x.count {
recursiveSwap(s2x: String(s2x), c2x: c2x, j2m: j2m+1)
} else {
if (c2x + 1) < s2x.count - 1 {
recursiveSwap(s2x: String(anschr), c2x: c2x + 1, j2m: 1)
}
}
}
func recursiveString(s2s: String, char2s: Int) {
let blue = shiftString(s2s: s2s)
if char2s < s2s.count {
recursiveSwap(s2x: blue, c2x: 0, j2m: 1)
recursiveString(s2s: blue, char2s: char2s + 1)
}
}
func shiftString(s2s: String) -> String {
let str2s = Array(s2s)
let newS = str2s.suffix(str2s.count - 1) + str2s.prefix(1)
return String(newS)
}
var-ans:Set!
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
让str=“ABCD”
ans=[]
递归字符串(s2s:str,char2s:0)
打印(“\(ans)\(ans.count)”)
}
func递归交换(s2x:String、c2x:Int、j2m:Int){
var anschr=数组(s2x)
设tmpchr=anschr[c2x]
anschr[c2x]=anschr[c2x+j2m]
anschr[c2x+j2m]=tmpchr
打印(“\(字符串(anschr))”)
ans.insert(字符串(anschr))
如果(c2x+j2m+1)String{
设str2s=数组(s2s)
let newS=str2s.suffix(str2s.count-1)+str2s.prefix(1)
返回字符串(新闻)
}
它给我
CBDA
DCBA
ACDB
ADCB
ABDC
ABCD
DCAB
ADCB
BDAC
BADC
BCAD
BCDA
亚洲开发银行
BADC
出租车
CBAD
CDBA
CDAB
BACD
CBAD
DBCA
DCBA
DACB
DABC因为您说过:
试图编写一个递归函数来显示字符串的所有可能组合
我想你可以这样做:
// Takes any collection of T and returns an array of permutations
func permute<C: Collection>(items: C) -> [[C.Iterator.Element]] {
var scratch = Array(items) // This is a scratch space for Heap's algorithm
var result: [[C.Iterator.Element]] = [] // This will accumulate our result
// Heap's algorithm
func heap(_ n: Int) {
if n == 1 {
result.append(scratch)
return
}
for i in 0..<n-1 {
heap(n-1)
let j = (n%2 == 1) ? 0 : i
scratch.swapAt(j, n-1)
}
heap(n-1)
}
// Let's get started
heap(scratch.count)
// And return the result we built up
return result
}
// We could make an overload for permute() that handles strings if we wanted
// But it's often good to be very explicit with strings, and make it clear
// that we're permuting Characters rather than something else.
let string = "ABCD"
let perms = permute(string.characters) // Get the character permutations
let permStrings = perms.map() { String($0) } // Turn them back into strings
print(permStrings) // output if you like
//获取T的任何集合并返回一个排列数组
func排列(项:C)->[[C.Iterator.Element]]{
var scratch=Array(items)//这是堆算法的暂存空间
var result:[[C.Iterator.Element]=[]//这将累积我们的结果
//希普算法
func堆(n:Int){
如果n==1{
结果.追加(暂存)
返回
}
对于0中的i..不是对您问题的直接回答,但您可以按如下方式获取所有信息(从java翻译为Swift):
public extension RangeReplaceableCollection {
func permutations() -> [SubSequence] {
isEmpty ? [] : permutate(.init())
}
private func permutate(_ subSequence: SubSequence) -> [SubSequence] {
var permutations = isEmpty ? [subSequence] : []
indices.forEach {
permutations += (self[..<$0] + self[$0...].dropFirst())
.permutate(subSequence + CollectionOfOne(self[$0]))
}
return permutations
}
}
每个子串的变异
print("ABCD".dropLast().permutations()) // ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]\n"
请看:我想知道为什么有人投票以离题结束,因为“寻求调试帮助的问题……必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。”–这个问题中的所有内容。重复的?将来,你可能想看看Github上的Swift算法俱乐部。有很多流行的算法,有解释和代码。是的,你的权利。这是一个重复的,我以前在搜索时没有找到。虽然我喜欢Leo的答案,但这是一个简单明了的piec确实是代码的e。也要感谢Adrian,我也会看看ray algorithm club。Gabox,谢谢你回答这个问题!!我也会勾选你的答案,但这迫使我选择一个。它们都是我设法找到的更好的代码片段…这并不相似,但都是代码的逐字副本。
print("ABCD".dropLast().permutations()) // ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]\n"