int find(char *s, int start, int len){
int ret = -1;
int i;
char tmp = 0;
for(i=len-1; i>start; i--){
if(s[i] > s[start] && s[i] > tmp){
tmp = s[i];
ret = i;
}
}
return ret;
}
void swap(char *a, char *b){
char c = *a;
*a = *b;
*b = c;
}
int get_num(char *s, int len){
int i, ret = 0;
for(i=0; i<len; i++){
ret *= 10;
ret += s[i]-'0';
}
return ret;
}
int maximumSwap(int num){
char s[100];
int len, i, tmp;
sprintf(s, "%d", num);
len = strlen(s);
for(i=0; i<len; i++){
tmp = find(s, i, len);
if(tmp == -1) continue;
swap(&s[i], &s[tmp]);
break;
}
return get_num(s, len);
}
leetcode-670. 最大交换-C语言
最新推荐文章于 2022-10-10 23:26:39 发布