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 ...
随机推荐
- 转载-git使用之忽略不需要上传的文件的几种方式
在我们使用git 的时候通常会遇到一些问题,一些文件我创建了但是我并不想上传或者有些文件我修改了但是并不想上传(为了适应个自己的开发环境),但是在每次git status的时候总能看到它,不仅感到很心 ...
- pthon-安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示“This application failed to start because no Qt platform plugin could be initialized.Reinstalling the application the application may fix this program”
最近学习python,安装网上教程一步一步的安装,网上很多帖子都写的非常详细,不由深深感慨多谢各位不辞辛苦的记录,指导着来自新入门的同学. 但是实际安装中,最理想莫过于一次性安装成功,但自己安装就出现 ...
- Java设计模式之建造者模式(Builder Pattern)
前言 这篇文章主要向大家讲解什么是建造者模式,建造者模式的实例讲解及应用场景等知识点. 一.建造者介绍 用户可以不知道产品的构建细节直接可以创建复杂的对象,主要是分离了产品的构建和装配,这样就实现 ...
- react 动态渲染echarts折线图,鼠标放大缩小
//折线图组件import React,{Component} from 'react'; import ReactEcharts from 'echarts-for-react'; class Ec ...
- Win32 Sdk 连接Access数据库
/************************************************************* *** MyWinClass.cpp 创建窗口模板 *** vs2017+ ...
- dTree
1.dtree.js源码 /*--------------------------------------------------| | dTree 2.05 | www.destroydrop.co ...
- 存储层技术:JDBC、Hibernate、Mybatis三者之间的比较学习
JDBC Hibernate Mybatis (Java DataBase Connnection) 是通过JAVA访问数据库 对JDBC的轻量封装 像操作对象操作数据库 对SQL的轻 ...
- JS最通俗易懂简易轮播实现
轮播图作为前端比较简易的动画,使用非常频繁,这里记录以便使用 此轮播图为最简易自动播放,非无缝,无按钮,无缩略图和序号 想看全套轮播图可以查看我的分类轮播图全套 html 布局 <div sty ...
- 不懂代码?没关系,照样可以做SaaS软件开发
众所周知,一家标准化的企业的日常运营管理都需要一个强大的中枢或中台管理系统来统筹整个企业或是整个集团的运作,这个强大的中台管理系统就相当于是企业的引擎.在引擎的带动下,汽车可以快速的飞驰起来,同样,在 ...
- day20 函数闭包与装饰器
装饰器:本质就是函数,功能是为其他函数添加新功能 原则: 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数添加新功能后,不修改被修饰函数的调用方式 装饰器的知识储备: 装饰器=高阶函数+ ...