LC 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]
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的更多相关文章
- 670. Maximum Swap 允许交换一个数 求最大值
[抄题]: Given a non-negative integer, you could swap two digits at most once to get the maximum valued ...
- [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 numbe ...
- [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 ...
- 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 ...
随机推荐
- Linux常用配置选项
- kubernetes之node隔离与恢复
需求: 在硬件升级, 硬件维护等情况下, 我们需要将某些node隔离, kubectl cordon <node_name> #禁止pod调度到该节点上, 在其上运行的pod并不会自动停止 ...
- <meta http-equiv="X-UA-Compatible" content="IE=edge">详解
X-UA-Compatible是针对IE8新加的一个设置,对于IE8之外的浏览器是不识别的. 这个区别与content="IE=7"在无论页面是否包含<!DOCTYPE> ...
- Training #2 cell battle (BFS)
Constraints: 1 <= R, C <= 500 1 <= T <= 5 Sample Input: 5 3 5 ##### a...b ##### 3 4 #### ...
- Java&Selenium&JS&AWT之那些难以点击到的按钮
一.摘要 本篇博文的重点并不是简单的click()方法,而是要讲的是那些click()方法失效的时候的处理方式,其实做自动化久了我们都能发现研发的代码并不是都那么美丽,selenium支持的8种定位方 ...
- Windows环境下使用uiautomatorviewer进行元素定位
一.摘要 元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作,uiautomatorviewer 是 android-sdk 自带的一个元 ...
- python_模块1
1.将指定的字符串进行加密 # 导入模块 import hashlib def get_md5(data): # 获取hashlib模块中的md5加密算法 obj = hashlib.md5() # ...
- python_函数高级
1.函数名当变量来使用 def func(): print('wdc') # 可以将函数赋值给变量 v1 = func v1() func() def func(): print('wdc')# 可以 ...
- 【线段树 dp】8.6集合
线段树维护dp 题目大意 给定初始大小为 $N$ 的正整数集合.定义两个数$x$和$y$建立联系的的代价为 $|x-y|$.我们定义整数集合的代价为:将每个整数都与至少一个另外的整数建立联系之后,所有 ...
- vue的prop父子组件传值
props down, events up 父组件通过 props 向下传递数据给子组件:子组件通过 events 给父组件发送消息. 静态 props 要让子组件使用父组件的数据,需要通过子组件的 ...