Advertisement
vladimirVenkov

ConcatInteger

Jun 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class ConatCombinations {
  8.     public static void main (String[] args) throws IOException {
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10.         int[] aa = {1, 2, 3, 4};
  11.  
  12.         List<Integer> proba =
  13.         Arrays.stream(aa)
  14.                 .boxed()
  15.                 .collect(Collectors.toList());
  16.         proba.forEach(System.out::print);
  17.         var res = concatComb(new HashSet<>(), proba, 0);
  18.         res.forEach(System.out::print);
  19.     }
  20.  
  21.     static HashSet<List<Integer>> concatComb(HashSet<List<Integer>> result,
  22.                                              List<Integer> inp, int index) {
  23.         if (index == inp.size()) {
  24.             List<Integer> next = new LinkedList<>();
  25.             next.addAll(inp);
  26.             result.add(next);
  27.         }
  28.         for (int i = index; i < inp.size(); i++) {
  29.             concatComb(result, inp, ++index);
  30.             index--;
  31.             int first = inp.get(i);
  32.             int second = inp.get(i + 1);
  33.             inp.set(i + 1, first * 10 + second);
  34.             inp.remove(i);
  35.             concatComb(result, inp, index);
  36.             inp.set(i, second);
  37.             inp.add(i, first);
  38.         }
  39.         return result;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement