670. 最大交换

给定一个非负整数,你至多可以交换一次数字中的任意两位。返回你能得到的最大值。

示例 1 :

输入: 2736

输出: 7236

解释: 交换数字2和数字7。

示例 2 :

输入: 9973

输出: 9973

解释: 不需要交换。

注意:

给定数字的范围是 [0, 108]

class Solution {
public int maximumSwap(int num) {
if(num<10) return num;
int[] nums = new int[9];
int size=0;
while(num !=0){
nums[size++] = num%10;
num = num/10;
} for(int tmp = size-1;tmp>=0;tmp--){
int max = -1;
int point = -1;
for(int j = tmp-1;j>=0;j--){
if(nums[j] >= max){
max = nums[j];
point = j;
}
}
if(max > nums[tmp]){
swap(nums, point, tmp);
break;
}
} int sum = 0;
for(int i =size-1;i>=0;i--){
sum = sum*10 + nums[i];
}
return sum;
}
private static void swap (int[] arr, int l, int r) {
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
}

Java实现 LeetCode 670 最大交换(暴力)的更多相关文章

  1. Leetcode 670.最大交换

    最大交换 给定一个非负整数,你至多可以交换一次数字中的任意两位.返回你能得到的最大值. 示例 1 : 输入: 2736 输出: 7236 解释: 交换数字2和数字7. 示例 2 : 输入: 9973 ...

  2. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. 【Hadoop离线基础总结】Yarn集群的资源调度

    Yarn集群的资源调度 介绍 概述 Yarn是 Hadoop 2.x 引入的新的资源管理系统模块,主要用于管理集群当中的资源(主要是服务器的各种硬件资源,比如内存.CPU等),它不光管理硬件资源,还管 ...

  2. Qt 操作sql server数据库

    添加qtsql的库 连接数据库 QSqlDatabase_db = QSqlDatabase::addDatabase("QODBC"); _db.setHostName(); _ ...

  3. linux(ubuntu) 1045, "Access denied for user 'root'@'localhost' (using password: YES)"

    问题现象: 最近使用 flask 的 sqlalchemy 框架,在链接数据库(mysql)时出现报错 sqlalchemy.exc.OperationalError: (pymysql.err.Op ...

  4. c++内存管理学习纲要

    本系列文章,主要是学习c++内存管理这一块的学习笔记. 时间:6.7-21 之下以技术内幕的开头语,带入到学习C++内存管理的技术中吧: 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题 ...

  5. angular js 页面添加数据保存数据库

    一.编写实体类Controller层返回数据使用 package entity; import java.io.Serializable; public class Result implements ...

  6. 一文解读C# 动态拦截第三方进程中的方法函数(外挂必备)

    一.前言 由于项目需要,最近研究了一下跨进程通讯改写第三方程序中的方法(运行中),把自己程序中的目标方法直接覆盖第三方程序中的方法函数:一直没有头绪,通过搜索引擎找了一大堆解决方案,资料甚是稀少,最后 ...

  7. Angular 初体验

    事情起源当初一个简单的截屏然后推流出去的工具,这个工具当初我用winform简单实现了下,然后因公司业余,添加许多程序包,需要自动管理这些程序包,包含下载更新上传等,以及与后台交互,学生老师提醒,自动 ...

  8. Postman学习宝典(二)

    文章来源于:米阳MeYoung Postman 入门2 - Script.Runner 上次Postman 入门1 我们介绍全局变量和环境变量时已经使用过Tests 和 pre-request scr ...

  9. Jenkins 实现 ldap认证

    使用自己搭建的openldap: 使用Test LdapSetting测试的结果: 所测试的用户在:svn,jenkins,gitlab,sonarqube,wpsadmin组下 若用户不在jenki ...

  10. javascript 获取页面的高度及滚动条的位置的代码

    http://www.jb51.net/article/23331.htm javascript 获取页面的高度及滚动条的位置的代码 作者: 字体:[增加 减小] 类型:转载   javascript ...