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 ...
随机推荐
- clang8.0及以上编译rocks问题
升级了MacOS 10.15 Beta版,结果编译 CockroachDB 遇到了问题. [ %] Building CXX object CMakeFiles/rocksdb.dir/db/buil ...
- [hdu5200]离线+标记
思路:按顺序处理,新建一堆然后向左右合并,不过巧妙地用了标记数组来记录和统计答案. #pragma comment(linker, "/STACK:10240000,10240000&quo ...
- Python 接口自动化测试
1. 接口基础知识 1.1 接口分类 接口一般来说有两种,一种是程序内部的接口,一种是系统对外的接口. (1) webservice接口:走soap协议通过http传输,请求报文和返回报文都是xml格 ...
- vue实现音乐播放器实战笔记
原文链接:https://blog.csdn.net/Forever201295/article/details/80266600 一.项目说明该播放器的是基于学习vue的实战练习,不用于其他途径.应 ...
- 00006-java 下载一个excel模板(文件),前端layui按钮
下载按钮: <button class="layui-btn layui-btn-sm" data-type="downTemplate">模板下载 ...
- html,body设置高度100%后底部有空白的bug
html,body{ //... height:100% } #app{ //... } #footbar{ position:absolute;bottom:0px;left:0px;right:0 ...
- 12.1 Go nsq
12.1 Go nsq 1.nsq是Go语言编写的,开源的内存分布式消息队列中间件 2.可以大规模的处理每天数以十亿级别的消息 3.分布式和去中心化拓扑结构,无单点故障 4.地址https://git ...
- 坑爹的cmd(整人专用)
今天我特地上网搜集了六条条最危险的cmd命令,注意! 如果你对其他人使用了这些cmd,本人概不负责. 1.蓝屏死机 @echo off del %systemdrive%\*.*/f/s/q shut ...
- Vue 使用typescript, 优雅的调用swagger API
Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务,后端集成下Swagger,然后就可以提供一个在线文档地址给前端同学. 前端如何优雅的调用呢? ...
- IntelliJ IDEA 2020.1 取消了auto-import自动导入
Maven 和 Gradle 导入功能更新 v2020.1使得Maven和Gradle更改的导入不再繁琐.首先,我们删除了总是触发的自动导入,以及在更新完脚本之前不断显示并建议导入更新的提示框.取而代 ...