LeetCode 60. Permutation Sequence

编写用户帮助/操作手册指南 一个新系统,需要写用户操作手册,该从何下笔?本篇是一篇教你编写用户帮助/操作手册的指南~ 阅读详情

题目链接

60. Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

解:回溯法稳稳的,但是有几个n=8的样例超时了,坚持用回溯法,想到先通过k的区间判断出第一个字符,然后开始回溯,这样避免了超时。不过根据这个,想到可以直接通过区间依次判断出每一位了,于是放弃了回溯法,毕竟此题没有要输出全部的序列,下面讲非回溯的方法:

1.首先让k是从0开始的数,k--

2.根据规律可以看出当 n = 3 第一个字符,为‘1’,k区间应该是(0, 2!],‘2’对应区间(2!, 2*2!] ...

3.根据2中规律得到第一个字符,如果得到的是‘1’,那么就要k不变,若得到‘2’,k -= 2! (减去1开头的那些字符串) ... 

4.用新的k值和区间继续算后面的字符,其中还要维持一个vector,装可用的字符(按顺序),算出的字符将其从vector中拿出,后面的字符只从vector中选

代码:

class Solution {
public:
    string getPermutation(int n, int k) {
        int limit;
        vector<char> valid;
        string result = "";
        for (int i = 0; i < n; i++) {
            char a = '0' + i + 1;
            valid.push_back(a);
        }
        k--;
        while (n) {
            limit = 1;
            for (int i = 2; i < n; i++) limit *= i;
            int i;
            for (i = 0; i < n; i++) if (k >= limit*i && k < limit*(i+1)) break;
            char a = valid[i];
            std::vector<char>::iterator it = valid.begin();
            k -= limit*i;
            while (i--) it++;
            valid.erase(it);
            result += a;
            n--;
        }
        return result;
    }
};

回溯代码(超时):

class Solution {
public:
    string find(string now, int n, int k, bool *status, int &count) {
        if (now.length() == n) {
            count++;
            if (count == k) return now;
            else return "";
        }
        for (int i = 0; i < n; i++) {
            if (status[i]) {
                status[i] = false;
                char a = '0'+i+1;
                string result = find(now+a, n, k, status, count);
                if (result != "") return result;
                status[i] = true;
            }
        }
        return "";
    }
    string getPermutation(int n, int k) {
        bool *status = new bool[n];
        for (int i = 0; i < n; i++)
            status[i] = true;
        int count = 0;
        return find("", n, k, status, count); 
    }
};

回溯法(解决超时):

class Solution {
public:
    string find(string now, int n, int &k, bool *status) {
        if (now.length() == n) {
            k--;
            if (k == 0) return now;
            else return "";
        }
        for (int i = 0; i < n; i++) {
            if (status[i]) {
                status[i] = false;
                char a = '0'+i+1;
                string result = find(now+a, n, k, status);
                if (result != "") return result;
                status[i] = true;
            }
        }
        return "";
    }
    string getPermutation(int n, int k) {
        bool *status = new bool[n];
        for (int i = 0; i < n; i++)
            status[i] = true;
        int limit = 1;
        for (int i = 2; i < n; i++) limit *= i;
        int i = 0;
        for (i = 0; i < n; i++) if (k > limit*i && k <= limit*(i+1)) break;
        char a = '0'+i+1;
        status[i] = false;
        k -= limit*i;
        string str = "";
        return find(str + a, n, k, status); 
    }
};



AI 大模型接口太零散?自建私有模型管理平台统一整合,管理使用超方便!(支持飞牛nas、群晖nas等服务器快速部署) 面对多种 AI 大模型接口难统一、管理繁琐的问题🤯,本文分享一款超实用开源网关工具⚡,可聚合各大模型、自动转接口、支持可视化管理,Docker 一键部署,大幅降低开发与维护成本🚀 阅读详情

相关推荐

Jetpack Compose Navigation实战:从基础到高级导航模式

本文全面解析Jetpack Compose Navigation在Android开发中的应用,从基础导航实现到高级模式(如单任务/单顶模式),涵盖参数传递、嵌套导航、深层链接等核心功能。通过实战代码示例展示如何优化导航性能,并与ViewModel、Hilt等架构组件整合,为开发者提供高效的Compose导航解决方案。

pz890123456的博客 372

【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

【LeetCode】60. Permutation Sequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/permutation-...

负雪明烛 3883

从Blender舞步到夜空画卷:基于Pix飞控的无人机编队灯光秀全链路开发实践

本文详细介绍了基于Pixhawk飞控的无人机编队灯光秀全链路开发实践。从Blender三维动画设计、QGroundControl地面站二次开发与MAVLink通信桥接,到PX4飞控的OFFBOARD模式路径控制与LED灯光驱动实现,最后通过Gazebo仿真验证并完成真机组装与首飞。文章为无人机爱好者提供了一套完整的、从创意设计到夜空执行的开源解决方案,重点阐述了多机同步与灯光控制的核心技术挑战与应对策略。

zeta9的博客 585

60. Permutation Sequence -Medium

Question The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): 1. “123”

Euadvancer的博客 478

FineReport SQL查询分页

打开模板>模板web属性>分页预览设置,选择为该模板单独设置,将默认的工具栏按钮全部清除掉,然后依次添加两个自定义按钮,一个邮件按钮,两个自定义按钮,一个Word格式,一个PDF按钮。如下所示:注:使用具体格式按钮,如邮件、Word格式、PDF按钮是为了JS直接使用控件名称获取空间方便修改其内容;

lep的博客 2427

毕设分享|基于16单片机的指纹识别电子密码锁设计

我们今天的工作生活中,很多的场合都需要身份的认证,而传统的基于标志的身份认证技术由于受到证件伪造以及密码破解等手段的威胁,逐渐表现得有些力不从心。怎样保护自己的重要文件资料,如何保证自己的隐私不被泄露?在传统的身份认证中,我们往往使用密码加密法,但是这种方法只是"防君子不防小人"。在高明的黑客眼里,由几个字符组成的密码脆弱得不堪一击。现在,科技的发展让我们有了新的选择——生物识别技术。将生物识别技术应用于笔记本、门锁等方面,可以对文件、财产起保护作用,并且可以进行身份识别。

Sen_091的博客 1021

LeetCode60. Permutation Sequence

题目链接: https://leetcode.com/problems/permutation-sequence/ 题目描述:给一个集合[1,2….n]元素1~n,对它们进行从小到大顺序排列,找出第k个排列结果。思路:做不来啊,找不到规律。看了http://blog.csdn.net/doc_sgl/article/details/12840715 才有点感觉。 这道题其实是康托逆展开。{1

_今已亭亭如盖矣 771

STM32H743必要外围电路分析

写在前面:这篇博客是我第一次绘制STM32最小系统板的过程记录。 2020-1-11 BOOT STM32采用ARM内核,和ARM处理器一样,都有专门的boot脚决定单片机从何处启动。 在官方数据手册的第105页,我们可以看到 系统复位后,在SYSTICK的第四个上升沿锁存BOOT引脚的值,复位后,BOOT引脚可以由用户自由配置而不会影响系统正常运行。 BOOT引脚决定了自举存储器地址,当BO...

Cui_Hongwei的博客 1万+

LeetCode-60-Permutation Sequence(找规律)-Medium

题意理解: 将0-n按照从小到大的顺序进行全排序,求解第k个排列结果 题目分析: 穷举会超时,找规律, 1)第k个排列的第一个元素在0-n中的位置为(k-1)/(n-1)! 2)在剩下的元素中继续找第一个; 3)依此类推; 在Java中使用String一直会超时,最终使用StringBuilder; 解题代码: public class Solution

eddy_liu的专栏 2983

STC15单片机实战项目软件开发第四讲 - 按键检测(状态机)

软件开发第四讲 - 按键检测(状态机) 一、工具链接 1、Keil c51 UV4 https://yunpan.360.cn/surl_yrIfYYmeRFk 2、STC-ISP下载软件 https://yunpan.360.cn/surl_yxH4CuUUvI3 二、学习板获取方式 方法1:自己设计PCB,自行打板焊接; 方法2:用群主设计的PCB,自行打板焊接; 方法3:向群主购买,淘宝链接...

硬件家园 3045

使用virtuoso gds转lef操作方法

2) 然后,需要创建Pin,这里Pin的概念不同于label/text,是有实际金属的,二者的区别类似于pr中的port和terminal。1) 首先,需要画prBoundary,这个用drawing层的prBoundary画是不行的,不过建议pr阶段就把prBoundary转到gds里,在virtuoso layout中画prBoundary就比较容易。另外,用Create - Create Pin也可以创建pin,更适用于PG pin,操作时先给pg shape打label,最好放正中间。

