题解 | #最大数字交换#
最大数字交换
https://www.nowcoder.com/practice/ffcd59fbf5814d2e94c0e2f4a679473c
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param num string字符串
* @return string字符串
*/
export function maximumSwap(num: string): string {
const list = num.split('');
const sortList = num.split('');
sortList.sort((a,b)=> Number(b) - Number(a));
let i = 0;
while(i<list.length){
if(list[i] != sortList[i]){
const targetIndex = list.lastIndexOf(sortList[i]);
list[targetIndex] = list[i];
list[i]=sortList[i]
return list.join('')
}
i++;
}
return num
}
