迅速的康威序列[关闭]

问题描述 投票:-2回答:1

谁能帮我解决这个sequece.I已经尝试了多次,但没有得到解决。一些解决方案如下

1
11
21
1211
11121

但我需要以下解决方案。I want to print this sequece in console. Can anyone help me out with this sequece.

1 
11 
21 
1211 
3112 
132112 
311322
swift swift5 swift4.2
1个回答
1
投票
var last = [1, 3, 2, 1, 1, 2]
var next = [Int]()

func getNext() {
    if last.count == 0 { return }
    let first = last.first!
    let firstCount = last.filter{ $0 == first }.count
    next.append(firstCount)
    next.append(first)
    last.removeAll { $0 == first }
    getNext()
}

getNext()
print(next)//[3, 1, 1, 3, 2, 2]
© www.soinside.com 2019 - 2024. All rights reserved.