Java for LeetCode 031 Next Permutation
Next Permutation
Total Accepted: 33595 Total Submissions: 134095
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
解题思路:
题目不难,关键是理解题目的意思: 如果把nums看做一个数字的话,即返回比原来数大的最小的数(如果没有(原数是降序排列)则返回升序排列)。
也就是字典排序,JAVA实现如下:
static public void nextPermutation(int[] nums) {
int index = nums.length - 1;
while (index >= 1) {
if (nums[index] > nums[index - 1]) {
int swapNum=nums[index-1],swapIndex = index+1;
while (swapIndex <= nums.length - 1&& swapNum < nums[swapIndex])
swapIndex++;
nums[index-1]=nums[swapIndex-1];
nums[swapIndex-1]=swapNum;
reverse(nums,index);
return;
}
index--;
}
reverse(nums,0);
}
static void reverse(int[] nums,int swapIndex){
int[] swap=new int[nums.length-swapIndex];
for(int i=0;i<swap.length;i++)
swap[i]=nums[nums.length-1-i];
for(int i=0;i<swap.length;i++)
nums[swapIndex+i]=swap[i];
}
Java for LeetCode 031 Next Permutation的更多相关文章
- 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 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 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 ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- javaScript基础练习题-下拉框制作(JQuery)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HDU 5497 Inversion
Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description You have ...
- ARP协议格式、ARP运行机制入门学习
相关学习资料 http://baike.baidu.com/view/149421.htm?fromtitle=ARP%E5%8D%8F%E8%AE%AE&fromid=1742212& ...
- jsp学习(四)
JavaBean是一个可重复使用的软件组件,是遵循一定标准.用java语言编写的一个类,该类的一个实例称为一个JavaBean,简称bean. 它必须符合如下规范:1.必须有一个零参数的默认构造函数2 ...
- jsp学习一
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1&q ...
- TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133
程序代码 class Person: #constructor def __init__(self,name,sex): self.Name = name ...
- 平衡二叉树,AVL树之图解篇
学习过了二叉查找树,想必大家有遇到一个问题.例如,将一个数组{1,2,3,4}依次插入树的时候,形成了图1的情况.有建立树与没建立树对于数据的增删查改已经没有了任何帮助,反而增添了维护的成本.而只有建 ...
- 清空file input框
测试环境:OS --> winXPBrowsers --> IE6+, FF 3.0.11, FF 3.5, Opera 9.64, Opera 10 beta 2, Safari 4, ...
- 保存知乎收藏夹功能的NodeJS版本
前两天发现知乎收藏夹中的答案正在不断减少..看来需要保存一下了,但之前别人的方式是用chrome插件(浏览器无法自动保存本地文件)+wget前后端配合来完成这个工作的,而且还有一些缺点(比如保存的ht ...
- [整理]Web应用安全学习
最近在复习TCP/IP相关知识,同时也想对网络安全相关的知识进行系统的补漏. 阅读了一些常见的关于XSS.CSRF等的一些网上文章: http://www.cnblogs.com/luminji/ca ...