1.4字串:60.Permutation Sequence(Leetocde)
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"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
題目大意:給定n和k,返回在n的數的所有組合中第k個數的組合方式,字串形式返回;
又是排列組合的題,這些題已經困擾了我很久了,找時間對演算法技巧做一下總結,這道題我完成的還不錯O(N)的執行時間O(1)的空間複雜度(不開闢額外空間,我還沒學空間複雜度)
先貼程式碼:
因為我分析要用到很多的刪除新增步驟,所以我建了一個連結串列來儲存n個數據public String getPermutation(int n, int k) { List<Integer> list = new LinkedList<>(); for(int i = 1;i <= n;i ++) list.add(i); int i = 0; k --;//因為第一個數不做變換 n --;//對於n = 1來講只有一個組合 while(k != 0){ int num1 = k / factorial(n);//高位腳標 list.add(i,list.remove(num1 + i)); k = k % factorial(n); n--; i ++; } StringBuilder result = new StringBuilder(); for(Integer it : list){ result.append(it); } return result.toString(); } public int factorial(int n){ int result = 1; for(int i = 1;i <= n;i ++) result *= i; return result; }
思路:要清楚第k個數是在哪一個最高位覆蓋範圍之內,比如n = 3,k = 4則k是在2高位覆蓋範圍內然後依次類推求出每一位的變換情況。注意k和n的變化即可。
相關推薦
1.4字串:60.Permutation Sequence(Leetocde)
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 fo
【LeetCode】60. Permutation Sequence(C++)
地址:https://leetcode.com/problems/permutation-sequence/ 題目: The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and la
60. Permutation Sequence(求全排列的第k個排列)
self. while enc n-1 span all list clu post The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling a
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 f
spring boot 2.1.4 hibernate二級快取 Hazelcast實現(二)
在(一)中我們配置好了 hibernate二級快取 Hazelcast實現,但是當我們使用spring cache相關注
1.4 資料庫和常用SQL語句(正文)——MySQL資料庫命令和SQL語句
前面我們已經講述了,登入時,我們使用mysql –u root –p命令進行,此時如果設定了密碼,則需要輸入密碼。 輸入密碼後即進入MySQL的操作介面,此時,命令列窗體左側顯示“mysql>”表示此時可接受mysql命令。 (1)列出全部資料庫命令 我們使用“show databases;”命令列
LeetCode 60. 第k個排列 Permutation Sequence(C語言)
題目描述: 給出集合[1,2,3,…,n]其所有元素共有 n! 種排列。 按大小順序列出所有排列情況,並一一標記,當 n = 3 時, 所有排列如下: “123” “132” “213” “231” “312” “321” 給定 n
python實操(1):搜尋指定目錄(檔案)下包含指定字串的檔案,及行號
#!/usr/bin/python #encoding:utf-8 # Filename : findString.py #author by :morespeech #python2.7 #platform:pycharm import os import re c
POJ - 2785 :4 Values whose Sum is 0 (二分)
num pro ive con res per cin 查找 ati The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute h
程序員的自我救贖---1.4.3: 核心框架講解(MVC)
登錄頁面 就會 技術 virtual mon status pan gpu aac 《前言》 《目錄》 (一) Winner2.0 框架基礎分析 (二) 短信中心 (三)SSO單點登錄 (四)PLSQL報表系統 (五)錢包系統 (六)GPU支付中心 (七)權
2018-4-25 18周1次課 分發系統-expect講解(上)
expect20.27 分發系統介紹由於業務叠代更新,需要更改代碼,如果機器很多,那麽久需要一個分發系統,可以把每段時間更新的代碼分別發布到機器上去分發系統就是上線的shell腳本,核心為expectexpect是一種腳本語言,和shell很像,可以用它去實現傳輸文件和遠程執行命令,不需要去輸入密碼20.28
60. Permutation Sequence
bre set string owin solution order 記錄 amp color The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling
2018 Multi-University Training Contest 1 hdu 6299 Balanced Sequence(貪心)
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=6299 題意:n個'('和 ')'和串,任意排列的拼接中最長的子序列,滿足括號匹配的最大長度 做法:把每個串做一個匹配,最後會有 1. 只包含’(’ 2
#Leetcode# 60. Permutation Sequence
https://leetcode.com/problems/permutation-sequence/ The set [1,2,3,...,n] contains a total of n! unique permutations. By l
岡薩雷斯:數字影象處理(三):第三章灰度變換與空間濾波(1)——基本灰度變換函式
一、前言 空間域指影象平面本身。這類影象處理方法直接以影象中的畫素操作為基礎。這是相對於變換域中的影象處理而言的。變換域的影象處理首先把一幅影象變換到變換域,在變換域中進行處理,然後通過反變換把處理結果返回到空間域 空間域處理主要分為灰度變換和空間濾波兩類。 灰度變換在影象的單個畫素上操
第015講:字串:格式化 | 學習記錄(小甲魚零基礎入門學習Python)
(標答出處: 魚C論壇) 《零基礎入門學習Python》 根據說明填寫相應的字串格式化符號 符 號 說 明 %c 格式化字元及其ASCII碼 %s 格式化字串 %d 格式化整數 %o 格式化無符號八進位制數 %x 格式化無符號十六進位制數 %X 格式化無符號十六進位制數(大寫)
Atitit 前端與ui開發的技術道術與藝術 attilax著 1. 概述 2 1.1. 適用領域: ui相關領域(包括h5 web ios android安卓 cs桌面程式 遊戲程式 等
Atitit 前端與ui開發的技術道術與藝術 attilax著 1. 概述 2 概述 適用領域: ui相關領域(包
Opencv2.4學習::霍夫變換(2)圓變換
霍夫圓變換 基本原理 關於基本原理,其思想大概跟霍夫線變換相似,但是有兩種說法。 第一種: 在霍夫線變換中,笛卡爾X-Y直角座標系中的直線,變換到霍夫空間中則為1個點 因此類比可得,笛卡爾X-Y直角座標系中的圓,變換到abr空間中,則為一條曲線,具體如下:
Chapter 4 字串和格式化輸入\輸出(Character strings and formatted input\output)
重要內容 1. C中的字串一定以空字元結尾。這意味著陣列的容量至少比待儲存字串中的字元數多1. 2. 建立符號常量: 方法1: 宣告一個變數,然後將該變數設定為所需的常量。 方法2:用#define NAME value (注:一般用大寫表示符號
R:ggplot2(7),第4章 用圖形構建影象(3)
《ggplot2:資料分析與圖形藝術》 第4章 用圖形構建影象 4.6 幾何物件 幾何圖形物件,簡稱為geom,它執行著圖層的實際渲染,控制著生成的影象型別。表4.2列出了ggplot2裡面所有可用的幾