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]

思路:

暴力枚举所有交换可能,竟然没有超时。。。感觉因为输入最多到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]的更多相关文章

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

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

  2. LC 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. 670. Maximum Swap 允许交换一个数 求最大值

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

  5. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  6. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  7. [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 ...

  8. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  9. [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 ...

  10. [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

随机推荐

  1. Codeforces Round #487 (Div. 2)

    A. A Blend of Springtime(暴力/模拟) 题目大意 给出$n$个花,每个点都有自己的颜色,问是否存在连续大于等于三个花颜色均不相同 sol 直接模拟判断即可 #include&l ...

  2. nuxt 优化项:禁用js的预加载

    这里有个nuxt和vue不同的地方,这个地方很有意思,官方的中文文档说得蜜汁自信 ------------------------------- In production, nuxt.js uses ...

  3. MySQL至TiDB复制延迟监控

    因生产环境mysql中有较多复杂sql且运行效率低,因此采用tidb作为生产环境的从库进行部分慢sql及报表的读写分离.其中MySQL至TIDB采用Syncer工具同步.关于TIDB的安装及Synce ...

  4. ecshop 后台添加新菜单 以及 权限控制

    首先 在languages\zh_cn\admin\common.php 中添加 一级菜单 二级菜单 其次 在admin\includes\inc_menu.php 中添加 然后 在admin\inc ...

  5. vue cli3超详细创建多页面配置

    1.首先按照vue cli3 给的入门文档下载个vue cli3 如果之前下载了vue cli2的要先卸载之前的 2.检查安装是否成功 3.ok,现在环境搭建好了,新建项目 vue create he ...

  6. SpaceVim 语言模块 python

    原文连接: https://spacevim.org/cn/layers/lang/python/ 模块简介 功能特性 依赖安装及启用模块 启用模块 语法检查 代码格式化 格式化 imports 快捷 ...

  7. vue相关ajax库的使用

    相关库: vue-resource: vue插件, 多用于vue1.x axios: 第三方库, 多用于vue2.x vue-resource使用 // 引入模块 import VueResource ...

  8. VS2015编译MapWinGIS

    在github上下载MapWinGIS,目前最新版本为4.9.5.0 GitHub上项目地址为:https://github.com/MapWindow/MapWinGIS 通过git客户端下载mas ...

  9. 关于java的wait、notify、notifyAll方法

    wait.notify.notifyAll 遇到的问题 之前开发打印机项目,因为需要使用多线程技术,当时并不怎么理解,一开始随意在方法体内使用wait.notify.notifyAll 方法导致出现了 ...

  10. python版protobuf 安装

    转自:http://www.tuicool.com/articles/VfQfM3 1. 下载protobuf源代码(当前最新版本为:2.5.0) #cd /opt #wget https://pro ...