原题链接在这里: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. shell-一些有趣的使用

    1. 对字符串进行MD5加密  echo test |md5sum|awk '{print $1}' 字符串数量很多时可以这样做: echo test |md5sum|awk '{print $1}' ...

  2. Gentoo系统安装步骤详解

    下载镜像 一般我都是用国内的镜像源,不管是centos,ubuntu还是gentoo在国内的镜像来说肯定比国外快 #下载地址mirrors.163.com/gentoo/#我用的x86的http:// ...

  3. 文件(1)--File

    File简介 Java.io.File用于表示文件(目录),也就是说程序员可以通过File类在程序中操作硬盘上的文件和目录.File类只用于表示文件(目录)的信息(名称.大小等),不能对文件的内容进行 ...

  4. 关于使用JAVA正则表达式报java.lang.StackOverflowError错误问题

    最近在使用hadoop做apache日志分析,发现测试数据没问题,但数据一多就出问题,报 java.lang.StackOverflowError错误,最后定位为正则表达式栈溢出,发现某些行的日志数据 ...

  5. 低版本C++ string的万能转换,从long string 之间的转换来看看

    string 转 long 那必须是万年atoi(),不过得配合c_str()使用! [plain] view plain copy #include <string> #include  ...

  6. on绑定阻止冒泡失败

    使用zepto库,有如下dom <div id="J_parent"> <a href="#"> <span>点我有惊喜&l ...

  7. ReactiveX/RxJava文档中文版

    项目地址:https://github.com/mcxiaoke/RxDocs,欢迎Star和帮忙改进. 有任何意见或建议,到这里提出 Create New Issue 阅读地址 ReactiveX文 ...

  8. 本地测试ajax遇到的跨域问题

    浏览器console问题描述:Cross origin requests are only supported for protocol schemes: http, data, chrome, ch ...

  9. C语言中链接影响程序的细节

    参考:<深入理解计算机系统>  7.61节  链接器如何解析多重定义的全局符号 基本的原则是这样的:对于所有的全局符号,函数和已初始化的全局变量是强符号,未初始化的全局变量是弱符号. Un ...

  10. 新东方雅思词汇---8.1、reckon

    新东方雅思词汇---8.1.reckon 一.总结 一句话总结:reck(注意,留心)+on 英 ['rek(ə)n]  美 ['rɛkən]  vt. 测算,估计:认为:计算 vi. 估计:计算:猜 ...