Created
May 13, 2016 08:01
-
-
Save cobbspur/ce1fe8ab9f7f86ddae73d0e451e4fdc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
- (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