Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import Foundation
  2. import Glibc
  3.  
  4. // you can write to stdout for debugging purposes, e.g.
  5. // print("this is a debug message")
  6.  
  7. public func solution(N : Int) -> Int {
  8. // write your code in Swift 2.2 (Linux)
  9. let stre = String(N, radix: 2)
  10. let characters = stre.characters.map { String($0)}
  11. characters[0]
  12.  
  13. var trck = false
  14. var startIdx = 0
  15. var maxGap = 0
  16.  
  17. for i in 0..<characters.count{
  18. if characters[i] == "1" && trck == false {
  19. trck = true
  20. startIdx = i
  21. }else if (characters[i] == "1") && (trck == true){
  22.  
  23. let tempGap = (i - startIdx - 1)
  24. //Compare each binary gap and compare to saved max gap
  25. maxGap = (tempGap > maxGap) ? tempGap : maxGap
  26. //Re tag the starting index for next search
  27. startIdx = i
  28. }
  29.  
  30.  
  31. }
  32.  
  33. return maxGap
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement