[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

数字强制转char会失败,必须用digits[buckets[k]] 值-index-值的三层转换

[思维问题]:

取最大、第二大再合并,莫名其妙写起来很麻烦。涉及到交换位置、索引有关的,用桶排序

[一句话思路]:

存索引,然后从9开始往小试

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 字符串转数组,目标是数组,所以需要-'0'

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 字符串转数组,目标是数组,所以需要-'0'

[复杂度]:

时间复杂度为O(N+K),空间复杂度也为O(N+K)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

前面的类型是什么就能转成什么

Integer.valueOf(String.valueOf(digits));

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int maximumSwap(int num) {
//cc //ini: digits, buckets
char[] digits = String.valueOf(num).toCharArray();
int[] bucket = new int[10]; for (int i = 0; i < digits.length; i++) bucket[digits[i] - '0'] = i;
//for loop: exchange
for (int i = 0; i < digits.length; i++) {
for (int j = 9; j > digits[i] - '0'; j--) {
//if bigger index
if (bucket[j] > i) {
char temp = digits[i];
digits[i] = digits[bucket[j]];
digits[bucket[j]] = temp;
return Integer.valueOf(String.valueOf(digits));
}
}
} return num;
}
}

670. Maximum Swap 允许交换一个数 求最大值的更多相关文章

  1. LC 670. Maximum Swap

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

  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. [ACM_暴力] 最多交换k个数的顺序,求a[i]的最大连续和

    /* http://codeforces.com/contest/426/problem/C 最多交换k个数的顺序,求a[i]的最大连续和 爆解 思路:Lets backtrack interval ...

  5. c语言实现交换两个数的值

    C语言中要实现交换两个数的值,可以有很多种方法,具体如下所述. 不使用中间变量: // 异或, a^=b^=a^=b; a ^= b; b ^= a; a ^= b; // 加减 a = a + b; ...

  6. Qt_C++交换两个数

    在编程过程中,交换两个数是我用常用的 ,这里做下简单的搬运 bool Widget::swap(int a, int b) { int temp =a; a= b; b = temp; } 这种方式其 ...

  7. c# 任意多个数,求最大值

    c#  任意多个数,求最大值 使用parms: 正在研究中,如果有好的方案,可评论,共同进步,共同提高,谢谢!

  8. 实现pow(int x, int y),即x的y次方 ; 异或交换两个数;

    问题1:实现pow(int x, int y) ,即x的y次方 x的y次方就是有y个x连续乘机,代码如下: #include <stdio.h> #include <stdlib.h ...

  9. Linux命令 swap:内存交换空间

    swap 内存交换空间的概念 swap使用上的限制

随机推荐

  1. C#修改系统环境变量,调用批处理bat

    一.设置环境变量 public void SetPath(string pathValue) { string pathlist; pathlist = Environment.GetEnvironm ...

  2. PAT1034. Head of a Gang ——离散化+并查集

    题意:成员A与成员B通话 ,成员B与成员C通话,则 ABC即为一个团伙,一共有若干个团伙,每个团伙的人数大于2且相互通话时间超过一定值即为黑帮,每个黑帮伙里有一个BOSS,boss是与各个成员打电话最 ...

  3. Cannot read property 'setState' of undefined

    You're using function() in your Promise chain, this will change the scope for this. If you're using ...

  4. SPUtils

    public class SPUtils { /** * 保存在手机里的SP文件名 */ public static final String FILE_NAME = "my_sp" ...

  5. SQL Server、Oracle和MySQL判断NULL的方法

    SQL Server.Oracle和MySQL判断NULL的方法 本文讲述SQL Server.Oracle.MySQL查出值为NULL的替换. 在SQL Server Oracle MySQL当数据 ...

  6. 安装FreePBX的ISO版本

    下载地址:http://schmoozecom.com/distro-download.php 这个相当于系统了,第一步:安装程序会提示选择你想安装Asterisk的版本:现在出现了11版本,这个根据 ...

  7. 016:Explain

    一. Explain EXPLAIN 官方文档 1.explain说明 explain是解释SQL语句的执行计划,即显示该SQL语句怎么执行的 使用explain的时候,也可以使用desc 5.6 版 ...

  8. ubuntu16.04让内核编译一次过的方法

    问题: 进入内核后,发现make menuconfig 出错,而且在在网上找到的一些安装包,安装结束后,发现make menuconfig后的图形界面虽然出来了,但是图形界面里的内容没有出来! 解决方 ...

  9. json data 解析demo

    json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data&quo ...

  10. OD 实验(一) - 修改程序标题

    需要修改的程序 把 I love fishc.com 修改为 hello world sch01ar 用 OD 打开程序 在程序入口处开始一直按 F8 运行程序,看看在哪里弹出对话框 运行到该地址的时 ...