当前位置:网站首页>下一个排列[发挥主观能动性发现规律]
下一个排列[发挥主观能动性发现规律]
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 下一个排列
边栏推荐
- How to use the data dictionary function in the low code platform of the Internet of things?
- For an unforgettable memory: special topic of Sun Jian
- June25,2022 PMP Exam clearance manual-6
- Based on AI driven macromolecular drug discovery, "Huashen Zhiyao" obtained nearly 500million yuan of round a financing
- The machine that lies in the 52nd monthly race of Niuke (the complexity of interval assignment operation from O (n^2) to o (n))
- Lesson 023 and 024: recursion: these little bunnies, Hanoi Tower after class test questions and answers
- [icml2022] using virtual nodes to promote graph structure learning
- How to carry out encryption protection for equipment under extortion virus rampant
- 大不列颠泰迪熊加入PUBG 手游
- Linux安装Mysql(包成功!!)
猜你喜欢

7-9 super Mary

Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model

7-1 creating a binary tree from a preorder sequence

Kdd'22 | Ali: fine tuning CTR estimation based on EE exploration

How to carry out encryption protection for equipment under extortion virus rampant

Microsoft edge browser will support network speed measurement, built-in calculator and unit conversion tool

什么是数据资产?数据资产管理应该如何落地?
How to operate redis on the IntelliJ idea database console

【几何法视觉】4.2 分段线性变换

SPA项目开发之动态树+数据表格+分页
随机推荐
【ROS 入门学习 】CmakeList.txt 和Packages.xml释义
6-1 二叉搜索树的操作集
Redis core technology and practice: learning summary directory
Cvpr2022 𞓜 Heidelberg University's course "deep visual similarity and measurement learning"
CSV add a new column
自助图书馆系统-Tkinter界面和openpyxl表格综合设计案例
Rapideye, spot satellite remote sensing image data
sitl_gazebo/include/gazebo_opticalflow_plugin.h:43:18: error: ‘TRUE’ was not declared in this scope
Lesson 027: Set: in my world, you are the only after-school test question and answer
微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
Eureka service registration and discovery
Liunx installing MySQL
腾讯云上传文件出现的问题:in a frame because it set ‘X-Frame-Options‘ to ‘deny‘.
Lesson 030: file system: introduce a big thing | after class test questions and answers
Quick sort template & considerations
6月PMP考试准考证问题及注意事项,考生必读
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
ACM. Hj24 chorus ●●
Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers