Skip to content

Instantly share code, notes, and snippets.

@cobbspur
Created May 13, 2016 08:01
Show Gist options
  • Save cobbspur/ce1fe8ab9f7f86ddae73d0e451e4fdc5 to your computer and use it in GitHub Desktop.
Save cobbspur/ce1fe8ab9f7f86ddae73d0e451e4fdc5 to your computer and use it in GitHub Desktop.
```
- (NSUInteger *)binaryGap(NSInteger N) {
NSMutableString *str = [NSMutableString stringWithFormat:@""];
for (NSInteger NCopy = N; NCopy > 0; NCopy >>= 1) {
[str insertString:((NCopy & 1) ? @"1": @"0") atIndex:0];
}
unichar buffer[str.length];
[str getCharacters:buffer range:NSMakeRange(0, str.length)];
NSInteger count = -1;
NSInteger maxCount = 0;
for (NSUInteger i = 0; i < str.length; i++) {
char current = buffer[i];
if (current == '0' && count >= 0) {
count ++;
} else {
maxCount = MAX(maxCount, count);
count = 0;
}
}
return maxCount;
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment