当前位置:网站首页>Topic34——31. Next spread
Topic34——31. Next spread
2022-06-21 08:43:00 【_ Cabbage_】
subject : An integer array array Is to arrange all its members in sequence or linear order .
for example ,arr = [1,2,3] , The following can be regarded as arr Permutation :[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] .
Integer array Next spread It refers to the next lexicographic order of its integers . More formally , Arrange all the containers in the order from small to large , So the array of Next spread It is the arrangement behind it in this ordered container . If there is no next larger arrangement , Then the array must be rearranged to the lowest order in the dictionary ( namely , Its elements are arranged in ascending order ).
for example ,arr = [1,2,3] The next line up for is [1,3,2] .
Similarly ,arr = [2,3,1] The next line up for is [3,1,2] .
and arr = [3,2,1] The next line up for is [1,2,3] , because [3,2,1] There is no greater order of dictionaries .
Give you an array of integers nums , find nums The next permutation of .
must In situ modify , Only additional constant spaces are allowed .
Example 1:
Input :nums = [1,2,3]
Output :[1,3,2]
Example 2:
Input :nums = [3,2,1]
Output :[1,2,3]
Example 3:
Input :nums = [1,1,5]
Output :[1,5,1]
Tips :
1 <= nums.length <= 100
0 <= nums[i] <= 100
class Solution {
public void nextPermutation(int[] nums) {
int l;
int r;
for(int i = nums.length - 1; i > 0; i--) {
if(i == 1 && nums[i] < nums[i - 1]) {
l = 0;
r = nums.length - 1;
while(l <= r) {
swap(nums, l, r);
l++;
r--;
}
break;
}
if(nums[i] > nums[i - 1]) {
for(int j = nums.length - 1; j >= i; j--) {
if(nums[j] > nums[i - 1]) {
swap(nums, j, i - 1);
l = i;
r = nums.length - 1;
while(l <= r) {
swap(nums, l, r);
l++;
r--;
}
break;
}
}
break;
}
}
}
public void swap(int[] nums, int a, int b) {
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
}
边栏推荐
- Given a two-dimensional list of m*n, find out whether a number exists
- Difference between function declaration and function expression
- For hand drawn graphics, covering multiple topics, CVPR 2022 sketchdl workshop begins to solicit contributions!
- 南京理工大学MOOC慕课:程序设计基础(Ⅰ)第8章测试选择题答案及解析
- 给定一个m*n的二维列表,查找一个数是否存在
- STL教程2-MyArray框架实现
- Kotlin---- detailed explanation of data types
- GQL+Nodejs+MySQL数据库
- leetcode:打印两个有序链表的公共部分
- 2.19 simulation summary
猜你喜欢

解密FTP

For hand drawn graphics, covering multiple topics, CVPR 2022 sketchdl workshop begins to solicit contributions!

5分钟搞懂MySQL - 行转列

STL tutorial 3- type conversion static_ cast、dynamic_ cast、const_ cast、reinterpret_ Cast method

Markdown rule for writing articles

leetcode:19. 删除链表的倒数第 N 个结点

Redis master-slave vulnerability and remote connection vulnerability

Character function and string function

Understanding and use of advanced pointer

Using elastic stack to analyze Olympic data (II)
随机推荐
Idea common code templates
Unity .net 框架问题
4.4 Eval function replaces function
Qsort sort string
Topic34——31. 下一个排列
adb使用技巧和usb通信原理
How to build a deep learning framework?
5分钟搞懂MySQL - 行转列
Linux Installation of Damon database /dm8 (with client tool installation full version)
26. Hikvision camera configuration and preliminary test
STL tutorial 2-myarray framework implementation
【JUC系列】Executor框架之CompletionService
An aunt's towel holds up the 100 billion market behind 400million Chinese women
Tidb3.0- 4.0 memory control / modification log saving days / maximum index length
[DB written interview 274] in Oracle, what is deferred segment creation?
Unity write multithreading considerations
C # implement callback
Unmanned, automation technology affects the world
优化食品生产行业库存管理的6种方法
sql查看数据库/表磁盘占用情况,杀死进程终止tidb中的连接