原题链接在这里:https://leetcode.com/problems/maximum-swap/

题目:

Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.

Example 1:

Input: 2736
Output: 7236
Explanation: Swap the number 2 and the number 7. 

Example 2:

Input: 9973
Output: 9973
Explanation: No swap. 

Note:

  1. The given number is in the range [0, 108]

题解:

想要组成最大的数字,就是要把尽量开头的digit换成后面比他大的最大digit. 若这个最大digit有重复, 去最右侧的那个.

It is about how to get the index of largest digit after current one.

所以先把么个digit出现的last index记录下来.

Iterating num, check from max = 9 to check if max last occurance index is after i. If yes, swap.

Time Complexity: O(n). n是原有num digits的位数.

Space: O(n).

AC Java:

 class Solution {
public int maximumSwap(int num) {
char [] digits = Integer.toString(num).toCharArray(); int [] lastInd = new int[10];
for(int i = 0; i<digits.length; i++){
lastInd[digits[i]-'0'] = i;
} for(int i = 0; i<digits.length; i++){
for(int k = 9; k>digits[i]-'0'; k--){
if(lastInd[k] > i){
char temp = digits[i];
digits[i] = digits[lastInd[k]];
digits[lastInd[k]] = temp;
return Integer.valueOf(new String(digits));
}
}
}
return num;
}
}

类似Remove K Digits.

LeetCode Maximum Swap的更多相关文章

  1. [LeetCode] Maximum Swap 最大置换

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  2. LeetCode:Maximum Depth of Binary Tree_104

    LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...

  3. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  4. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  5. LC 670. Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  6. [LeetCode] 670. Maximum Swap 最大置换

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  7. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. [Swift]LeetCode670. 最大交换 | Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  9. 670. Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

随机推荐

  1. 通过FFmpeg将多媒体文件解码后保存成Bmp图像(YUV420 RGB32)

    /* g++ -o test test.cpp -lavformat -lavcodec -lavutil -lz -lm -lpthread -lswscale */ #include <st ...

  2. Web开发相关笔记

    1.MySQL命令行下执行.sql脚本详解http://database.51cto.com/art/201107/277687.htm 在可视化工具里导出.sql脚本 --> 放命令行里运行 ...

  3. EasyUI:datagrid控件简介

    EasyUI:datagrid控件简介 1,水平滚动条属性: //显示滚动条 fitColumns:false //不显示滚动条 fitColumns:true

  4. vue2.0 transition 多个元素嵌套使用过渡

    在做vue的demo的时候遇到一个问题,多个嵌套的元素如何设置transition? 我的代码:代码中元素整体做平移,里面的inner中做旋转,实现一个圆形滚动的效果 <transition n ...

  5. CSS3 文本常用属性

    CSS 常用属性 text-shadow属性文字阴影:第一个值背景相对原本文字居左的距离,第二个值据当前文本上方的距离,第三个值清晰度(越小越清晰),第四个值颜色 word-wrap:自动换行,如果是 ...

  6. 20145222黄亚奇《网络对抗》MSF基础应用

    实践目标 掌握metasploit的基本应用方式. 具体需要完成(1)ms08_067;(2)ms11_050:(3)Adobe(4)成功应用任何一个辅助模块. 实验内容 掌握metasploit的基 ...

  7. 非法字符: '\ufeff' 解决方案|错误: 需要class, interface或enum

    解决方案,把文件用Editplus打开,UTF-8+BOM编码的文件转为普通的UTF-8文件

  8. linux 进阶命令___0002

    #列出重复文件,首先检查文件大小,再检查md5sum find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | ...

  9. 编写一个程序,将 d:\java 目录下的所有.java 文件复制到 d:\jad 目录下,并将原来文件的扩展名从.java 改为.jad。

    package IO; import java.io.*; public class FileCopy { public static void main(String[] args) throws ...

  10. mysql 创建数据库 并设置utf8格式

    CREATE DATABASE `database` CHARACTER SET utf8 COLLATE utf8_general_ci; 设置utf8之后,不容易出现中文乱码.