Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import Glibc
- // you can write to stdout for debugging purposes, e.g.
- // print("this is a debug message")
- public func solution(N : Int) -> Int {
- // write your code in Swift 2.2 (Linux)
- let stre = String(N, radix: 2)
- let characters = stre.characters.map { String($0)}
- characters[0]
- var trck = false
- var startIdx = 0
- var maxGap = 0
- for i in 0..<characters.count{
- if characters[i] == "1" && trck == false {
- trck = true
- startIdx = i
- }else if (characters[i] == "1") && (trck == true){
- let tempGap = (i - startIdx - 1)
- //Compare each binary gap and compare to saved max gap
- maxGap = (tempGap > maxGap) ? tempGap : maxGap
- //Re tag the starting index for next search
- startIdx = i
- }
- }
- return maxGap
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement