Created
October 30, 2017 16:18
-
-
Save atifazad/7cfe4e6b1150c56e5b542cdca8ec313e to your computer and use it in GitHub Desktop.
Objective C solution to https://leetcode.com/problems/count-and-say/description/
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
#import <Foundation/Foundation.h> | |
@interface Solution : NSObject | |
- (NSString *)countAndCall: (int) n; | |
- (NSString *)count:(int) n; | |
@end | |
@implementation Solution | |
- (NSString *)countAndCall: (int) n { | |
return [self count:n]; | |
} | |
- (NSString *)count:(int) n { | |
if(n<=1) return @"1"; | |
NSString *s=[self count:n-1]; | |
NSMutableString *res=[[NSMutableString alloc] initWithString:@""]; | |
int cnt=1; | |
for(int i=0; i<s.length; i++) { | |
char c1=[s characterAtIndex:i]; | |
if(i==s.length-1 || c1 != [s characterAtIndex:i+1]) { | |
[res appendString:[NSString stringWithFormat:@"%d",cnt]]; | |
[res appendString:[NSString stringWithFormat:@"%c",c1]]; | |
cnt=1; | |
} | |
else { | |
cnt++; | |
} | |
} | |
return res; | |
} | |
@end | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
int cont; | |
do { | |
int n; | |
printf("Number: "); | |
scanf("%d", &n); | |
printf("\n"); | |
Solution *sol=[[Solution alloc] init]; | |
printf("%s\n",[[sol countAndCall:n] UTF8String]); | |
scanf("%d",&cont); | |
} while (cont!=0); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment