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 ...
随机推荐
- Django-JS实现的ajax
JS实现的ajax ajax的优缺点 AJAX使用Javascript技术向服务器发送异步请求 AJAX无须刷新整个页面 因为服务器响应内容不再是整个页面,而是页面中的局部,所以AJAX性能高 小练习 ...
- resin启动不成功
resin启动只有一个进程,但显示启动正常: Resin/4.0.35 launching watchdog at 127.0.0.1:28084Resin/4.0.35 started -serve ...
- Linux系统下Git操作命令整理
1.显示当前的配置信息 git config --list 2. 创建repo从别的地方获取 git clone git://git.kernel.org/pub/scm/git/git.git 自己 ...
- Linux查看服务和强制结束服务
查看服务id命令,常用的两种: a: ps -ef | grep 服务名 b: netstat -antp 找到结果名称,然后 pki ...
- 牛的障碍Cow Steeplechase
题目描述 Farmer John has a brilliant idea for the next great spectator sport: Cow Steeplechase! As every ...
- Newtonsoft.Json 序列 反序列 IEnumerable
下面是memcached 中获取的obj 类型的数据,转list string sessionId = Request.Cookies["sessionId"].Value;//授 ...
- CSS3 content属性学习
css3中出现了 ":before",":after"伪类, 你可以这样写: h1:after{ content:'h1后插入的文本'; ... } 这两个选择 ...
- MFC--自定义CMFCTabCtrl的实现
在MFC实现桌面程序时,可能会用到TabView效果,我实现的是最基本的效果,如下图: 下面介绍详细的实现过程,如果需要效果更好看些,自行美化. 1. 创建自定义MFCTabCtrl类MyMFCTa ...
- TCP粘包处理 参考spserver
TCP粘包出现的原因就不在详细描述了.TCP粘包是在做TCP编程时经常会遇到的问题,网上相关的参考也不少,大都都是一个热心人士编写的, 若仅用于学习就算了,若用真正用于项目还有待考虑. 本文就简述一下 ...
- day5-configparser模块
一.概述 在软件开发过程中,很多时候需要处理配置文件的读取解析和改写,在python中专门处理配置文件的模块就是configpaser了.顾名思义,configpaser就是配置解析器,可用来对符合格 ...