当前位置:网站首页>下一个排列[发挥主观能动性发现规律]
下一个排列[发挥主观能动性发现规律]
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 下一个排列
边栏推荐
- Android kotlin SP DP to PX
- SPA项目开发之登录注册
- 勒索病毒横行下设备该如何进行加密防护
- [Li mu] how to read papers [intensive reading of papers]
- Optimization solver | gurobi's Mvar class: a sharp tool for matrix modeling and an alternative solution to dual problems (with detailed cases and codes attached)
- [mavros] mavros startup Guide
- [interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV
- [GWCTF 2019]mypassword XSS
- Microsoft edge browser will support network speed measurement, built-in calculator and unit conversion tool
- [icml2022] using virtual nodes to promote graph structure learning
猜你喜欢

Redis usage scenario sharing (project practice)
![[geometric vision] 4.2 piecewise linear transformation](/img/1e/a810f4d7e9a6a34647b5cb56fdde67.png)
[geometric vision] 4.2 piecewise linear transformation

June 25 PMI certification examination site epidemic prevention requirements and examination room arrangement

In 2022, the "product innovation and achievement transformation" training camp of Chaoyang District Science and technology innovation class was successfully completed
![[interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV](/img/a5/bfa011c961b8f05348f9905fe3d996.png)
[interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV

RapidEye快鸟、SPOT卫星遥感影像数据

Share deadlock problems encountered in insert into select (project practice)

CYCA少儿形体礼仪 深圳市培训成果考核圆满落幕

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

For an unforgettable memory: special topic of Sun Jian
随机推荐
Shell (34): Time
Self service library system Tkinter interface and openpyxl form comprehensive design case
Analysis of fegin
【持续更新中...】2021年全国大学生电子设计大赛 (三)匿名四轴拓空者飞控系统设计解读
(duc/ddc) digital up mixing / quadrature down mixing principle and MATLAB simulation
ACM. Hj24 chorus ●●
[GWCTF 2019]mypassword XSS
Es total number of data queried by criteria
Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use
Based on AI driven macromolecular drug discovery, "Huashen Zhiyao" obtained nearly 500million yuan of round a financing
SPA项目开发之CRUD+表单验证
Oracle数据库中文字符串和英文字符串的截取不同
【李沐】 如何读论文【论文精读】
二级造价工程师考前必备15个知识点来了!祝你旗开得胜!
[interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV
Lesson 022: function: recursion is god horse after class test questions and answers
[chess life] 01 life is like chess
CVPR2022 | 用于重采图像的特征解耦学习与动态融合
RealNetworks vs. Microsoft: the battle in the early streaming media industry
NiO copy file call getchannel method transferfrom()