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 制作逐帧动画,每个图片都与舞台水平、垂直中齐即可。 立即下载

相关推荐

ubuntu18.04 显卡驱动问题 造成屏幕分辨变大 NVIDIA-SMI has failed because it couldn't communicate with ....等问题

问题1:装好了显卡驱动,cudnn,tensorflow等没问题,一直可以用。但是突然有一天不行了,终端输入:nvidia-smi 显示:NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed...

梦萦飞--CV/DL/ML 2248

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

三分地 2335

N32在Keil中J-Link识别失败的排查与修复指南

本文详细解析了在Keil开发环境中,J-Link调试器无法识别N32系列MCU的常见问题。通过分步指导,从检查修改Keil工程配置文件、更新J-Link驱动,到手动添加N32专用设备支持包,系统性地提供了排查与修复方案,帮助开发者快速恢复调试连接,确保开发流程顺畅。

307

LeetCode Permutation Sequence

LeetCode解题之Permutation Sequence原题找出由[1,2,3…n]中所有数字组成的序列中第k大的。

DRFish 1056

汇丰外包Java岗面试复盘:Zoom面1.5小时,从英语、粤语到现场敲代码的极限挑战

本文详细记录了汇丰银行外包Java岗位的面试全过程,包括多语言切换(英语、粤语)的技术沟通挑战和现场编码测试的实战经验。从高并发编程到JVM调优,面试不仅考察技术深度,更注重实战问题解决能力。文章分享了应对跨国银行技术面试的实用策略,帮助Java开发者在跨文化环境中提升竞争力。

weixin_42524824的博客 363

小程序HTTPS证书配置踩坑记:为什么Android访问失败而iOS正常?

本文深入剖析了小程序开发中常见的跨平台网络问题:Android访问HTTPS接口失败而iOS正常。核心原因在于服务器(如Nginx)HTTPS证书链配置不完整,Android系统对此校验严格,而iOS相对宽松。文章提供了从问题诊断、使用在线工具检测到修复Nginx证书链配置的完整解决方案,并总结了HTTPS部署的最佳实践,帮助开发者从根本上避免此类“玄学”故障。

weixin_29207149的博客 396

[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

开关电源环路学习笔记(1)-为啥要知道传递函数?

做硬件都会接触到开关电源,或多或少会接触到环路补偿。很多兄弟们也提出来让我说说这个,说明大家也都很关心,也说明这个好像有点难。 就我个人而言,我也想搞明白环路这一块,也查了一些资料,视频,不过说实话,一直都还是懵的,感觉很困难。 具体感觉就是,看资料或视频的时候,感觉好像是那么回事。但是撇开之后,就又云里雾里的感觉,总有一些想法没有找到答案。 问题 比如下面我想过的问题: 开关电源有开关,根本就不是一个线性系统,传递函数是咋弄出来的? 系统环路框图的输入量为什么不是Vin,而是参考电压V

weixin_42005993的博客 1万+

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 920

LoRa项目实战:手把手教你为ESP32选配和焊接天线(从PCB到信号测试)

本文详细介绍了为ESP32选配和焊接LoRa天线的全流程,包括天线选型、PCB布局设计、焊接工艺及信号测试。重点解析了PCB板载天线、导线天线和陶瓷贴片天线的适用场景与性能特点,并提供专业的焊接技巧和信号测试方案,帮助开发者优化LoRa模块的无线通信性能。

weixin_30470643的博客 396

文献阅读笔记:量子微波测量技术综述

量子微波测量技术研究进展:量子微波测量技术利用里德堡原子与微波电场的相干相互作用,突破了传统测量方法的物理限制。该技术基于电磁诱导透明效应和里德堡原子能级跃迁原理,通过激光激发原子并监测光谱变化来测量微波电场强度。相比传统方法,量子微波测量具有超高灵敏度(理论极限可达nV/cm)、宽动态范围(超过100dB)、小型化等显著优势,且测量结果可直接溯源至普朗克常数。典型实验系统包括光源、原子气室和探测器,采用光谱法、干涉法等多种测量方法。近年来,该技术在基础研究和应用方面取得重要进展,展现出在通信、雷达、计量等

Grow into a more wonderful you 991

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

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

MebiuW的专栏 1809

Open3D (C++) 泊松重建【2025最新版】

使用C++版本的Open3D实现点云的泊松重建

点云侠的博客 3077

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的专栏 1133

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

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的博客 236

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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的博客 372

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、付费专栏及课程。

余额充值