博客
关于我
670. 最大交换
阅读量:534 次
发布时间:2019-03-08

本文共 915 字,大约阅读时间需要 3 分钟。

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

示例 1 :

输入: 2736输出: 7236解释: 交换数字2和数字7。

示例 2 :

输入: 9973输出: 9973解释: 不需要交换。

注意:

  1. 给定数字的范围是 [0, 108]
package Solution670;import java.util.Arrays;class Solution {	public int maximumSwap(int num) {		int[] numarr = Integer.toString(num).chars().map(c -> c - '0').toArray();		l: for (int i = 0; i < numarr.length; i++) {			int[] numarrcopy = Arrays.copyOfRange(numarr, i, numarr.length);			Arrays.sort(numarrcopy);			int max = numarrcopy[numarrcopy.length - 1];			for (int j = numarr.length - 1; j > i; j--) {				if (max > numarr[i]) {					if (numarr[j] == max) {						int temp = numarr[i];						numarr[i] = numarr[j];						numarr[j] = temp;						break l;					}				}			}		}		int out = 0;		for (int a : numarr) {			out = 10 * out + a;		}		return out;	}	public static void main(String[] args) {		Solution sol = new Solution();		int num = 1993;		System.out.println(sol.maximumSwap(num));	}}

 

转载地址:http://zmniz.baihongyu.com/

你可能感兴趣的文章
nacos config
查看>>
NacosClient客户端搭建,微服务注册进nacos
查看>>
Nacos原理
查看>>
Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
查看>>
Nacos如何实现Raft算法与Raft协议原理详解
查看>>
Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
nacos注册失败,Feign调用失败,feign无法注入成我们的bean对象
查看>>
Nacos编译报错NacosException: endpoint is blank
查看>>
NACOS部署,微服务框架之NACOS-单机、集群方式部署
查看>>
Nacos配置中心集群原理及源码分析
查看>>
nacos配置自动刷新源码解析
查看>>
Nacos集群搭建
查看>>
nacos集群搭建
查看>>
nagios安装文档
查看>>
name_save matlab
查看>>
Nami 项目使用教程
查看>>
NAT-DDNS内网穿透技术,解决动态域名解析难题
查看>>
NativePHP:使用PHP构建跨平台桌面应用的新框架
查看>>
NAT技术
查看>>
NAT模式下虚拟机centOs和主机ping不通解决方法
查看>>