gpt4 book ai didi

arrays - Swift3 错误 : Binary operator '<' cannot be applied to two 'ArraySlice' operands

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:30 25 4
gpt4 key购买 nike

我正在迭代数组并通过索引比较值但出现错误

enter image description here

这是我的代码

class Solution {
func threeSum(_ alice: [Int] = [5,6,7], _ bob: [Int] = [3,6,10]) {

var aliceP = 0
var bobP = 0

for i in [0..<alice.count] {
if alice[i] > bob[i] {
aliceP += 1
} else if alice[i] < bob[i] {
bobP += 1
}
}


print(aliceP, bobP)
}
}

最佳答案

您需要更改 [0..<alice.count]只是0..<alice.count .没有括号,i将根据需要作为索引,在给定范围内迭代。使用方括号,您可以创建一个包含单个 CountableRange 的数组,因此唯一的值是 i。是 CountableRange<Int>而不是所需的简单 Int .

关于arrays - Swift3 错误 : Binary operator '<' cannot be applied to two 'ArraySlice<Int>' operands,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48751649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com