Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

permutation array java

public class Permute{
    static void permute(java.util.List<Integer> arr, int k){
        for(int i = k; i < arr.size(); i++){
            java.util.Collections.swap(arr, i, k);
            permute(arr, k+1);
            java.util.Collections.swap(arr, k, i);
        }
        if (k == arr.size() -1){
            System.out.println(java.util.Arrays.toString(arr.toArray()));
        }
    }
    public static void main(String[] args){
        Permute.permute(java.util.Arrays.asList(3,4,6,2,1), 0);
    }
}
Comment

permutation array java


int a[] = {3,4,6,2,1};
int size = sizeof(a)/sizeof(a[0]);
std::sort(a, a+size);
do {
  // print a's elements
} while(std::next_permutation(a, a+size));

Comment

Permutations of array in java

 
package org.arpit.java2blog;
 
import java.util.ArrayList;
import java.util.List;
 
public class PermutateArray {
 
    public static void main(String[] args) {
        PermutateArray pa=new PermutateArray();
 
        int[] arr= {10, 20, 30};
 
        List<List<Integer>> permute = pa.permute(arr);
 
        System.out.println("Permuations of array : [10, 20, 30] are:");
        System.out.println("=========================================");
        for(List<Integer> perm:permute)
        {
            System.out.println(perm);
        }
 
    }
    public List<List<Integer>> permute(int[] arr) {
        List<List<Integer>> list = new ArrayList<>();
        permuteHelper(list, new ArrayList<>(), arr);
        return list;
    }
 
    private void permuteHelper(List<List<Integer>> list, List<Integer> resultList, int [] arr){
 
        // Base case
        if(resultList.size() == arr.length){
            list.add(new ArrayList<>(resultList));
        }
        else{
            for(int i = 0; i < arr.length; i++){
 
                if(resultList.contains(arr[i]))
                {
                    // If element already exists in the list then skip
                    continue;
                }
                // Choose element
                resultList.add(arr[i]);
                // Explore
                permuteHelper(list, resultList, arr);
                // Unchoose element
                resultList.remove(resultList.size() - 1);
            }
        }
    }
 
}
 
Comment

permutation array java

public class Permute{
    static void permute(java.util.List<Integer> arr, int k){
        for(int i = k; i < arr.size(); i++){
            java.util.Collections.swap(arr, i, k);
            permute(arr, k+1);
            java.util.Collections.swap(arr, k, i);
        }
        if (k == arr.size() -1){
            System.out.println(java.util.Arrays.toString(arr.toArray()));
        }
    }
    public static void main(String[] args){
        Permute.permute(java.util.Arrays.asList(3,4,6,2,1), 0);
    }
}
Comment

permutation array java


int a[] = {3,4,6,2,1};
int size = sizeof(a)/sizeof(a[0]);
std::sort(a, a+size);
do {
  // print a's elements
} while(std::next_permutation(a, a+size));

Comment

Permutations of array in java

 
package org.arpit.java2blog;
 
import java.util.ArrayList;
import java.util.List;
 
public class PermutateArray {
 
    public static void main(String[] args) {
        PermutateArray pa=new PermutateArray();
 
        int[] arr= {10, 20, 30};
 
        List<List<Integer>> permute = pa.permute(arr);
 
        System.out.println("Permuations of array : [10, 20, 30] are:");
        System.out.println("=========================================");
        for(List<Integer> perm:permute)
        {
            System.out.println(perm);
        }
 
    }
    public List<List<Integer>> permute(int[] arr) {
        List<List<Integer>> list = new ArrayList<>();
        permuteHelper(list, new ArrayList<>(), arr);
        return list;
    }
 
    private void permuteHelper(List<List<Integer>> list, List<Integer> resultList, int [] arr){
 
        // Base case
        if(resultList.size() == arr.length){
            list.add(new ArrayList<>(resultList));
        }
        else{
            for(int i = 0; i < arr.length; i++){
 
                if(resultList.contains(arr[i]))
                {
                    // If element already exists in the list then skip
                    continue;
                }
                // Choose element
                resultList.add(arr[i]);
                // Explore
                permuteHelper(list, resultList, arr);
                // Unchoose element
                resultList.remove(resultList.size() - 1);
            }
        }
    }
 
}
 
Comment

PREVIOUS NEXT
Code Example
Java :: java.lang.RuntimeException: Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules guava-20.0.jar (com.google.guava:guava:20.0) and listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0) 
Java :: Index through 2d array 
Java :: Java Create LinkedList in Java 
Java :: get preference value android 
Java :: concat result of List to one text java 
Java :: 8.1.1. Declaring an Array&para; 
Java :: add opacity to activity android 
Java :: How to determine whether a graph contains a cycle, in Java? 
Java :: implement the bubble sort algorithm on the following arraylist 
Java :: fhow to find location of java jdk 
Java :: string tokenizer java 
Java :: java Write a program to reverse an array or string 
Java :: java loop through list 
Java :: java sleep 1 second 
Java :: build.plugins.plugin.version 
Java :: convert javascritp getTime() to datetime 
Java :: resize image icon to fit jlabel 
Java :: get day from date month year java 
Java :: template competitive programming java 
Java :: spring swagger 
Java :: how to find max in min in an array 
Java :: parse object to int java 
Java :: cmd java compile 
Java :: how to take max value from priority queue in java 
Java :: java replace element in list 
Java :: how to add chips dynamically android 
Java :: generic list java 
Java :: how to check palindrome in java 
Java :: convert char array to set in java 
Java :: for cicle java 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =