1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| //字符串逆序 NSString *reverseStr(NSString *str){ NSMutableString *string=[[NSMutableString alloc] init]; for(int i=0;i<str.length;i++){ [string appendString:[str substringWithRange:NSMakeRange(str.length-1-i, 1)]]; } return string; }
//字符串分成数组 NSMutableArray *separateStringToArray(NSString *str){ NSMutableArray *marr = [NSMutableArray array]; for (NSInteger i = 0; i < str.length; i++) { [marr addObject:[str substringWithRange:NSMakeRange(i, 1)]]; } return marr; }
NSString *findNextLargerNum(NSString *oriNum){
NSMutableString *finalStr = [NSMutableString string];
NSMutableArray *marr = separateStringToArray(oriNum); for (NSInteger i = marr.count - 1; i > 0; i--) { NSNumber *lastNum = [marr objectAtIndex:i]; NSNumber *preNum = [marr objectAtIndex:i-1]; if (lastNum.integerValue > preNum.integerValue) { //截取字符串 NSString *rightStr = reverseStr([oriNum substringWithRange:NSMakeRange(i, oriNum.length - i)]); NSString *leftStr = [oriNum substringWithRange:NSMakeRange(0,i)]; //第i-1位截取出来 NSString *leftCompareStr = [oriNum substringWithRange:NSMakeRange(i-1, 1)]; //和右边逐个对比 for (int j = 0; j < rightStr.length; j++) { NSString *rightCompareStr = [rightStr substringWithRange:NSMakeRange(j, 1)]; if (leftCompareStr.integerValue < rightCompareStr.integerValue) { NSMutableArray *finalArr = separateStringToArray([NSString stringWithFormat:@"%@%@",leftStr,rightStr]); NSInteger leftIndex = [finalArr indexOfObject:leftCompareStr]; NSInteger rightIndex = [finalArr indexOfObject:rightCompareStr]; [finalArr exchangeObjectAtIndex:leftIndex withObjectAtIndex:rightIndex]; for (NSString *str in finalArr) { [finalStr appendString:str]; } break; } } } }
return finalStr.length?finalStr:oriNum; }
|