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
}
网友评论