670. 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]
Approach #1: Math. [Java]
class Solution {
public int maximumSwap(int num) {
char[] digits = Integer.toString(num).toCharArray();
int[] buckets = new int[10];
for (int i = 0; i < digits.length; ++i)
buckets[digits[i]-'0'] = i; for (int i = 0; i < digits.length; ++i) {
for (int j = 9; j > digits[i]-'0'; --j) {
if (buckets[j] > i) {
char temp = digits[i];
digits[i] = digits[buckets[j]];
digits[buckets[j]] = temp;
return Integer.valueOf(new String(digits));
}
}
} return num;
}
}
Analysis:
https://leetcode.com/problems/maximum-swap/discuss/107068/Java-simple-solution-O(n)-time
670. Maximum Swap的更多相关文章
- 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 ...
- 670. Maximum Swap 允许交换一个数 求最大值
[抄题]: Given a non-negative integer, you could swap two digits at most once to get the maximum valued ...
- [LeetCode] Maximum Swap 最大置换
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- [Swift]LeetCode670. 最大交换 | Maximum Swap
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- Maximum Swap LT670
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- LeetCode Maximum Swap
原题链接在这里:https://leetcode.com/problems/maximum-swap/description/ 题目: Given a non-negative integer, yo ...
- 1095. Maximum Swap —— Weekly Challenge
题目限定输入是[0, 10^8],因而不用考虑负数或者越界情况,算是减小了难度. public class Solution { /** * @param num: a non-negative in ...
- 最大交换 Maximum Swap
2018-07-28 16:52:20 问题描述: 问题求解: 使用bucket数组来记录每个数最后出现的位置,然后从左向右遍历一遍即可. public int maximumSwap(int num ...
随机推荐
- win7系统administrator用户提示没有管理员权限,造装驱动安装错误,软件无法使用
警告1909.无法创建快捷方式 最近使用windows 7 32位安装软件,好多都失败.出现以上类似错误. 解决方法:在系统盘(C:)右键属性“安全”选项卡--“编辑”,添加“Everyone”设置为 ...
- mstsc本地驱动器
mstsc 可以把本地驱动器 挂载到服务器上
- [BAT]批处理脚本双击可运行,但在定时计划任务中无法执行(当前工作路径不对导致的)
一开始,红色部分我是用set AutoPath=%cd%,双击可执行,但是将这个批处理脚本放在定时任务中无法执行,后来发现在定时执行的时候,当前工作路径不是批处理脚本所在的路径,而是C:/Window ...
- org.apache.commons札记
StringUtils.isBlank(null); //trueStringUtils.isBlank(""); //trueStringUtils.isBlank(" ...
- python类的继承-1
#!/usr/bin/python3 #类定义 class people: #定义基本属性 name = '' age = 0 #定义私有属性,私有属性在类外部无法直接进行访问 __weight = ...
- fortran write格式
advance="no",就是输出不换行. write(*,"(f10.1)",advance="no")A 格式化输出的控制字符非常的丰富 ...
- verilog系统函数用法
1.$fwrite 向文件写入数据 $fdisplay 格式:$fwrite(fid,"%h%h\n",dout_r1,dout_r2); (1)fwrite是需要触发条件的,在一 ...
- ISO 8895-1
https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Codepage_layout http://czyborra.com/charsets/
- 9) 依赖查询 & 镜像站
依赖查询 http://mvnrepository.com/ Maven仓库查询 http://search.maven.org 仓库 加上这两个,如果使用中央仓库 Eclipse 极有可能会卡死 & ...
- 获取手机的唯一标示uuid
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];