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:

  1. The given number is in the range [0, 108]
 

Runtime: 4 ms, faster than 86.08% of C++ online submissions for Maximum Swap.

//
// Created by yuxi on 2019-01-18.
//
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std; class Solution {
private:
unordered_map<int,vector<int>> mp;
public:
int getret(vector<int>& a){
int ret = ;
for(int i=; i<a.size(); i++){
ret = ret * + a[i];
}
return ret;
}
int maximumSwap(int num) {
int savenum = num;
vector<int> numa;
while(num > ){
numa.push_back(num%);
num /= ;
}
reverse(numa.begin(), numa.end());
for(int i=; i<numa.size();i++) mp[numa[i]].push_back(i);
if(numa.size() == ) return numa[];
helper(numa, );
return getret(numa);
}
void helper(vector<int>& a, int idx) {
if(idx == a.size()) return ;
int maxval = INT_MIN, maxidx = ;
for(int i=idx; i<a.size(); i++) {
if(maxval < a[i]) {
maxval = a[i];
maxidx = i;
}
}
if(maxval != a[idx]) {
int tmp = a[idx];
a[idx] = maxval;
a[maxidx] = tmp;
if(mp[maxval].size() != && mp[maxval].back() > maxidx) {
a[mp[maxval].back()] = tmp;
a[maxidx] = maxval;
}
return;
} else {
helper(a, idx+);
}
}
};

LC 670. Maximum Swap的更多相关文章

  1. 670. Maximum Swap 允许交换一个数 求最大值

    [抄题]: Given a non-negative integer, you could swap two digits at most once to get the maximum valued ...

  2. [LeetCode] 670. Maximum Swap 最大置换

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  3. 670. Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  4. [LeetCode] Maximum Swap 最大置换

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  5. [Swift]LeetCode670. 最大交换 | Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  6. Maximum Swap LT670

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  7. LeetCode Maximum Swap

    原题链接在这里:https://leetcode.com/problems/maximum-swap/description/ 题目: Given a non-negative integer, yo ...

  8. 1095. Maximum Swap —— Weekly Challenge

    题目限定输入是[0, 10^8],因而不用考虑负数或者越界情况,算是减小了难度. public class Solution { /** * @param num: a non-negative in ...

  9. LC 918. Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

随机推荐

  1. MySQL cmd操作

    1.开启关闭服务 net start mysql net stio mysql 2.登陆 在CMD命令窗口敲入命令 mysql -hlocalhost -uroot -p 后按回车(注意这里的&quo ...

  2. Java中接口是否可以继承多个接口?

    可以. 接口是常量值和方法定义的集合.接口是一种特殊的抽象类. java类是单继承的.classB Extends classA java接口可以多继承.Interface3 Extends Inte ...

  3. 数学模块 math 函数的调用

    数学模块 math 模块名: math 注: linux下为内建模块 Mac OS下为标准库模块 数学模块用法: import math # 或 from math import * 数据 描述 ma ...

  4. OverflowError:django signed integer is greater than maximum 数据库日期字段相关错

    使用django中的默认数据库sqlite3, 在pycharm中录入日期字段相关信息结果出现问题 在保存的时候如图 直接在界面选择的日期变成了时间戳, 并且在获取数据的时候报错 经过查询之后(舔大佬 ...

  5. Kinect for windows的脸部识别

    需要引入的dll: 需要将下面两个dll复制到当前路径 Kinect for windows提供了脸部识别的功能,可以识出人脸.主要是通过FaceTrackFrame类的GetTriangles()来 ...

  6. __builtin_ _Find_first()

    •int __builtin_ffs (unsigned int x) 返回x的最后一位1的是从后向前第几位,比如7368(1110011001000)返回4. •int __builtin_clz ...

  7. Java查漏补缺

    1.自动转换按从低到高的顺序转换.不同类型数据间的优先关系如下: 低 ---------------------------------------------> 高 byte,short,ch ...

  8. wmware虚拟化的启动问题

    2019-05-09,14点14 vmware出现VMware提示:已将该虚拟机配置为使用 64 位客户机操作系统.但是,无法执行 64 位操作.解决方案 进入bios里面intel 虚拟化技术 先设 ...

  9. docker修改数据库密码

    运行mysql(--name 容器名称  -e MYSQL_ROOT_PASSWORD设置初始密码  -p 3307:3306  端口映射,主机端口3307) docker run --name my ...

  10. CSS3 -- column 实现瀑布流布局

    本例使用 CSS column 实现瀑布流布局 关键点,column-count: 元素内容将被划分的最佳列数 关键点,break-inside: 避免在元素内部插入分页符 html div.g-co ...