Given a set of n symbols a size k and a combination of length k of non repeating characters from the symbol set
2011-12-23 17:11
671 查看
Given a set of n symbols a size k and a combination of length k of non repeating characters from the symbol set. Write only an ITERATIVE algorithm to print the next unique combination.
Ex: Symbols =[1,2,3,4,5]
size = 3;
given combination = 123, result = 124
given combination = 254, result = 312
Q:
提供一个数字的集合n,然后给一个长度为k的整数(该整数中的数字,都是取自该集合n)。现在要求写一个算法,得到当前整数的“下一个数字”,该整数是比当前整数大的最小整数,并且所有的数字也都是取自集合n。
A: 我的思路是
1. 把当前整数中的所有数字先列出来,如:123 –> A = {1,2,3},然后得到一个剩余数字集合,如B = {4,5}
2. 从右往左扫描整数,如果当前的数字 i 小于B集合中的任意个数字j,则将i和j交换,如把3替换成4
3. 然后把从当前数字i往后的所有数字,替换为集合中的数字(从小到大)
4. 如果碰到i > B中所有的数字,可以把当前的i数字加入到B集合中,继续循环
现在以第二个例子讲解一下
1. A = {2, 5, 4}, B = {1, 3}
2. 扫描
a) 4 > B, 5 > B
b) 2 < B, 所以 2 和 3交换, B = {1,2}, 整数此时变为 354
3. 然后将原来2后面的数字和B中的数字交换(从小到大)5 <-> 1, 4 <-> 2,
所以答案是312
[非正式代码]
View Code
Ex: Symbols =[1,2,3,4,5]
size = 3;
given combination = 123, result = 124
given combination = 254, result = 312
Q:
提供一个数字的集合n,然后给一个长度为k的整数(该整数中的数字,都是取自该集合n)。现在要求写一个算法,得到当前整数的“下一个数字”,该整数是比当前整数大的最小整数,并且所有的数字也都是取自集合n。
A: 我的思路是
1. 把当前整数中的所有数字先列出来,如:123 –> A = {1,2,3},然后得到一个剩余数字集合,如B = {4,5}
2. 从右往左扫描整数,如果当前的数字 i 小于B集合中的任意个数字j,则将i和j交换,如把3替换成4
3. 然后把从当前数字i往后的所有数字,替换为集合中的数字(从小到大)
4. 如果碰到i > B中所有的数字,可以把当前的i数字加入到B集合中,继续循环
现在以第二个例子讲解一下
1. A = {2, 5, 4}, B = {1, 3}
2. 扫描
a) 4 > B, 5 > B
b) 2 < B, 所以 2 和 3交换, B = {1,2}, 整数此时变为 354
3. 然后将原来2后面的数字和B中的数字交换(从小到大)5 <-> 1, 4 <-> 2,
所以答案是312
[非正式代码]
View Code
int n = length(Symbols); int k = length(A); // TRACK WHICH LETTERS ARE STILL AVAILABLE available = sort(Symbols minus A); // SEARCH BACKWARDS FOR AN ENTRY THAT CAN BE INCREASED for (int i=k-1; i>=0; --i) { // LOOK FOR NEXT SMALLEST AVAILABLE LETTER for (int j=0; j<n-k; ++j) { if (A[i] < available[j]) { break; } } if (j < n-k) { // CHANGE A[i] TO THAT, REMOVE IT FROM AVAILABLE int tmp = A[i]; A[i] = available[j]; available[j] = tmp; // RESET SUBSEQUENT ENTRIES TO SMALLEST AVAILABLE for (j=i+1; i<k; ++j) { A[j] = available[i+1-j]; } return A; } else { // A[i] MUST BE LARGER THAN AVAILABLE, SO APPEND TO END available = append(available,A[i]); } }
相关文章推荐
- Find the first non-repeating character from a stream of characters
- LeetCode : Given a string, find the length of the longest serial substring without repeating characters.
- Given a string, find the length of the longest substring without repeating characters
- Given an array of size n, find all the possible sub set of the array of size k
- Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: startIndex
- Length of the longest substring without repeating characters(dp)
- Leetcode 3. Longest Substring Without Repeating Characters The Solution of Python and Javascript
- Its width is W and height is H. As a result, if the font size of characters is S then it can only sh
- Print all possible strings of length k that can be formed from a set of n characters
- leetcode-Length of the longest substring without repeating characters
- .NET中使用GridView控件输入数据时出现“ Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"的问题
- Length of the longest substring without repeating characters
- Length of the longest substring without repeating characters
- More 3D Graphics (rgl) for Classification with Local Logistic Regression and Kernel Density Estimates (from The Elements of Statistical Learning)(转)
- To find names containing exactly five characters, use “^”and “$”to match the beginning and end of the name, and five instances of “.”in between: mysql
- Microsoft SQL Server 2008 R2 XML处理 One solution is to increase the number of characters retrieved from the server for XML data.
- 扫描线算法的论文《The Intersections for a Set of 2D Segments, and Testing Simple Polygons》的部分翻译
- Infinite size of Hypothesis set and growth function
- Infinite size of Hypothesis set and growth funciton
- You're given a string of lower-case Latin letters. Your task is to find the length of its longest su