fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(final String[] args) {
  11. //Your input of [[1,0],[2,0],[3,3],[1,0,0]] in a List of Lists
  12. final List<List<Integer>> input = createInput();
  13.  
  14. //To hold onto the groups for printing later
  15. final Map<Integer, List<List<Integer>>> groupsMap = new HashMap<Integer, List<List<Integer>>>();
  16.  
  17. //This will track the List<Integer> that have already been grouped.
  18. final List<List<Integer>> ungroupedLists = new ArrayList<List<Integer>>(input);
  19.  
  20. for (int i = 0; i < input.size(); i++ ) {
  21. //This gets the group indexed at i (starting from 0 just like arrays)
  22. final List<Integer> currentList = input.get(i);
  23.  
  24. //This checks if the currentList has already been grouped.
  25. //If it has, then continue to the next List.
  26. if( !ungroupedLists.contains(currentList) ) {
  27. continue;
  28. }
  29. //If it hasn't already been grouped, then remove it as we're going to group it now.
  30. ungroupedLists.remove(currentList);
  31.  
  32. //Create a new group to add any grouped lists to.
  33. final List<List<Integer>> currentGroup = new ArrayList<List<Integer>>();
  34. currentGroup.add(currentList);
  35.  
  36. //For the ungrouped lists, check if they group with the current list.
  37. for (int j = 0; j < ungroupedLists.size(); j++) {
  38. //Get the list we want to compare the current one to.
  39. final List<Integer> currentComparisonList = ungroupedLists.get(j);
  40.  
  41. //Gets the first integer of the currentList
  42. final Integer integer = currentList.get(0);
  43. //Gets the last integer of the currentList
  44. final Integer integer2 = currentList.get(currentList.size()-1);
  45. //Gets the first integer of the currentComparisonList
  46. final Integer integer3 = currentComparisonList.get(0);
  47. //Gets the last integer of the currentComparisonList
  48. final Integer integer4 = currentComparisonList.get(currentComparisonList.size()-1);
  49.  
  50. //If the first Integers match, and the last integers match, add the currentComparisonList
  51. //To the group and remove it from the ungroupedLists collection
  52. if( integer == integer3 && integer2 == integer4 ) {
  53. currentGroup.add(currentComparisonList);
  54. ungroupedLists.remove(currentComparisonList);
  55. }
  56. }
  57.  
  58. //When we've done all of that checking, add the group to the map of groups.
  59. groupsMap.put(i, currentGroup);
  60. }
  61.  
  62. //This simply prints the output int he format desired.
  63. for ( int i = 0; i < groupsMap.size(); i++ ) {
  64. System.out.println("group: " + i);
  65. for (final List<Integer> list : groupsMap.get(i)) {
  66. System.out.println(list.toString());
  67. }
  68. }
  69.  
  70. }
  71.  
  72. private static List<List<Integer>> createInput() {
  73. final List<List<Integer>> input = new ArrayList<List<Integer>>();
  74. final ArrayList<Integer> list1 = new ArrayList<Integer>();
  75. list1.add(1);
  76. list1.add(0);
  77. final ArrayList<Integer> list2 = new ArrayList<Integer>();
  78. list2.add(2);
  79. list2.add(0);
  80. final ArrayList<Integer> list3 = new ArrayList<Integer>();
  81. list3.add(3);
  82. list3.add(3);
  83. final ArrayList<Integer> list4 = new ArrayList<Integer>();
  84. list4.add(1);
  85. list4.add(0);
  86. list4.add(0);
  87. input.add(list1);
  88. input.add(list2);
  89. input.add(list3);
  90. input.add(list4);
  91. return input;
  92. }
  93. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
group: 0
[1, 0]
[1, 0, 0]
group: 1
[2, 0]
group: 2
[3, 3]