我编写了以下方法,并尝试调用和运行该方法,但是我在geeting不能将不变值作为inout错误传递。
func compress(_ chars: inout [Character]) -> Int {
var index = 0, currentCount = 0
for i in 0..<chars.count
{
currentCount += 1
if i+1 == chars.count || chars[i] != chars[i+1]
{
chars[index] = chars[i]
if currentCount != 1
{
chars.replaceSubrange(index + 1...String(currentCount).count + index, with : Array(String(currentCount)))
}
index += currentCount == 1 ? 1 : 1 + String(currentCount).count
currentCount = 0
}
}
return index
}
print(compress(["a","a","b","b","c","c","c"]))
不能将不可变值作为[Character]类型的inout值传递 争论迅速