Codility의 Lesson 1 Binary Gap 문제 풀이입니다.
하루 1 알고리즘을 목표로..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public func solution(_ N : Int) -> Int { var cnt: Int = 0 var maxCnt: Int = 0 var check: Bool = false let str = String(N, radix: 2) for i in 0..<str.characters.count { let index = str.index(str.startIndex, offsetBy: i) if(str[index] == "1") { if(cnt > maxCnt) { maxCnt = cnt } check = true cnt = 0 }else { if(check) { cnt += 1 } } } return maxCnt } | cs |
댓글