【发布时间】:2021-09-08 13:23:05
【问题描述】:
想先打印出重复的字符,然后再打印出下一个字符数。
喜欢从 ["a","a","b","c","c"] 到 ["a", "2","c","2"]。
但是,我没有正确地解决这个问题。希望您就我的代码给我反馈,或者让我知道您的智慧如何解决这个问题。
import Foundation
var m : [String] = ["a","a","b","c","c"]
var count : Int = 0
var result : [String] = []
for i in 0..<m.count{
for word in m {
if m[i] == word{
count += 1
result.append(word)
}else{
count = 0
}
if count > 1{
result.append(String(count))
}
}
}
print("\(count)")
print("\(result)")
//["a", "a", "2", "a", "a", "2", "b", "c", "c", "2", "c", "c", "2"]
print(["a", "2","c","2"])// want to print it on the console like this.
【问题讨论】: