Java实现 LeetCode 670 最大交换(暴力)
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 最大交换(暴力)的更多相关文章
- Leetcode 670.最大交换
最大交换 给定一个非负整数,你至多可以交换一次数字中的任意两位.返回你能得到的最大值. 示例 1 : 输入: 2736 输出: 7236 解释: 交换数字2和数字7. 示例 2 : 输入: 9973 ...
- 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 ...
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【Scala】看代码,初步了解Apply方法
class ApplyTest(val name:String) { /** * apply源码 * def apply(x: Int, xs: Int*): Array[Int] = { * val ...
- SAP登录消息提醒
1功能说明 在相应用户登录时,给其提示相关信息. 2功能实现 2.1函数实现 在函数NAVIGATION_SET_START_TCODE中注册要监听的用户和程序的事务代码,当用户登录时,将自动运行 ...
- qt获取指定目录下符合条件的文件路径
1)设置名称过滤器 QDir * dir = new QDir(路径); QStringList filter; Filter << QStringLiteral(“筛选的文件条件,如.x ...
- Coda docs
a doc,反过来就是coda,有点冷. 对我而言,在线文档的好处在于在线数据库.存放代码等. Quip能高亮代码,但有时墙内不香,害得我用APP查看记录.Slite能高亮代码,但表格功能弱.号称al ...
- [UVA Live 12931 Common Area]扫描线
题意:判断两个多边形是否有面积大于0的公共部分 思路:扫描线基础. #pragma comment(linker, "/STACK:10240000") #include < ...
- [hdu5360]贪心
题意:一个人想邀请n个人出去玩,假设当前同意和他一起去的人数为cnt,那么他去邀请i的时候,i同意的条件是L[i]<=cnt<=R[i],L[i],R[i]是给定的,问怎样安排邀请顺序,使 ...
- Linux,Unix,GNU 到底有什么样的渊源?
Linux,Unix, GNU,你可能经常听到这些名字被放在一起,比如 “Linux是类Unix系统”, “Linux其实应该叫 GNU/Linux” 等等.为什么会有这些说法,这些名词的历史渊源和背 ...
- 数据库中取出YYYY-mm-dd H:i:s的数据怎么将其转化成YYYY/mm/dd格式,另外,怎么将一个数据表中的数据插入另一个数据表
sql语句是select left(replace(rq,'-','/'),10) as rq from 表名 tp5.1中的写法 $res = Db::table('表名') ->field ...
- git init 后关联github仓库是发生错误:
: failed to push some refs to 'git@github.com:AlanKnightly/reactC.git'hint: Updates were rejected be ...
- React:Composition
在日常的UI构建中,经常会遇到一种情况:组件本身更多是作为一个容器,它所包含的内容可能是动态的.未预先定义的.这时候它的内容取决另一个组件或外部的输入.比如弹层. props.children: Re ...