轉:假設有一個字串aabcad,請編寫一段程式,去掉字串中不相鄰的重複字元。

來源:互聯網
上載者:User

標籤:

假設有一個字串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,請編寫一段程式,去掉字串中不相鄰的重複字元。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.