简体   繁体   English

Swift:使用 for-in 循环,找到 Int 数组中的最大值

[英]Swift: Using a for-in loop, find the largest value in an Int array

Sorry guys, I'm new here and I'm learning iOS developing from scratch.对不起,我是新来的,我正在从头开始学习 iOS 开发。

I know that in order to find the largest value in an array of Int, we can use the propertie ".max()".我知道为了在 Int 数组中找到最大值,我们可以使用属性“.max()”。 But I need to do this using a for-in loop.但我需要使用 for-in 循环来做到这一点。 Would someone please help me?有人可以帮助我吗? I know it's so easy, but I can't find it and can't find out how to do it on my own as well.我知道这很容易,但我找不到它,也不知道如何自己做。 Thanks.谢谢。

Well the complexity of array.max() of Swift is O(N) just like the for-in loop. Swift 的 array.max() 的复杂度是 O(N),就像 for-in 循环一样。

There are two ways to for-in in Swift.在 Swift 中有两种 for-in 方法。

First solution (for-in here like for each value)第一个解决方案(这里的 for-in 就像每个值一样)

let arr = [1, 4, 3]
var max = Int.min

// get each value
for val in arr {
    if (max < val) {
        max = val
    }
}

Second solution (for-in here is for each index)第二种解决方案(这里的 for-in 是针对每个索引的)

let arr = [1, 4, 3]
var max = Int.min

// get each index
for i in 0..<arr.count {
    if (max < arr[i]) {
        max = arr[i]
    }
}

Two ways have the same output.两种方式具有相同的输出。 Feel free when choosing which to use in your further code.在选择在您的进一步代码中使用哪个时,请随意。

If your array is empty, then returning Int.min as the maximum value is not correct.如果您的数组为空,则返回Int.min作为最大值是不正确的。

Returning an optional is more correct:返回一个可选的更正确:

var max: Int? = nil

for val in arr {
    guard let maxSoFar = max else {
        max = val
        continue
    }

    if val > maxSoFar {
        max = val
    }
}

Though you might prefer to write it as an extension to Collection, like:尽管您可能更喜欢将其编写为 Collection 的扩展,例如:

extension Collection where Element: Comparable {
    func mMax() -> Element? {
        var max: Element? = nil

        // get each value
        for val in self {
            guard let maxSoFar = max else {
                max = val
                continue
            }

            if val > maxSoFar {
                max = val
            }
        }

        return max
    }

}

[1, 2, 3].mMax() // 3

([] as [Int]).mMax() // nil

["a", "c", "b"].mMax() // "c"

Or perhaps more generally, so it's not tied to the '>' function, like:或者更一般地说,它不依赖于 '>' 函数,例如:

extension Collection {
    func mMax(by compare: (Element, Element) -> Bool) -> Element? {
        var max: Element? = nil

        // get each value
        for val in self {
            guard let maxSoFar = max else {
                max = val
                continue
            }

            if compare(val, maxSoFar) {
                max = val
            }
        }

        return max
    }

}

[1, 2, 3].mMax(by: >) // 3

([] as [Int]).mMax(by: >) // nil


let longestString = ["aaa", "a", "aaaaa"].mMax(by: { $0.count > $1.count })  /// "aaaaa"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM