java – 从给定的数字查找下一个最高的唯一编号

前端之家收集整理的这篇文章主要介绍了java – 从给定的数字查找下一个最高的唯一编号前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给定一组n个符号,大小k和来自符号集合的非重复字符的长度k的组合,仅写入迭代算法来打印可以进行的下一个最高唯一数字.

例如:

Symbols =[1,2,3,4,5]
size = 3;
given combination = 123,result = 124
given combination = 254,result = 312

解决方法

这是一个伪代码算法:
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]);
     }
}
原文链接:https://www.f2er.com/java/123314.html

猜你在找的Java相关文章