题目限定输入是[0, 10^8],因而不用考虑负数或者越界情况,算是减小了难度。

 public class Solution {
/**
* @param num: a non-negative intege
* @return: the maximum valued number
*/
public int maximumSwap(int num) {
// Write your code here
char[] numArrays = String.valueOf(num).toCharArray();
if (numArrays.length < 2) {
return num;
} //第一个递增点:
int firstHeigerPoint = 0;
for (int i = 0; i < numArrays.length - 1; i++) {
if (numArrays[i] < numArrays[i + 1]) {
firstHeigerPoint = i + 1;
break;
}
} if (firstHeigerPoint == 0) {
return num;
} //找到递增点后的最大值(如有相等情况取低位,因而更新条件是numArrays[j] >= max)
int max = numArrays[firstHeigerPoint];
int maxPoint = firstHeigerPoint;
for (int j = firstHeigerPoint; j < numArrays.length; j++) {
if (numArrays[j] >= max) {
max = numArrays[j];
maxPoint = j;
}
} //找到第一个更小的高位
int toSwapIndex = firstHeigerPoint-1;
for (int i = 0; i <= firstHeigerPoint - 1; i++) {
if (numArrays[i] < max) {
toSwapIndex = i;
break;
}
} char temp = numArrays[toSwapIndex];
numArrays[toSwapIndex] = numArrays[maxPoint];
numArrays[maxPoint] = temp; return Integer.parseInt(new String(numArrays));
}
}

1095. Maximum Swap —— Weekly Challenge的更多相关文章

  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] Maximum Swap 最大置换

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

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

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

  4. Maximum Swap LT670

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

  5. 670. Maximum Swap

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

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

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

  7. LeetCode Maximum Swap

    原题链接在这里:https://leetcode.com/problems/maximum-swap/description/ 题目: Given a non-negative integer, yo ...

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

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

  9. 最大交换 Maximum Swap

    2018-07-28 16:52:20 问题描述: 问题求解: 使用bucket数组来记录每个数最后出现的位置,然后从左向右遍历一遍即可. public int maximumSwap(int num ...

随机推荐

  1. VC6.0 中 添加/取消 块注释的Macro代码

    SAMPLE.DSM是微软提供的样例,使用的是vb语言.其中的 CommentOut 函数,是支持块注释的,可是这种/**/的注释方式,有时候用起来不是很方便,因为两个/会因为一个/而终止.对于大块代 ...

  2. MySQL 存储过程和存储函数 的定义条件和处理程序

    #三.定义条件和处理程序 #mysql 存储函数和过程可以将错误 变成我们想要的处理结果分2步骤 #1.定义错误条件 语法:DECLARE 条件名字 CONDITION for 错误编码 #2. 定义 ...

  3. HDU 1111 Secret Code (DFS)

    题目链接 题意 : 给你复数X的Xr和Xi,B的Br和Bi,让你求一个数列,使得X = a0 + a1B + a2B2 + ...+ anBn,X=Xr+i*Xi,B=Br+Bi*i : 思路 : 首 ...

  4. 用word2013发布csdn博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  5. Tomcat配置文件与启动顺序

    三个配置应用的位置: 1.conf目录下的server.xml文件:此方式为Eclipse默认配置方法,同时也是三种方式中优先级最高的. <?xml version="1.0" ...

  6. 前端jq设置下拉框的,单选框,复选框的帖子

    $(function(){ var sex=$("#sex").val(); var marriageStatus=$("#marriageStatus").v ...

  7. [LeetCode 题解]: Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. Oracle定時email通知

    small_program_task 這張表的資料是待發送的email通知,再次之前已經有一個job會定時掃描固定時間內未接收到小程式回報狀態將其寫入到該表,send_flag為N,表示為寄過通知.e ...

  9. docker中安装Jenkins

    1.获取Jenkins镜像(不要直接docker pull jenkin,这样获取的不是最新的版本,后续安装部分插件会不成功) docker pull jenkins/jenkins 创建文件夹 su ...

  10. ASP.NETMVC4 分页组合查询解决方法

    本人新手刚在webform转到mvc   像linq  ef啥的,都是不会的不行不行的,不会就问群友,找资料 今天本屌遇到了一个分页组合查询的问题,解决了2个小时,把代码共享给大家 话不多话,直接上代 ...