标签:
假设有一个字符串aabcad,请编写一段程序,去掉字符串中不相邻的重复字符。即上述字串处理之后结果是为:aabcd;
分析,重点考查 char 与int 的隐式转换。程序如下:
-(void) removeRepeat:(NSString *)aNum
{
NSMutableArray *mArr = [[NSMutableArray alloc]initWithCapacity:10];
for(int i = 0; i<aNum.length; i++)
{
[mArr addObject:[aNum substringWithRange:NSMakeRange(i,1)]];
}
NSLog(@"%@",mArr);
[self compareNum:mArr];
NSLog(@"%@",mArr);
}
// 比较是否相等
-(NSMutableArray *)compareNum:(NSMutableArray *)mArr
{
int count = mArr.count; // 重新定义了count不会减1
for(int j = 0; j< count - 1 ;j++)
{
for(int i = j;i < count -1-1-1;i++)
{
NSLog(@"%@ %@",[mArr objectAtIndex:j],[mArr objectAtIndex:i + 2]);
NSString *a = [mArr objectAtIndex:j];
NSString *b = [mArr objectAtIndex:i+2];
if([a isEqualToString:b])
{
[mArr replaceObjectAtIndex:i + 2 withObject:@" "];
}
}
}
return mArr;
}
转:假设有一个字符串aabcad,请编写一段程序,去掉字符串中不相邻的重复字符。
标签:
原文地址:http://www.cnblogs.com/leidaxie/p/4649279.html