[leetcode-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]
思路:
暴力枚举所有交换可能,竟然没有超时。。。感觉因为输入最多到108
最多8位,复杂度O(n3)虽然很高,但数据规模小,但总之不是什么好办法。
vector<int> digits(int num)
{
if (num == )return{};
vector<int> ret;
while (num > )
{
ret.push_back(num % );
num /= ;
}
reverse(ret.begin(), ret.end());
return ret;
}
int toNum(vector<int>num)
{
int r = ;
for (int i = ; i < num.size();i++)
{
r *= ;
r += num[i];
}
return r;
}
int maximumSwap(int n)
{
vector<int> num = digits(n);
int m = n; for (int i = ; i < num.size();i++)
{
for (int j = i + ; j < num.size();j++)
{
swap(num[i], num[j]);
m = max(m, toNum(num));
swap(num[i], num[j]);
}
}
return m;
}
[leetcode-670-Maximum Swap]的更多相关文章
- [LeetCode] 670. Maximum Swap 最大置换
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- LC 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 ...
- 670. Maximum Swap 允许交换一个数 求最大值
[抄题]: Given a non-negative integer, you could swap two digits at most once to get the maximum valued ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- 简单几行代码使用百度地图API接口分页获取信息
首发于: 万能助手扩展开发:使用百度地图API接口分页获取信息_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=426 使用 ...
- E. K Balanced Teams
类比背包问题,为每个学生附加一个权重$pos[i]$,意思是选择该学生后,之后可以选择$p[i]~p[i]+5$的学生. 转换公式: $$d[i][j]=max(d[i+1][q],d[i+pos][ ...
- Linux基础(05)、Linux进阶命令
目录 一.进阶命令 二.系统命令 三.压缩和归档 3.1.归档 3.2.压缩 3.3.归档并压缩 归档.接档:tar -cf.tar -tvf 压缩.解压:gzip.gunzip 归档并压缩:tar ...
- Android内存分析工具
在Android系统开发过程中,经常会要去分析进程的内存的使用情况,简单介绍下Android内存分析的相关工具. 文章参考: 1.dumpsys 2.memory-analysis-command 1 ...
- Altium Designer常用快捷键
一:Altium原理图快捷键: Shift+左键选择 :实现多个目标选择 Ctrl+左键拖动 :保持连线拖动目标 Shift+c :清除当前过滤(?? ...
- Python的scrapy之爬取顶点小说网的所有小说
闲来无事用Python的scrapy框架练练手,爬取顶点小说网的所有小说的详细信息. 看一下网页的构造: tr标签里面的 td 使我们所要爬取的信息 下面是我们要爬取的二级页面 小说的简介信息: 下面 ...
- ...续上文(一个小萌新的C语言之旅)
我们继续上次没介绍完的继续讲: 下面我们说一下二进制,二进制是计算技术中广泛采用的一种 数制. 二进制数据是用0和1两个 数码来表示的数.它的基数为2,进位规则是“逢二进一”.那么二进制怎么转化为十进 ...
- 【数据结构】线性表&&顺序表详解和代码实例
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 预备知识 1.0 什么是线性表? 线性表(List)是零个或者多个数据元素的有限序列. 首先它是一个序列.里面的元素是有顺 ...
- (cmath)关于头文件cmath
关于头文件cmath // -*- C++ -*- C forwarding header. // Copyright (C) 1997-2014 Free Software Foundation, ...
- 20145202马超《网络对抗》Exp8 Web基础
1.本实践的具体要求有: (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. (2).Web前端javas ...