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 ...
随机推荐
- 《浏览器工作原理与实践》<07>变量提升:JavaScript代码是按顺序执行的吗?
讲解完宏观视角下的浏览器后,从这篇文章开始,我们就进入下一个新的模块了,这里我会对 JavaScript 执行原理做深入介绍. 今天在该模块的第一篇文章,我们主要讲解执行上下文相关的内容.那为什么先讲 ...
- Some ML Tutorials
VAE: What-is-variational-autoencoder-vae-tutorial Variational-autoencoders-explained Building variat ...
- Python3+Appium学习笔记02-环境配置(下)
配置所需软件及我当前使用的版本: 1)java jdk 1.8.0 2)android sdk 24.4.1 3)Python3 3.7.3 4)Appium-Python-Client 5)n ...
- Windows&Appium&Python自动化测试-Appium安装
一.安装node.js 官方下载地址为:https://nodejs.org/en/download 傻瓜式安装即可,安装完成后,CMD中运行node -v查看版本号 输入npm 出现如上图信息,表示 ...
- C# 通过物理路径将文件以二进制保存到指定文件夹
/// <summary> /// 通过物理路径将文件以二进制保存到指定文件夹 /// </summary> /// <param name="filePath ...
- 软件测试常用的linux命令
不同Linux发行版的命令数量不一样,但Linux发行版本最少的命令也有200多个.这里我把比较重要和使用频率最多的命令,按照它们在系统中的作用分成下面六个部分一一介绍. ◆ 安装和登录命令:logi ...
- [Javascript] How to deal with floating number
What's your expect of the output?: console.log(0.1 + 0.2 === 0.3); The answer is 'false'. Because: 0 ...
- 【SQL-历史执行语句查询】 查询对数据库执行了哪些SQL
Sql语句 QS.creation_time as '创建时间', ), (( THEN DATALENGTH(st.text) ) ) AS '查询语句' , ST.text as '执行文本', ...
- jquery里把数组转换成json的方法
首先来看,jquery里自带的,和json相关的函数: 1.$.parseJSON : 用来解析JSON字符串,返回一个对象. 什么叫“JSON字符串”? 比如: var a={name:&quo ...
- 018_linuxC++之_抽象类的引入
(一)参考原文链接:C++多态 (二) 抽象类 在介绍抽象类之前,我们先介绍一下纯虚函数. 1.纯虚函数 在基类中仅仅给出声明,不对虚函数实现定义,而是在派生类中实现.这个虚函数称为纯虚函数.普通函数 ...