-
Notifications
You must be signed in to change notification settings - Fork 107
/
NDArrayToIndexes.java
73 lines (63 loc) · 1.49 KB
/
NDArrayToIndexes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Assuming all the values to be greater than or equal to 1
import java.util.ArrayList;
import java.util.List;
public class ArrayToIndex {
public static void main (String[] args) {
int[] arr = new int[]{3,2,1};
List<Node> rootNodes = new ArrayList<>();
List<Node> currParents = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
List<Node> forThisLoop = new ArrayList<>();
for(int j=0;j<arr[i];j++)
{
if(currParents.size()==0)
{
Node iter = new Node(j);
rootNodes.add(iter);
forThisLoop.add(iter);
}
else
{
for(int k=0;k<currParents.size();k++)
{
Node parent = currParents.get(k);
Node iter = new Node(j);
parent.children.add(iter);
forThisLoop.add(iter);
}
}
}
currParents.clear();
currParents.addAll(forThisLoop);
}
List<String> indexes = new ArrayList<>();
String str = "";
printIndexes(rootNodes,str,indexes);
for(int i=0;i<indexes.size();i++)
{
System.out.println(indexes.get(i));
}
}
public static void printIndexes(List<Node> nodes,String str,List<String> indexes)
{
for(int i=0;i<nodes.size();i++)
{
Node obj = nodes.get(i);
if(obj.children.size()==0)
{
indexes.add(str+obj.value);
}
else
printIndexes(obj.children,str+obj.value,indexes);
}
}
}
class Node {
public int value;
public List<Node> children = new ArrayList<>();
public Node(int value)
{
this.value = value;
}
}