如题:如果字符串为  “0123”.

循环移位。

输出:“0123”,“1230”,“2301”,“3012”.

解法:

 1 NSString *str = @"0123";
 2         
 3         for (int i=0; i<str.length ; i++) {
 4                 if (i==0) {
 5                         NSLog(@"str=%@",str);
 6                 }else{
 7                         if (i+1==str.length) {
 8                                 NSLog(@"str=%@%@",[str substringWithRange:NSMakeRange(i,1)],[str substringWithRange:NSMakeRange(0,i)]);
 9                         }else{
10                                 
11                                 NSLog(@"i=%d",i);
12                                 NSLog(@"str=%@%@%@",[str substringWithRange:NSMakeRange(i,1)],[str substringWithRange:NSMakeRange(i+1,str.length-i-1)],[str substringWithRange:NSMakeRange(0,i)]);
13                                 
14                         }
15                         
16                 }
17                 

希望大家提出不同的意见。以后有空都会更新算法题目。和大家交流。

 

相关文章:

  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-02
  • 2021-09-05
  • 2021-08-12
  • 2021-11-07
  • 2022-12-23
相关资源
相似解决方案