Leetcode 670.最大交换
最大交换
给定一个非负整数,你至多可以交换一次数字中的任意两位。返回你能得到的最大值。
示例 1 :
输入: 2736
输出: 7236
解释: 交换数字2和数字7。
示例 2 :
输入: 9973
输出: 9973
解释: 不需要交换。
注意:
- 给定数字的范围是 [0, 108]
思路
- class Solution {
- public int maximumSwap(int num) {
- char[] A = Integer.toString(num).toCharArray();
- int[] last = new int[10];
- for (int i = 0; i < A.length; i++) {
- last[A[i] - '0'] = i;
- }
- for (int i = 0; i < A.length; i++) {
- for (int d = 9; d > A[i] - '0'; d--) {
- if (last[d] > i) {
- char tmp = A[i];
- A[i] = A[last[d]];
- A[last[d]] = tmp;
- return Integer.valueOf(new String(A));
- }
- }
- }
- return num;
- }
- }
Leetcode 670.最大交换的更多相关文章
- Java实现 LeetCode 670 最大交换(暴力)
670. 最大交换 给定一个非负整数,你至多可以交换一次数字中的任意两位.返回你能得到的最大值. 示例 1 : 输入: 2736 输出: 7236 解释: 交换数字2和数字7. 示例 2 : 输入: ...
- [LeetCode] 670. Maximum Swap 最大置换
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- LeetCode:627.交换工资
题目链接:https://leetcode-cn.com/problems/swap-salary/ 题目 给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值.交换所有的 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
- LeetCode刷题总结-数学篇
本文总结LeetCode上有数学类的算法题,推荐刷题总数为40道.具体考点分析如下图: 1.基本运算问题 题号:29. 两数相除,难度中等 题号:166. 分数到小数,难度中等 题号:372. 超级次 ...
- [LeetCode] Minimum Swaps To Make Sequences Increasing 使得序列递增的最小交换
We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...
- [LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of ...
随机推荐
- 2017.9.26 request请求参数用法
4.2 访问请求参数 request对象的getParamter()方法,可以用来获取用户(客户端)提交的数据 4.2.1 访问请求参数的方法 String 自符串变量 =request.getPar ...
- CentOS7 设置开机自启
[root@master-1 ~]# systemctl enable mariadb ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/sy ...
- 高亮代码 SyntaxHighlighter
SyntaxHighlighter: http://alexgorbatchev.com/SyntaxHighlighter/download/ demo <!DOCTYPE html PUBL ...
- 开源项目托管github步骤
一.在github新建项目,复制到本地更改之后命令提交. 1.进入github主页新建项目:https://github.com/ccyinghua 2.复制项目地址 3.打开git Bash 命令行 ...
- 1、SpringBoot+MybatisPlus整合
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- iOS 多线程 之 GCD(大中枢派发)(二)
本文接着上一篇讲.主要讲:dispatch_source. dispatch_source主要用户监听事件,可以监听如下事件 DISPATCH_SOURCE_TYPE_DATA_ADD DISPATC ...
- forEach、for...in、for...of
forEach 数组实例的遍历方法 const arr=['red', 'green', 'blue']; arr.forEach(function(element, index) { console ...
- java动态返回一个大对象里多个小对象map返回,el表达式用c:set拼接
基于堆内存,先把map放到返回值里 Map _map=new HashMap(); modelAndView.addObject("pledgeInsurance",_map);/ ...
- JavaScript对象回收机制
js维护了一张对象引用表: 当一个对象被创建以后,栈内就有一个a,a这个对象就指向了对这个地址,当a=new Person()执行后,引用次数加1.当a=null置空,引用次数减1.由系统来维护对象引 ...
- laravel环境配置的常见问题
从开始下载相关软件到现在,整整一天,终于成功了.不得不说官方的说明文档相当详细,毕竟我都成功了,不是吗,哈哈. 好了,不多说了,直接上干货 官方环境配置文档地址:https://laravel-chi ...