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 for n = 3:

“123”
“132”
“213”
“231”
“312”
“321”
Given n and k, return the kth permutation sequence.

Note:

Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:

Input: n = 3, k = 3
Output: “213”

给出数字n和k,找出n!的排列组合中第k个排列组合

思路:
可以看到固定首位数字后,还剩下(n-1)个数,也就是说以每个数字开头的排列组合有(n-1)! 个,只需用(k-1)/(n-1)!+1就能确定首位数字,
确定首位数字后,用k % (n-1)! 得到子序列中index的k,
然后每个首位数字对应(n-2)!个组合,用(k-1)/(n-2)!+1确定第二位数字,
这样循环下去,
一共n个数字,首位对应后面的(n-1)!个组合,最后一位对应最后0!个组合,所以循环从(n-1)到0

而且可以看到比如“213”,确认首位2之后,下面要取“13”,“31”两个组合中的一个,比如确认是这个子序列中的第2个(k=2),这时首位数字要取3而不是2,所以可以看出每确定完一个数字,要把该数字从数字表中删除,避免重复利用

    public String getPermutation(int n, int k) {
        String result = "";
        int[] sum = new int[n];
        ArrayList<Integer> index = new ArrayList<>();
        
        Arrays.fill(sum, 1);
        index.add(1);
        
        for(int i = 1; i < n; i++) {
            sum[i] = sum[i-1] * i;
            index.add(i);
        }
        
        index.add(n);
        k --;
        
        for (int i = n-1; i >= 0; i--) {
            int num = k / sum[i] + 1;
            result += index.get(num);
            index.remove(num);
            k = k % sum[i];
        }
        
        return result;
    }
Flash飞鸟逐帧素材 包括11张飞鸟的逐帧png图片,可以直接导入flash 制作逐帧动画,每个图片都与舞台水平、垂直中齐即可。 立即下载

相关推荐

【BAT32G113-仿真器及脱机烧录器教程-A3-2024/9/19】

提示:此壁纸收集于互联网,如有侵权,请联系作者!今日下班,闲来无事,就与生活中在乎的人闲日常,互相彼此增进一些友谊或爱意情愫!在座的朋友看到这里,有时间,抽抽空,多多联系一下,身边重要的人哟!四种指示灯和一个按钮:MODE->代表红色POWER->代表黄色ACTIVE->代表蓝色STATUS->代表XX(不记得了)黑色按钮:按键上电,会进入升级模式,这里我们一般默认不需要改变!仿真工具-ICE8-PRO介绍隶属于产品分支里。烧写工具-CMS-WRITER8 LITE介绍点击。

星新计划的博客 8308

LeetCode——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""213""231""3

三分地 2338

解决linux 2.5G网卡驱动问题

解决linux 2.5G网卡驱动问题 关键词:2.5G网卡驱动,ubuntu,安装,2.5G Ethernet LINUX driver,Realtek网卡驱动 由于2.5G网卡较新,各linux发行版的通用驱动已不适用,新装的系统会出现无法上网问题,由于国内网络环境去Realtek官网很难下载到驱动文件,故本文提供github上的2.5G Ethernet LINUX driver r8125 for kernel up to 5.6的版本驱动文件r8125-9.003.05.tar.bz2各位可以自

poorguy_aos的博客 1万+

Permutation Sequence(LeetCode)

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""312""321"Giv

ChuanjieZhu 216

谈谈人大金仓数据库

人大金仓数据库(Kingbase)是国产自主研发的关系型数据库管理系统,兼容主流数据库语法,支持高并发、高安全性及国产化适配。该数据库提供完整的安装部署方案,包括环境准备、初始化配置和验证流程,并配备专业的数据迁移工具(KDMS)实现从Oracle/MySQL等数据库的平滑迁移。系统提供KStudio等图形化管理工具,支持性能监控、备份恢复等运维功能,同时具备性能调优、高可用架构、分区表管理等高级特性,满足政府、金融等领域的企业级需求。

蜚鸣的博客 2124

基于AIS动态数据与AI结合得经纬度标示算法

港口船舶智能标注系统 本系统通过结合摄像头视频流和AIS船舶动态数据,利用AI技术实现船舶实时标注功能。系统架构包括视频流处理、AIS数据解析、船舶检测匹配和坐标映射转换等模块。关键技术步骤: AIS数据获取:通过本地接收机或网络API获取船舶位置、航速等信息 AI目标检测:采用YOLOv8等模型识别视频中的船舶 船舶匹配算法:通过位置投影或运动轨迹匹配视频目标与AIS数据 坐标映射:使用相机标定技术将经纬度转换为图像坐标 实时标注:在视频画面上叠加船名、位置等关键信息 应用场景包括港口监控、智能海事管理和

qianbo042311的博客 1836

LeetCode-permutation-sequence

The set[1,2,3,…,n]contains a total ofn! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, forn= 3): "123" "132" "213" "231" "31...

六月二十七的博客 277

足球赛事数据API:开发者生存指南(附真实踩坑记录)

作为一个既爱看球又爱写代码的老宅男,我这些年用过的足球API比看过的比赛还多。从最初被各种API文档折磨到怀疑人生,到现在能闭着眼睛调接口,中间的血泪史够写本书了。今天就把这些经验(和教训)分享给各位同好,特别是那些正在为"老板要做个足球App"而秃头的同行们。文档清晰免费层可用响应格式规范API-Sports免费额度大方支持GraphQL有中文文档(难得!Sportmonks数据全面有Webhook支持按需付费。

Happy0970的博客 2221

[leetcode]60. Permutation Sequence排列序列

The set[1,2,3,...,n]contains a total ofn! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence forn= 3: "123" "132" "213" "231" "31...

weixin_30614109的博客 119

手把手教你绕过Incapsula reese84防护:从JS反混淆到动态Token生成全流程

本文深入解析了以Incapsula reese84为代表的现代Web客户端挑战防护机制,详细介绍了从JS反混淆、算法逆向到动态Token生成的全流程实战方法。文章系统阐述了如何通过模拟浏览器指纹、移植关键算法或嵌入JS引擎来构建自动化应对策略,并提供了稳健的令牌管理与系统架构设计思路,帮助开发者在合规前提下提升自动化工具的适应性。

jenkins8butler的博客 526

OpenGL 进阶系列10 - 光线跟踪实现简介(Ray Tracing)

OpenGL 实现光线跟踪(Ray Tracing)是一个相对复杂但有趣的挑战,通常用于生成高度逼真的图像效果,如反射、折射、阴影等。OpenGL 本身并不是为光线跟踪设计的,但可以通过结合计算着色器和现代 OpenGL 功能实现光线跟踪。光线跟踪的基本思路是从相机(或视点)出发,发射光线并计算这些光线与场景中物体的交点。光线与场景中物体相交的计算。计算交点的光照(基于表面法线、光源位置、材质等)。反射和折射的处理(如果物体是透明或反射的)。

GPU全栈博主 1544

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 921

SAP S4 HANA:按单生产(MTO)场景中的COGS分割

本文介绍了SAP S/4HANA中提供的各种选项,以在销售订单/商品是按订单生产(以下称为MTO)生产流程中的成本对象时,在基于帐户的COPA环境中报告COGS分割信息。

adam915的博客 1199

Leetcode #60. Permutation Sequence 排列组合序列 解题报告

1 解题思想这道题是要求第K个全排列组合的序列,什么意思呢,对于一个1…n的数字,我们一共会有n!个排列组合的序列,那么我们现在按照字典序为顺序,要求给出第K个序列。传统的做法呢,是使用字典序的方式进行排列,迭代K次,但是这道题很明显会超时。所以这道题使用了一个方式,lehmer码。相关的lehmer介绍请先看这里:http://www.keithschwarz.com/interesting/co

MebiuW的专栏 1813

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Leetcode算法——60、排列序列(permutation sequence)

集合 [1,2,3,…,n] 共有 n! 个唯一的排列。 将所有排列方法按照升序排序,可以得到一个序列。 比如 n=3 时的序列为: “123” “132” “213” “231” “312” “321” 要求给定 n 和 k,返回序列中第 k 个排列。 备注: n 的范围为 1~9 k 的范围为 1~9! 示例: Example 1: Input: n = 3, k = 3 Output: "2...

HappyRocking的专栏 1141

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

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

爱胡椒的小白兔呢 570

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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""213""...

weixin_34419321的博客 238

LeetCode 60. Permutation Sequence(N排列,第K序列)

题目描述:    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""213""2...

andersonxie的博客 373

leetcode_60. Permutation Sequence 找n的全排列中的第k个序列

题目: 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""21

qqzj_bupt的博客 561

[C++][STL]next_permutation字典排列使用

字典顺序排列的问题也经常碰到,现在习惯直接使用next_permutation函数,你要我返回第几个我就返回第几个。 对应的,next_permutation(start,end)和prev_permutation(start,end)。这两个函数作用是一样的,区别就在于前者求的是当前排列的下一个排列,后一个求的是当前排列的上一个排列。 以Leetcode60.Permutation Sequ...

ChessC的博客 257

[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

数学问题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(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] 60. Permutation Sequence 序列排序

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

weixin_30552811的博客 183
上一篇: leetcode 150. Evaluate Reverse Polish Notation(逆波兰法)
下一篇: leetcode 216. Combination Sum III(数字的和III)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址