题目限定输入是[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. Spring MVC 配置文件

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  2. U盘安装RedHat linux 5.3

    U盘安装RedHat linux 5.3 1.下载rhel-5.3-server-i386-dvd.iso文件: 2.下载绿色版UltraISO软件: 3.将rhel-5.3-server-i386- ...

  3. 编写高质量代码改善C#程序的157个建议——建议71:区分异步和多线程应用场景

    建议71:区分异步和多线程应用场景 初学者有时候会将异步和多线程混为一谈.如果对它们之间的区别不是很清楚,很容易写出下面这样的代码: private void buttonGetPage_Click( ...

  4. 长时间停留在calculating requirements and dependencies

    如果安装插件的时候,Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 )这个问题通常就是在点击安装之后显示“Calcu ...

  5. 【小梅哥FPGA进阶教程】第十一章 四通道幅频相可调DDS信号发生器

    十一.四通道幅频相可调DDS信号发生器 本文由山东大学研友袁卓贡献,特此感谢 实验目标 实现多通道可调信号发生器 实验平台 芯航线FPGA核心板.ADDA模块 实验现象 实现基于FPGA的多通道可调信 ...

  6. ZOJ3705:Applications

    Recently, the ACM/ICPC team of Marjar University decided to choose some new members from freshmen to ...

  7. Android-SDCard外部存储文件读写

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses ...

  8. selenium+jenkins+maven+testNG搭建持续集成环境

    为了简明起见,分几大部分,很基础的细节就不详述了 一·安装jenkins 二·创建一个maven项目的job 2.1   填上SVN的Repository URL 2.2  由于是在本地执行maven ...

  9. ASP.NET MVC 缓存页面(方法)

  10. asp.net——Ajax与ashx异步请求的简单案例

    Ajax与ashx异步请求的简单案例: 前台页面(aspx): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...