美文网首页Codility 解决方案 [Swift 3.0]
[Codility] Lession 5.1 Passing c

[Codility] Lession 5.1 Passing c

作者: sunlitamo | 来源:发表于2016-07-25 18:42 被阅读48次

    Test URL can be found in extension link
    Swift solution:

    public func solution (inout A: [Int]) -> Int {
        var multiply = 0
        var passings = 0
        for car in A {
            // If the car is 0, means the car is driving EAST
            if (car == 0) { multiply += 1 }
           // Ignore those cars (to WEST) before you find the first car (to EAST)
            if (multiply > 0) {
                
           // If you already found some car (to EAST)
           // and if the current car is the car (to WEST)
           // then this car will pass them eventually.
                if car == 1 {
                    passings += multiply
                
    // If passing count more than certain number,shall return -1 
    // as the question expected
                    if passings > 1000000000{ return -1 }
                }
            }
        }
        return passings
    }
    

    相关文章

      网友评论

        本文标题:[Codility] Lession 5.1 Passing c

        本文链接:https://www.haomeiwen.com/subject/bytnjttx.html