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 ...
随机推荐
- Oracle数据库使用总结
--1.使用月份作为条件筛选(to_char函数与extract函数使用) select * from test_date where to_char(dqsj,'mm') like '%07%'; ...
- oracle中INSTR函数的用法
今天有个同学问我这个INSTR函数,我也不太清楚就上网查了查做一个小小的记录吧 INSTR(C1,C2,I,J) 在一个字符串中搜索指定的字符,返回发现指定的字符的位置; C1 被搜索的字符串 C2 ...
- 安装配置资产管理软件GLPI
GLPI是法语Gestionnaire libre de parc informatique的缩写,是一款历史悠久的资产管理软件: GLPI提供功能全面的IT资源管理接口,可以用来建立数据库全面管理I ...
- quartz(2) -- 入门案例
第一步:添加jar,maven配置 <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</g ...
- Oracle递归查询与常用分析函数
最近学习oracle的一些知识,发现自己sql还是很薄弱,需要继续学习,现在总结一下哈. (1)oracle递归查询 start with ... connect by prior ,至于是否向上查 ...
- JavaScript tips —— 谈谈数组乱序
前言 先看一个段代码 function randArr (arr) { return arr.sort(() => { return (Math.random() - 0.5); }); } 目 ...
- angular一些常用的方法:
angular.copy(); 用法:对Object对象的深度拷贝$scope.data = {name:'yanjinyun',age:'11'}; $scope.origData = angula ...
- cin关闭流同步加速
习惯了用cin 很多人会说cin的速度比scanf慢很多, 其实不然. cin慢的原因主要在于默认cin与stdin总是保持同步, 这一步是消耗时间大户. 只需要加上std::iOS::sync_wi ...
- Oracle11g数据库监听配置
(转自:http://blog.sina.com.cn/s/blog_6908928501018057.html) 经验告诉我:最好把数据库的SID和数据库全局名称分开,免得配置时混了,如果要配置服务 ...
- shell编程实例1
1.vim hello.sh 2. #!bin/bash echo "hello world!" 3.chmod +x hello.sh 4.source hello.sh ls ...