2018-07-28 16:52:20

问题描述:

问题求解:

使用bucket数组来记录每个数最后出现的位置,然后从左向右遍历一遍即可。

    public int maximumSwap(int num) {
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 (int i = 0; i < digits.length; i++) {
for (int k = 9; k > digits[i] - '0'; k--) {
if (bucket[k] > i) {
char tmp = digits[i];
digits[i] = digits[bucket[k]];
digits[bucket[k]] = tmp;
return Integer.valueOf(new String(digits));
}
}
}
return num;
}

最大交换 Maximum Swap的更多相关文章

  1. [Swift]LeetCode670. 最大交换 | Maximum Swap

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

  2. 如何在Linux上使用文件作为内存交换区(Swap Area)

    交换区域(Swap Area)有什么作用? 交换分区是操作系统在内存不足(或内存较低)时的一种补充.通俗的说,如果说内存是汽油,内存条就相当于油箱,交换区域则相当于备用油箱. Ubuntu Linux ...

  3. 如何增加Ubuntu交换空间swap

    如何增加Ubuntu交换空间swap 1  使用命令查看系统内swap分区大小 green@green:~$ free -m total used free shared buff/cache ava ...

  4. Linux 交换分区swap

    Linux 交换分区swap 一.创建和启用swap交换区 如果你的服务器的总是报告内存不足,并且时常因为内存不足而引发服务被强制kill的话,在不增加物理内存的情况下,启用swap交换区作为虚拟内存 ...

  5. LC 670. Maximum Swap

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

  6. Linux_交换分区SWAP

    一.交换分区SWAP 1️⃣:交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用. 2️⃣:交换分区一般指定虚拟内存的大小 ...

  7. 670. Maximum Swap 允许交换一个数 求最大值

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

  8. [LeetCode] Maximum Swap 最大置换

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

  9. Maximum Swap LT670

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

随机推荐

  1. #C++初学记录(算法考试1)

    B - Maximal Continuous Rest Each day in Berland consists of n hours. Polycarp likes time management. ...

  2. STA分析(四) lib model

    library中的一个cell可以是一个standard cell,IO buffer,或者一个complex IP.其中包含area,functionality,timing,power等相关的信息 ...

  3. php PDO遇到的坑

    <?php $dbConn = new PDO( "mysql:host=localhost;dbname=adtuu",'root','root', array( // 强 ...

  4. linux常用命令:traceroute 命令

    通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...

  5. click 在网页测试手机模式下无效,不能执行。调成非手机模式即可

    click  在网页测试手机模式下无效,不能执行. 调成非手机模式即可

  6. 利用构造函数对canvas里面矩形与扇形的绘制进行一个封装

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. MySQL数据库----函数

    函数 MySQL中提供了许多内置函数,例如: CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二字节字符集, LENG ...

  8. python基础七--集合

    12.221.昨日内容回顾 小数据池: int:-5--256 str:1.不能有特殊字符 2.*int不能超过20 编码:所能看到的最小构成单位叫字符 ascii : 8位 1字节 表示1个字符 u ...

  9. 浏览器内核、排版引擎、js引擎

    [定义] 浏览器最重要或者说核心的部分是“Rendering Engine”,可大概译为“渲染引擎”,不过我们一般习惯将之称为“浏览器内核”.负责对网页语法的解释(如标准通用标记语 言下的一个应用HT ...

  10. Linux解压文件到指定目录

    Linux解压文件到指定目录 tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数:-c :create 建立压缩档案的参数:-x : 解压缩压缩 ...