[抄题]:

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. Unit06: Spring对JDBC的 整合支持 、 Spring+JDBC Template、Spring异常处理

    Unit06: Spring对JDBC的 整合支持 . Spring+JDBC Template .Spring异常处理 1. springmvc提供的异常处理机制 我们可以将异常抛给spring框架 ...

  2. laravel验证器例子

    直接贴测试代码 Route::get('/', function() { $name = "rico"; $validateData = array('name1' => $ ...

  3. java代码--------构造方法的调用

    总结: package com.sads; //构造方法何时被调用, //构造方法里的内容先执行 public class Sdw { static { System.out.println(&quo ...

  4. 架构-架构风格:RESTful

    ylbtech-架构-架构风格:RESTful 一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层 ...

  5. java,js 解unicode

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class UnicodeDecodeDataConver ...

  6. Python web框架 Tornado(三)自定义session组件

    我们在学习Django框架的过程中,内部封装了session组件,以方便于我们使用进行验证.但是Tornado框架是没有session的,所以如果想使用session的话,就需要我们自己定制相对应的组 ...

  7. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    证券代码 证券简称 大股东持股比例 [日期] 最新 [大股东排名] 第1名 [单位] % 总市值2 [交易日期] 最新收盘日 [单位] 亿元 000004.SZ 国农科技 28.4200 23.261 ...

  8. Python handling an exception

    #try...except... try: You do your operations here; ...................... except ExceptionI: If ther ...

  9. pycharm多行代码缩进、左移

    在使用pycharm时,经常会需要多行代码同时缩进.左移,pycharm提供了快捷方式 1.pycharm使多行代码同时缩进 鼠标选中多行代码后,按下Tab键,一次缩进四个字符 2.pycharm使多 ...

  10. 自然语言处理NLTK

    Python文本分析工具NLTK 情感分析 文本相似度 文本分类 分类预测模型:朴素贝叶斯 实战案例:微博情感分析