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 ...
随机推荐
- windows文件属性操作 dsofile
dsofile.dll是com组件,.net程序中引用dsofile.dll文件后,程序集名称会变成“Interop.DSOFile.dll”, com组件需要用regsvr32注册,所以需要注册ds ...
- OpenSSL 结构体
X509_STORE 头文件:x509_vfy.h 定义 typedef struct x509_store_st X509_STORE; struct x509_store_st { /* The ...
- PAT 1052 卖个萌 (20)(代码+思路)
1052 卖个萌 (20)(20 分) 萌萌哒表情符号通常由"手"."眼"."口"三个主要部分组成.简单起见,我们假设一个表情符号是按下列格 ...
- PAT 1047 编程团体赛(代码)
1047 编程团体赛(20)(20 分) 编程团体赛的规则为:每个参赛队由若干队员组成:所有队员独立比赛:参赛队的成绩为所有队员的成绩和:成绩最高的队获胜. 现给定所有队员的比赛成绩,请你编写程序找出 ...
- Rendering Resources
1. Real-Time Rendering Resources http://www.realtimerendering.com/ 2. Books on Amazon http://www.ama ...
- Codeforces 612B. Wet Shark and Bishops 模拟
B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- cuda使用
import torchx = torch.randn(2, 3)x = x.cuda() print (x)#最简单的cuda使用,但是感觉好慢啊
- Java读properties文件中文乱码问题的解决方法
java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...
- 2018.08.27 lucky(模拟)
描述 Ly 喜欢幸运数字,众所周知,幸运数字就是数字位上只有 4 和 7 的数字. 但是本题的幸运序列和幸运数字完全没关系,就是一个非常非常普通的序列. 哈哈,是 不是感觉被耍了,没错,你就是被耍了. ...
- VS2010/MFC编程(对话框:模态对话框及其弹出过程)
讲讲什么是模态对话框和非模态对话框,以及模态对话框怎样弹出. 一.模态对话框和非模态对话框 Windows对话框分为两类:模态对话框和非模态对话框. 模态对话框是这样的对话框,当它弹出后,本应用程序其 ...