Get a list of numbers from user return all possible permutat

Get a list of numbers from user, return all possible permutations. For example, suppose we get number 1, 2, 3 from user, then we have [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class Program {

  
public static void permute(ArrayList l,int low,int high)
{
int i;
  
if(low==high)
System.out.println(l);
  
for(i=low;i<=high;i++)
{
Collections.swap(l,low,i);
permute(l,low+1,high);
Collections.swap(l,low,i);
}
  
}

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int temp;
ArrayList l1 = new ArrayList();
while(true)
{
System.out.println(\"Input a number or enter -1 for quit.\");
temp = input.nextInt();
if(temp==-1)
break;
else
l1.add(temp);
}
  
permute(l1,0,l1.size()-1);
}
}

 Get a list of numbers from user, return all possible permutations. For example, suppose we get number 1, 2, 3 from user, then we have [1,2,3], [1,3,2], [2,1,3]

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site