當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Implementing next_permutation()用法及代碼示例

給定一個數組或字符串,任務是在Java中按字典順序查找下一個更大的排列。

例子:

Input: string = "gfg"
Output: ggf

Input: arr[] = {1, 2, 3}
Output: {1, 3, 2}

在C++中,有一個特定的函數使我們免於編寫大量代碼。它位於頭文件#include中。該函數是next_permutation(a.begin(),a.end())。它用於將[first,last]範圍內的元素重新排列到下一個字典上更大的排列。排列是N的每一個!元素可以采用的可能排列方式(其中N是範圍內的元素數量)。可以根據它們在字典上的比較方式來排列不同的排列順序。


顯然,Java不提供任何此類內置方法。因此,本文討論如何在Java中實現下一個置換函數及其算法。

算法:

  1. 找到最長的不增加後綴並找到樞軸。
  2. 如果後綴是整個數組,則數據沒有更高階的排列。
  3. 找到最合適的繼任者。
  4. 交換後繼者和支點。
  5. 反轉後綴。

下麵是上述方法的實現:

// Java program to implement 
// the next_permutation method 
  
import java.util.Arrays; 
  
public class nextPermutation { 
  
    // Function to swap the data 
    // present in the left and right indices 
    public static int[] swap(int data[], int left, int right) 
    { 
  
        // Swap the data 
        int temp = data[left]; 
        data[left] = data[right]; 
        data[right] = temp; 
  
        // Return the updated array 
        return data; 
    } 
  
    // Function to reverse the sub-array 
    // starting from left to the right 
    // both inclusive 
    public static int[] reverse(int data[], int left, int right) 
    { 
  
        // Reverse the sub-array 
        while (left < right) { 
            int temp = data[left]; 
            data[left++] = data[right]; 
            data[right--] = temp; 
        } 
  
        // Return the updated array 
        return data; 
    } 
  
    // Function to find the next permutation 
    // of the given integer array 
    public static boolean findNextPermutation(int data[]) 
    { 
  
        // If the given dataset is empty 
        // or contains only one element 
        // next_permutation is not possible 
        if (data.length <= 1) 
            return false; 
  
        int last = data.length - 2; 
  
        // find the longest non-increasing suffix 
        // and find the pivot 
        while (last >= 0) { 
            if (data[last] < data[last + 1]) { 
                break; 
            } 
            last--; 
        } 
  
        // If there is no increasing pair 
        // there is no higher order permutation 
        if (last < 0) 
            return false; 
  
        int nextGreater = data.length - 1; 
  
        // Find the rightmost successor to the pivot 
        for (int i = data.length - 1; i > last; i--) { 
            if (data[i] > data[last]) { 
                nextGreater = i; 
                break; 
            } 
        } 
  
        // Swap the successor and the pivot 
        data = swap(data, nextGreater, last); 
  
        // Reverse the suffix 
        data = reverse(data, last + 1, data.length - 1); 
  
        // Return true as the next_permutation is done 
        return true; 
    } 
  
    // Driver Code 
    public static void main(String args[]) 
    { 
        int data[] = { 1, 2, 3 }; 
        if (!findNextPermutation(data)) 
            System.out.println("There is no higher"
                               + " order permutation "
                               + "for the given data."); 
        else { 
            System.out.println(Arrays.toString(data)); 
        } 
    } 
}
輸出:
[1, 3, 2]


相關用法


注:本文由純淨天空篩選整理自Lokesh Karthikeyan大神的英文原創作品 Implementing next_permutation() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。