当前位置:网站首页>leetcode-09(下一个排列+快乐数+全排列)
leetcode-09(下一个排列+快乐数+全排列)
2022-07-21 16:23:00 【Fairy要carry】
思路:我们希望下一个数比当前数大,所以我们只需要将后面的大数与前面的小数交换即可,另外,增加的幅度要是最小,满足紧密排列
所以我们需要从右往左进行低位交换,比如 123465,下一个排列应该把 5
和 4
交换而不是把 6
和 4
交换,所以这里思路是先循环找到>低位的低位位置,然后再来个循环从右边遍历,进行交换;
交换完后,还需要考虑低位后的字串情况,可能是个降序的,比如123465
,首先按照上一步,交换 5
和 4
,得到 123564,那么低位后面也就是64明显是个降序,所以需要反转一下
以求 12385764
的下一个排列为例:
首先从后向前查找第一个相邻升序的元素对 (i,j)
。这里 i=4
,j=5
,对应的值为 5
,7
:(这里目的是找到低位位置)
然后在 [j,end)
从后向前查找第一个大于 A[i]
的值 A[k]
。这里 A[i]
是 5
,故 A[k]
是 6
:(从后面再遍历找到大于低位的值)
将 A[i]
与 A[k]
交换。这里交换 5
、6
:
这时 [j,end)
必然是降序,逆置 [j,end)
,使其升序。这里逆置 [7,5,4]
:(需要考虑交换后,低位后面的数是一个降序的数,毕竟小的放后面了)
因此,12385764
的下一个排列就是 12386457
class Solution {
//从后往前找,直至arr[i]>arr[i-1],如果没有找到说明是降序,那么直接返回倒序,否则找到arr[i]>arr[i-1]的地方,将其换一下
public static void nextPermutation(int[] nums) {
boolean flag = true;
int aid = -1;
for (int i = nums.length - 1; i > 0; i--) {
//1.找到后一个比前一个大的,用aid记录前一个状态,找到就说明不是降序
if (nums[i] > nums[i - 1]) {
aid = i - 1;
flag = false;
break;
}
}
//2.根据flag状态进行判断是否为降序,true说明为降序
if (flag) {
reverse(nums, 0, nums.length - 1);
return;
}
//3.因为第一个for是确定低位的,这里的for是确定大于低位的最小值然后进行交换
for (int i = nums.length - 1; i > aid; i--) {
//将遍历过的部分比i-1大的最小值和i-1的位置交换
if(nums[i]>nums[aid]){
int temp=nums[i];
nums[i]=nums[aid];
nums[aid]=temp;
break;
}
}
//4.交换后低位后的值为降序需要反转
reverse(nums,aid+1, nums.length-1);
return;
}
/**
* 反转数组
*/
public static void reverse(int[] nums, int start, int end) {
while (start < end) {
//1.交换
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
//2.缩小范围进一步交换
start++;
end--;
}
}
}
class Solution {
public static boolean isHappy(int n){
HashSet<Integer> set = new HashSet<>();
while (n!=1){
set.add(n);
//1.找到子数
n=getNum(n);
//2.去重
if(set.contains(getNum(n))){
return false;
}
}
return true;
}
/**
* 2.找到一个数的各个位数的和
* @return
*/
public static int getNum(int n){
int num=0;
while(n>0){
int a = n % 10;
num+=a*a;
n/=10;
}
return num;
}
}
class Solution {
//将其分析为DFS,看做树形
//结果集
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>>result=new ArrayList<>();
List<Integer> list=new ArrayList<>();
dfs(result,list,nums);
return result;
}
//首先,自己定义一个dfs方法
private void dfs(List<List<Integer>>result,List<Integer>list,int[] nums){
//结束条件
if(list.size()==nums.length){
result.add(new ArrayList<Integer>(list));
return;
}
//分支操作
for(int num:nums){
//进行分支中放值的一个判断
if(!list.contains(num)){
list.add(num);
//进行分支当中的一个递归
dfs(result,list,nums);
list.remove(list.size()-1);
}
}
}
}
边栏推荐
- 日期类与时间类定义(运算符重载应用)
- 如何让游戏中的随机因素重新赢得玩家信任
- Dynamics crm: how to monitor and manage workflow processes and view their running history
- ROS机械臂 Movelt 学习笔记1 | 基础准备
- 华泰网上开户安全吗?是陷阱吗
- 极客星球丨字节跳动一站式数据治理解决方案及平台架构
- Dynamics crm: deeply analyze the impact of sandbox and asynchronous services on plug-in and workflow in on premise server
- 文件上传下载与Excel、数据表数据之间的转换
- Dynamics CRM: 批量导入数据来更新记录的注意事项
- Definition of feeling
猜你喜欢
MLX90640 红外热成像仪测温模块开发笔记(三)
开发动态 | StoneDB 2022年版本发布里程碑
Dynamics CRM: 批量导入数据来更新记录的注意事项
wallys/new product/DR7915/MT7915+MT7975/WiFi6 MiniPCIe Module 2T2R
看完这个,还不会DVMA,请你吃瓜
Dynamics crm: deeply analyze the impact of sandbox and asynchronous services on plug-in and workflow in on premise server
带你认识8个软件设计中的谬误
How to use document tools for API management?
How to design product MVP to maximize value
还在用 ListView?使用 AnimatedList 让列表元素动起来
随机推荐
How to use document tools for API management?
Dynamics crm: how to monitor and manage workflow processes and view their running history
wallys/new product/DR7915/MT7915+MT7975/WiFi6 MiniPCIe Module 2T2R
【二叉树】验证二叉树
滴滴收80亿罚单 数据安全板块大涨 奇安信午盘涨5.6%
看完这个,还不会DVMA,请你吃瓜
MySQL 分库分表及其平滑扩容方案
How to change the console font to console?
Dynamics CRM: 深度解析本地部署(On-premise)服务器中Sandbox, Asynchronous服务对插件Plug-in和工作流Workflow的影响
使用OpenCv+Arduino实现挂机自动打怪
【案例设计】事件分发器 — 实现跨类的事件响应思路分享与实现
Multithreading and concurrent programming (3)
Notes on formal languages and compilers & tutorial Chapter 2 context free languages
Introduction to 51 single chip microcomputer: LED lights flash at different frequencies
How to upload wechat applet developed by uniapp to wechat applet platform - complete and simple steps
如何让游戏中的随机因素重新赢得玩家信任
web3再牛 也沒能逃出這幾個老巨頭的手掌心
Front line engineers tell you the real status and development prospects of embedded "suggestions collection"
单片机入门:点亮第一个LED小灯
mysql搭建主从同步-手把手使用docker搭建