31. Next Permutation (JAVA)
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 and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
class Solution {
public void nextPermutation(int[] nums) {
int tmp;
int i;
int j;
for(i = nums.length-2; i >= 0; i--){
if(nums[i] < nums[i+1]) break;
} if(i >= 0){
//find the smallest num in the right which is larger than num[i]
for(j = nums.length-1; j >= i; j--){
if(nums[j] > nums[i]) break;
} //swap these two num
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp; //sort
Arrays.sort(nums, i+1, nums.length);
}
else{
Arrays.sort(nums);
}
} }
寻找规律:
- 从右向左扫描,找到小于右侧数字的第一个数字
- 将右侧大于它的最小的那个数放在该位,它和其余右侧的数字从小到大排列放在右侧。
31. Next Permutation (JAVA)的更多相关文章
- 31. Next Permutation (java 字典序生成下一个排列)
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- leetcode 31. Next Permutation JAVA
题目: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数 ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- <LeetCode OJ> 31. Next Permutation
31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...
- 刷题31. Next Permutation
一.题目说明 题目是31. Next Permutation,英文太差看不懂,翻译了一下.才知道是求字典顺序下的下一个排列,不允许使用额外空间.题目难度是Medium! 二.我的实现 首先要进一步理解 ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
随机推荐
- vuex上手文章参考
参考文章: vue-vuex上手
- vue根据参数不同的路由跳转以及name的作用
最近在做VUE路由跳转根据参数的值不同但是跳转的是同一个路由的功能.点击左边的目录,根据目录ID跳转不同的列表.如下图. 路由跳转的代码: this.$router.push({path: '/RFI ...
- vue中下载excel的使用,后端链接两种情况,一个是链接,一个是文件流
vue中下载excel使用 一.这是第一种情况,后台链接地址返回的是一个url,这个时候我只要在导出按钮上绑定exportData()这个事件方法就好了 exportData() { this ...
- Vue/Element-ui 安装搭建开发环境(一)
Element 是饿了么全段开发团队推出的一套基于 vue.js2.0 的 PC Web 端开发框架. Element 中文文档:https://element.eleme.cn/#/zh-CN 1. ...
- 多层全连接神经网络实现minist手写数字分类
import torch import numpy as np import torch.nn as nn from torch.autograd import Variable import tor ...
- 后盾网lavarel视频项目---phpstorm 配置ftp, 自动更新同步代码
后盾网lavarel视频项目---phpstorm 配置ftp, 自动更新同步代码 一.总结 一句话总结: 1.在phpstorm中设置:路径Tools/Deployment/Configuratio ...
- metrics+spring+influxdb
https://www.cnblogs.com/lixyu/p/9337055.html
- RedHat系统文本界面安装图形界面方法
版本: Linux version 2.6.32-431.el6.x86_64 (mockbuild@x86-023.build.eng.bos.redhat.com) (gcc version 4. ...
- gdb调试知识
之前一直不怎么用gdb,现在要用做一下记录 用gdb启动程序 gdb ./demo intel风格反汇编main函数,一共两行第一行设置汇编风格,第二行才是反汇编main函数 set disassem ...
- 数据结构和算法(Java版)快速学习(数组Array)
Java数组 在Java中,数组是用来存放同一种数据类型的集合,注意只能存放同一种数据类型. 用类封装数组实现数据结构 数据结构必须具有以下基本功能: ①.如何插入一条新的数据项 ②.如何寻找某一特定 ...