- [Codility] Lession 5.1 Passing c
- Codility 5.1 Passing Cars
- [Codility] Lession 3.1 TapeEquil
- [Codility] Lession 5.2 CountDiv
- [Codility] Lession 2.1 CyclicRot
- [Codility] Lession 2.2 OddOccurr
- [Codility] Lession 3.3 PermMissi
- [Codility] Lession 3.2 FrogJmp
- [Codility] Lession 4.1 FrogRiver
- [Codility] Lession 4.2 PermCheck
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
}
网友评论