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 ...
随机推荐
- Codeforces 691C. Exponential notation 模拟题
C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- Telnet 安装
Telnet 安装 一.Telnet 安装 (1) 登录目标主机检测 telnet 服务是否正常 [root@localhost ~]# telnet localhost -bash: telnet: ...
- C语言 链表基本函数
#include <stdio.h> #include <malloc.h> typedef struct my_node mynode; struct my_node{ ...
- java实现网站paypal支付功能并且异步修改订单的状态
java实现网站paypal支付功能并且异步修改订单的状态:步骤如下 第一步:去paypal的官网https://www.paypal.com注册一个个人账号,在创建沙箱测试账号时需要用到 第二步:p ...
- ThinkPHP5命令行 执行控制器下的某方法
入口文件后加一个空格就行了 1,首先cd到站点目录public下,我的入口文件是默认的index.php,然后执行以下命令,, 2,php要加入环境变量,index.php后面加空格,然后是模块,控制 ...
- MAC安装远程工具Securecrt的破解方式(详细有图)
想要实现mac的远程连接功能,本来想使用终端的,但是终端的很多功能是欠佳的,所以决定安装一款,像windows的xshell一样好的软件,所以选择了这款Securecrt. 首先准备两个东西,一个是S ...
- 关于React的入门级安装和最浅显解释
春节临近,办公室里半片空位,半片浮嚣. 想到将放假,屏幕上的代码也都变成了雀跃的小虫. 无法专心了. 终于还是强迫自己读了半篇文档,写了几坨程序. 这次记录的是关于React,最浅显的内容. ———— ...
- git push/pull时总需要输入用户名密码的解决方案
在提交项目代码或者拉代码的时候,git会让你输入用户名密码,解决方案:(我们公司用的是gitlab) 执行git config --global credential.helper store命令 然 ...
- hdu1081 To The Max 2016-09-11 10:06 29人阅读 评论(0) 收藏
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 配置好Nginx后,通过flume收集日志到hdfs(记得生成本地log时,不要生成一个文件,)
生成本地log最好生成多个文件放在一个文件夹里,特别多的时候一个小时一个文件 配置好Nginx后,通过flume收集日志到hdfs 可参考flume的文件 用flume的案例二 执行的注意点 avro ...