题目限定输入是[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. TF Boys (TensorFlow Boys ) 养成记(四):TensorFlow 简易 CIFAR10 分类网络

    前面基本上把 TensorFlow 的在图像处理上的基础知识介绍完了,下面我们就用 TensorFlow 来搭建一个分类 cifar10 的神经网络. 首先准备数据: cifar10 的数据集共有 6 ...

  2. es学习-索引管理

    1.创建索引 http://localhost:9200/suoyinguanli211/ 参数: { "settings":{ "index":{ ,分片数 ...

  3. exp,expdb,imp,impdb的使用

    1.使用expdp要先在数据库中创建directory,并给相应的用户read,write权限. SQL>create dexp和empdp的区别irectory dmpdir as ‘/u01 ...

  4. ThinkPhp数据缓存技术

    1.缓存初始化 在 ThinkPHP 中,有一个专门处理缓存的类:Cache.class.php(在Thinkphp/Library/Think/cache.class.php,其他的各种缓存类也在这 ...

  5. ConsoleAppender

    http://logback.qos.ch/manual/appenders.html#ConsoleAppender <configuration> <appender name= ...

  6. 怎么用谷歌浏览器查看请求或响应HTTP头?

    要使用谷歌浏览器查看请求或响应HTTP标头,可以采取以下步骤: 在Chrome浏览器,访问一个网址,点击右键,选择检查,打开开发人员工具(或直接按F12). 选择 Network 选项卡. 重新加载页 ...

  7. tomcat 6.x + log4j日志配置并按天(或大小)生成文件

      tomcat日志,默认路径在${catalina.home}/logs目录下,默认使用的是tomcat自己封装的logging工具类,默认配置文件使用的${catalina.home}/conf/ ...

  8. [LeetCode 题解]: LetterCombinations

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. Replication--使用备份初始化订阅--请求订阅

    1. 修改发布属性"许从备份文件初始化"置为TRUE 脚本修改:USE [DB01]GODECLARE @publication AS sysnameSET @publicatio ...

  10. c# Quartz.net的简单封装

    分享一个以前封装的Quartz.net类. 新建一个QuartzClass类库项目.nuget控制台输入 image.png 添加Quartz.net的引用. 我们新建一个JobBase.cs文件,里 ...