Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. // you can also use imports, for example:
  2. // import java.util.*;
  3.  
  4. // you can write to stdout for debugging purposes, e.g.
  5. // System.out.println("this is a debug message");
  6.  
  7. class Solution {
  8.     public int solution(int N) {
  9.         String binary = Integer.toBinaryString(N);
  10.  
  11.         int maxgap = 0;
  12.         boolean start = false;
  13.         int gap = 0;
  14.         for(char h : binary.toCharArray()) {
  15.             if (h == '1') {
  16.                 if(!start) {
  17.                     start = true;
  18.                    
  19.                 }
  20.                 else if(gap > 0) {
  21.                     if(gap > maxgap) maxgap = gap;
  22.                     gap = 0;
  23.                 }
  24.                 continue;
  25.             }
  26.            
  27.             if(start)
  28.             {
  29.                 gap++;
  30.             }
  31.  
  32.         }
  33.        
  34.         return maxgap;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement