60. Permutation Sequence
来源:互联网 发布:ubuntu nano命令 编辑:程序博客网 时间:2024/06/06 20:09
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.
程序如下:class Solution { private int cnt = 0; private String str = ""; public void swap(char[] array, int i, int j){ char tmp = array[i]; array[i] = array[j]; array[j] = tmp; } public void backTracing(char[] array, int start, int k){ if (cnt == k){ return; } if (start >= array.length){ cnt ++; if (cnt == k){ str = new String(array); } return; } for (int i = start; i < array.length; ++ i){ swap(array, start, i); backTracing(array, start + 1, k); } for (int i = start; i < array.length - 1; ++ i){ swap(array, i, i + 1); } } public String getPermutation(int n, int k) { char[] ch = new char[n]; for (int i = 0; i < ch.length; ++ i){ ch[i] = (char)(i + 1 + '0'); } backTracing(ch, 0, k); return str; }}
阅读全文
0 0
- LeetCode --- 60. Permutation Sequence
- [Leetcode] 60. Permutation Sequence
- [leetcode] 60.Permutation Sequence
- 60. Permutation Sequence
- 60. Permutation Sequence
- **LeetCode 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- 60. Permutation Sequence
- 60. Permutation Sequence
- 60. Permutation Sequence
- 60. Permutation Sequence
- 60. Permutation Sequence
- [LeetCode]60. Permutation Sequence
- LeetCode *** 60. Permutation Sequence
- 60. Permutation Sequence
- LeetCode 60. Permutation Sequence
- hiho 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- 数据结构——第八讲、图(下)(1)
- 如何查看input输入框中是否输入有值
- C#笔记2
- k8s
- String、StringBuffer、StringBuilder的区别
- 60. Permutation Sequence
- vitualbox 5.2.0 超详细4种网络配置模式。
- 动态规划解174Dice Possibility
- mysql主从复制(超简单)
- 计算机网络上机(1)
- 扩展vim成python ide,主要讲述ycm+vim8的安装
- Java基础练习选择题(1)答案
- OpenCV学习笔记(33)棋盘格角点检测练习程序
- [zz系列]增1和减1系列