当前位置:网站首页>下一个排列[发挥主观能动性发现规律]
下一个排列[发挥主观能动性发现规律]
2022-06-22 20:33:00 【REN_林森】
前言
有时候抛弃套路(共性),发挥主观能动性,找到题的个性所在,挖掘其特点,见招拆招,才能解掉一个题。
一、下一个排列

二、逻辑分析与代码整理
package everyday.medium;
// 下一个排列
public class NextPermutation {
/* target:把nums当作一个数,取下一个数。从后往前找,找到一对数,左 < 右,然后从后面的单调递增子数组交换一个稍大的数,然后翻转后面的单调递增子数组。 如果遍历完都没找到,那就是特殊情况,直接翻转。 */
public void nextPermutation(int[] nums) {
if (nums.length == 1) return;
int i = nums.length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) --i;
int begin = 0;//翻转开始处,默认全部翻转。
// 当i >= 0时,需要确定新的翻转开始处。
if (i >= 0) {
// 交换以获取下一个序列
// 寻找需要交换的数字
int idx = nums.length - 1;
while (nums[idx] <= nums[i]) idx--;
// 交换两者,且 i + 1 到 nums.length - 1保持单调递减,直接翻转。
int t = nums[i];
nums[i] = nums[idx];
nums[idx] = t;
// 确定从那里开始翻转
begin = i + 1;
}
// 翻转
for (int j = 0; j < nums.length - begin >>> 1; j++) {
int t = nums[begin + j];
nums[begin + j] = nums[nums.length - 1 - j];
nums[nums.length - 1 - j] = t;
}
}
}
总结
1)发挥主观能动性/思考力,发现问题个性,按逻辑写出代码,再整理逻辑代码。
参考文献
[1] LeetCode 下一个排列
边栏推荐
- Vs code one key sorting shortcut
- IDC releases China Data Governance Report Yixin Huachen No. 1
- Lesson 022: function: recursion is god horse after class test questions and answers
- Ten thousand words long text | use RBAC to restrict access to kubernetes resources
- NIO复制文件调用getChannel方法 transferFrom()
- 卸载mavros
- Android kotlin SP DP to PX
- Quick sort template & considerations
- Cvpr2022 𞓜 Heidelberg University's course "deep visual similarity and measurement learning"
- Why do you think it is common for Chinese people to earn more than 10000 yuan a month?
猜你喜欢
![[path planning] week 1: hodgepodge](/img/8b/d7c370b0deac33c41a72f8105ea357.png)
[path planning] week 1: hodgepodge

腾讯云上传文件出现的问题:in a frame because it set ‘X-Frame-Options‘ to ‘deny‘.

How to use the data dictionary function in the low code platform of the Internet of things?

自助圖書館系統-Tkinter界面和openpyxl錶格綜合設計案例

pycharm 配置远程连接服务器开发环境

Cannot re register id: pommeffacompetition-v0 problem solving

Lesson 030: file system: introduce a big thing | after class test questions and answers

Implementation of depth traversal adjacency matrix of figure 6-5
![[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design](/img/5d/71c75a3622f7814f385d04b6148074.png)
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design

7-9 超级玛丽
随机推荐
6月25日PMI认证考点防疫要求及考场安排
Database summary: common problems and Optimization in MySQL development
2022年6月25日PMP考试通关宝典-6
LeetCode#20. Valid parentheses
Système de bibliothèque libre - service - cas de conception complète de l'interface tkinter et du formulaire openpyxl
IDC publie le rapport sur la gouvernance des données en Chine
The interception of Chinese and English strings in Oracle database is different
Self service library system Tkinter interface and openpyxl form comprehensive design case
6-1 operation set of binary search tree
Shell (34): time
【路径规划】第一周: 路径规划 开源代码汇总(ROS) 版本
shell(34) : 时间
大不列颠泰迪熊加入PUBG 手游
IDC發布中國數據治理報告 億信華辰第一
如何在物联网低代码平台中使用数据字典功能?
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
Makefile:1860: recipe for target ‘cmake_ check_ build_ system‘ failed make: *** [cmake_check_build_syst
Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
Lesson 029: Documents: a task? After class test questions and answers