题解 | #最大数字交换#

最大数字交换

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
}

全部评论

相关推荐

03-02 17:23
门头沟学院 Java
程序员小白条:换项目,然后技术栈加点408丰富,然后有没有水赛奖项填充下,另外注意主修课程没必要写,除非全是99,100...其他分数毫无毕业,你都985了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务