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 最大交换(暴力)的更多相关文章

  1. Leetcode 670.最大交换

    最大交换 给定一个非负整数,你至多可以交换一次数字中的任意两位.返回你能得到的最大值. 示例 1 : 输入: 2736 输出: 7236 解释: 交换数字2和数字7. 示例 2 : 输入: 9973 ...

  2. 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 ...

  3. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. 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 ...

  5. 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. ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【Hadoop离线基础总结】linux的shell编程

    linux的shell编程 基本了解 概述 Shell是一个用C语言编写的程序,通过shell用户可以访问操作系统内核服务,它类似于DOS下的command和后来的cmd.exe.Shell既是一种命 ...

  2. FreeAnchor:抛弃单一的IoU匹配,更自由的anchor匹配方法 | NIPS 2019

    论文抛弃以往根据IoU硬性指定anchor和GT匹配关系的方法,提出FreeAnchor方法来进行更自由的匹配,该方法将目标检测的训练定义为最大似然估计(MLE)过程,端到端地同时学习目标分类.目标检 ...

  3. js代码中引入其他js文件

    /***引入 js 文件 @example: import('js/aui.picker.js') @example: import(['js/aui.picker.js', 'css/aui.pic ...

  4. HDU 2004 (水)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2004 题目大意: 给你成绩让你根据成绩打分解题思路: 简单的if...else 应用 需要注意是,if ...

  5. XSS挑战之旅(通过看代码解题)

    XSS 挑战之旅 level 1 没有什么过滤 payload: <script>alert(1)</script> level 2 php关键代码: echo "& ...

  6. HttpServletRequest 和 HttpServletResponse详解

    用HttpServletRequest,现在整理如下,以便以后查阅 请求与响应相关的类和接口非常多,下表是主要的与请求和接口相关的类以及接口. 主要的与请求和接口相关的类及接口 方    法 说    ...

  7. CentOS 7.1 图形化安装

    1.在命令行下输入下面的命令来安装 Gnome 包 sudo  yum groupinstall "GNOME Desktop" "Graphical Administr ...

  8. .NET 程序员的 Playground :LINQPad

    如果想执行一个简单的 C# 语句并获得运行结果,通常我们需要做几个步骤才能达成: 打开 Visual Studio 并新建一个控制台项目. 在 Program.cs 中编写代码并保存. 点击运行按钮或 ...

  9. 【雕爷学编程】Arduino动手做(5)---热敏温度传感器模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器模块,依照实践(动手试试)出真知的理念,以学习和交流为目的,这里准备逐 ...

  10. python之Python内置函数一览表

    Python 解释器自带的函数叫做内置函数,这些函数可以直接使用,不需要导入某个模块. 如果你熟悉 Shell 编程,了解什么是 Shell 内置命令,那么你也很容易理解什么是 Python 内置函数 ...