题目限定输入是[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. [SoapUI] 比较两个不同环境下XML格式的Response, 结果不同时设置Test Step的执行状态为失败

    import org.custommonkey.xmlunit.* def responseTP=context.expand( '${Intraday Table_TP#Response}' ) d ...

  2. Django框架 之 信号

    Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. Django内置信号 Model signals pre_init ...

  3. Microsoft(C)注册服务器(32位)CPU占用高

    Microsoft(C)注册服务器(32位)CPU占用高 摘自:https://blog.csdn.net/jtsqrj/article/details/83034252 2018年10月12日 23 ...

  4. HttpUploader6-queue版本更新说明

    HttpUploader6-queue版本更新说明 博客园:http://www.cnblogs.com/xproer/p/5109761.html 网易博客:http://hyhyo.blog.16 ...

  5. ASP.NET JSON数据转实体类方式

    实体类 public class FlieList { public string file_unid { get; set; } public string file_name { get; set ...

  6. emacs-ide配置

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CEDET Configuration ;;;;;;;;;; ...

  7. jmeter阶梯式加压测试

    转自:https://www.cnblogs.com/imyalost/p/7658816.html#4226560 性能测试中,有时需要模拟一种实际生产中经常出现的情况,即:从某个值开始不断增加压力 ...

  8. vue环境中生成二维码

    <template><div><div id='code'></div><canvas id="canvas">< ...

  9. 群晖Synology

    简介 群晖是做的最好的一家NAS公司,声称是买软件卖硬件,软件有丰富的功能. 白群晖就是指从正规渠道买软件+硬件,价格昂贵,性价比低. 黑群晖是指自己搭建或购买单独的硬件(可以是电脑主机.可以是其他厂 ...

  10. C# Winform Label内容根据其宽度自动调整字体大小

    C# Winform Label内容根据其宽度自动调整字体大小 项目,有个要求,Label中显示的内容,能够根据其宽度自动调整字体的大小进行显示,刚刚开始的时候,总是想着通过不同的方法来直接测量内容的 ...