题目限定输入是[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. [Token] 从index.jsp中获取Token

    import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def holde ...

  2. Spring Boot☞ 使用Thymeleaf模板引擎渲染web视图

    静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /s ...

  3. spring boot☞Swagger2文档构建及单元测试

    首先,回顾并详细说明一下在快速入门中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

  4. [GO]非结构体匿名字段

    package main import ( "fmt" ) type mystr string //给一个类型重命名 type Person struct { name strin ...

  5. Spring bean管理器 bean实例化的三种方式

    bean实例化的三种方式实现 第一种:使用类的无参数构造方法创建(常用 重要) 第一种实例化方式最常用,实例化类时会通过调用无参构造方法创建.示例代码如下: package spring.com.Us ...

  6. Digester学习笔记(二)转载

    为便于理解,将笔记的内容结构作了一些调整. 对象栈 对digester技术最普通的应用,是用来动态创建一个由Java对象构成的树结构,各对象的属性以及对象间的关系,基于XML文档的内容来设置(XML文 ...

  7. POJ 3581 Sequence(后缀数组)

    Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to ...

  8. PreTranslateMessage()函数捕获键盘按键消息

    01. PreTranslateMessage函数,常用于屏蔽MFC对话框中默认的Enter和ESC消息 函数原型:BOOL PreTranslateMessage(MSG* pMsg) 用法举例: ...

  9. 快速搭建windows服务器的可视化运维环境

    开发好的程序部署在服务器上,如何对服务器的基本指标进行监控呢?最近对一套工具进行了研究,可以快速搭建服务器监管环境,很是强大,最重要的是它还很酷炫. 原理:数据采集+时序数据库+可视化,下面记录一下搭 ...

  10. NVIDIA GTC照片

    这次的NVIDIA GPU Technology Conference 2014在美国San Jose(就是俗称的硅谷了)举办. 去美国的飞机上10个小时反正是睡不着,电影还是挺好看的. 美联航的早餐 ...