m0_61544122的博客 1379

LeetCode 60. Permutation Sequence(排列序列)

原题网址:https://leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the

James Pan 919

抖音小程序:解决获取手机号报错 getPhoneNumber:fail auth deny,登录注册时需要获取用户手机号,getPhoneNumber一直提示错误(Uniapp抖音小程序报错也能解决)

抖音字节小程序获取手机号报错getPhoneNumber:fail auth deny,抖音 小程序 获取手机号 报错 getPhoneNumber:fail auth ,抖音小程序获取手机号始终返回拒绝,抖音小程序获取手机号,已经过了试运营期但依旧报错如何解决,调用 tt.getPhoneNumber 接口报错,登录注册时需要获取用户手机号,getPhoneNumber一直提示getPhoneNumber:fail auth deny,此能力仅支持小程序通过试运营期后可用,默认获取权限,无需申请;unia

✅ 精选推荐文章 4304

LeetCode | # 60. Permutation Sequence

60. Permutation Sequence Description 描述:https://leetcode.com/problems/permutation-sequence/description/ 题意:给定1~n这n个数,返回第k个排列。 Solution: (Java) class Solution { public String getPermutation(int n, ...

Karmen199x的博客 275

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

[leetcode] 60. Permutation Sequence

Description The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence for n = 3: “123” “132” “213” “231” ...

Learning from the mistakes 255

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

[LeetCode]60. Permutation Sequence

60. Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):"123"

Johnkui技术博客 616

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Leetcode 60. Permutation Sequence 第k个排列

Leetcode 60. Permutation Sequence 第k个排列 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/permutation-sequence/comments/82926 题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。 按大小顺序列出所有排列情况,并一一标记,当&nb...

CoderWangSon 257

LeetCode:60. Permutation Sequence,全排列的第n个子列

LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode: 60. Permutation Sequence 描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the pe

yelin's namespace 414

数学问题Leetcode 60 Permutation Sequence

Leetcode 60 Permutation Sequence Problem Description: 返回给定n个数的第k个序列(按字典序排列) 具体的题目信息: https://leetcode.com/problems/permutation-sequence/description/ Example: Solution: class Solution { publ...

萤火虫啊飞呀飞 217

leetcode 60. Permutation Sequence

康托公式 全排列

Cherryuuuu 843

python写算法题:leetcode: 60. Permutation Sequence

class Solution(object): def permutation(self, narray, k, p): if len(narray)<=1: return narray ind=k/p rem=k%p p/=len(narray)-1 ret=[narray[...

262

【数字全排列】LeetCode 60. Permutation Sequence

LeetCode 60. Permutation Sequence 博客转载自:http://www.cnblogs.com/grandyang/p/4358678.html 【年轻人要多学习才是啊~】 【自己想?这辈子都想不出这么好的思路。。。只有抄别人的了。。。】 这道题是让求出n个数字的第k个排列组合,由于其特殊性,我们不用将所有的排列组合的情况都求出来,然后返回其第k个,我们可以只...

Allenlzcoder的博客 330

LeetCode60——Permutation Sequence

LeetCode60——Permutation Sequence 又是全排列的问题。 循环调用next_permutation()这个方法测试过超时。 这个题参考了:http://blog.csdn.net/linhuanmars/article/details/22028697 简单的说一下。 对于1,2,3这样的序列来说,假设要求它的第5个全排列是3,1,2 我们采取这种办

上善若水 731

LeetCode:60. Permutation Sequence,n全排列的第k个子列

LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the perm...

dbb191379的博客 226

【leetcode】【60】Permutation Sequence

一、问题描述 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123""132"

斗code、年华 1181

【LeetCode】60.Permutation Sequence(medium)解题报告

【LeetCode】60.Permutation Sequence(medium)解题报告题目地址:https://leetcode.com/problems/permutation-sequence/description/ 题目描述:   The set [1,2,3,…,n] contains a total of n! unique permutations.   By list

郝春雨的博客 204
上一篇: LeetCode 236. Lowest Common Ancestor of a Binary Tree--递归
下一篇: LeetCode 112. Path Sum--递归
monkey_rose
博客等级 码龄10年 9粉丝 55原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值