LeetCode Maximum Swap
原题链接在这里: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:
- 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;
     }
 }
LeetCode Maximum Swap的更多相关文章
- [LeetCode] Maximum Swap 最大置换
		Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ... 
- LeetCode:Maximum Depth of Binary Tree_104
		LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ... 
- LeetCode Maximum Product Subarray(枚举)
		LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ... 
- LeetCode——Maximum Depth of Binary Tree
		LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ... 
- LC 670. Maximum Swap
		Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ... 
- [LeetCode] 670. Maximum Swap 最大置换
		Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ... 
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
		Find the contiguous subarray within an array (containing at least one number) which has the largest ... 
- [Swift]LeetCode670. 最大交换 | Maximum Swap
		Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ... 
- 670. Maximum Swap
		Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ... 
随机推荐
- 如何选择合适的MySQL数据类型
			一.MySQL数据类型选择原则 更小的通常更好:一般情况下选择可以正确存储数据的最小数据类型.越小的数据类型通常更快,占用磁盘,内存和CPU缓存更小. 简单就好:简单的数据类型的操作通常需要更少的CP ... 
- 配置内核 Makefile:1449: *** mixed implicit and normal rules. Stop.【转】
			本文转载自:https://blog.csdn.net/bitowang/article/details/8446494 在编译内核的时候提示Makefile:1449: *** mixed impl ... 
- linux下字典生成工具-crunch与rtgen
			所谓的密码字典主要是配合密码破解软件所使用,密码字典里包括许多人们习惯性设置的密码.这样可以提高密码破解软件的密码破解成功率和命中率,缩短密码破解的时间.当然,如果一个人密码设置没有规律或很复杂,未包 ... 
- Percona 工具包 pt-online-schema-change 简介
			mysql的在线表结构修改,因为低效和阻塞读写.一直被诟病.至于ALTER TABLE 的原理,参看我上一篇文章.MySQL在线修改大表结构.看完后,发现的问题是还是会锁的,且对于在线更新的这块也是不 ... 
- Ubuntu 1210怎么获得root权限登录
			Ubuntu 12.10 怎么用Root 登录?以下是Ubuntu 12.10 启用Root 登录的方法吗,希望对大家有些帮助吧! 方法如下: 1.先设定一个 Root 密码 sudo passwd ... 
- cssrem 比例适配理解
			cssrem只是帮你自动计算,省去了你在切图时,从设计稿拿到的px再根据比例转换成rem的中间过程. 这个40我无法猜测, 以前设计稿给的都是按640px(iphone5s的宽)来的,我们就按照这个比 ... 
- js进阶---12-12、jquery事件委托怎么使用
			js进阶---12-12.jquery事件委托怎么使用 一.总结 一句话总结:通过on方法(事件委托),给要绑定事件的元素的祖先绑定事件,从而达到效果. 1.事件委托是什么? 通过事件冒泡,让子元素绑 ... 
- WebService是什么?以及工作原理
			WebService 就是一个应用程序,向外界暴露出公开的API使别人其能在WEB对其进行远程调用,具有跨平台和跨语言的等特点,采用Internet的Http协议进行客户端与服务器之间的交互 由XML ... 
- Vue2基于Axios Ajax Vuex的Loading组件
			1. 定义根state:ajaxIsLoading2. 在Axios拦截器中commit不同的状态实现状态切换3. 组件中通过getter获取ajaxIsLoading状态 Axios 拦截器配置 i ... 
- poj1274
			题解: 二分图匹配 裸题匈牙利匹配 代码: #include<cstdio> #include<cstring> #include<cmath> #include& ... 
