edanir
BAN USERFirst one;
class SubSets{
int[] array;
SubSets(){
array = new int[]{1,2,3,4,5};
}
public ArrayList<ArrayList<Integer>> getSubs(int totalNumber){
ArrayList<ArrayList<Integer>> subArray = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> tempSub = new ArrayList<Integer>();
Integer currentTotal;
for(int i = 0;i < array.length-1;i++){
currentTotal = array[i];
tempSub = new ArrayList<Integer>();
tempSub.add(currentTotal);
if (currentTotal < totalNumber) {
for(int k = i+1; k < array.length;k++){
int t = k;
currentTotal = array[i];
tempSub = new ArrayList<Integer>();
tempSub.add(currentTotal);
while(currentTotal < totalNumber && t < array.length){
tempSub.add(array[t]);
currentTotal += array[t];
t++;
}
if (currentTotal == totalNumber) subArray.add(tempSub);
}
}
else if (currentTotal == totalNumber) subArray.add(tempSub);
}
return subArray;
}
}
class CheckCode{
public static boolean check(String s){
Stack ll = new Stack();
HashMap<Character,Character> map = new HashMap<Character,Character>();
map.put('{', '}');
map.put('(', ')');
map.put('[', ']');
char c;
char k;
if (s == null) return false;
for (int i = 0;i<s.length();i++){
c = s.charAt(i);
if (map.get(c) != null) {
ll.push(c);
}
if (map.containsValue(c)){
if (ll.peek() != null) {
k = (char) ll.pop();
if (map.get(k) != c) return false;
}
else return false;
}
}
return ll.peek() == null;
}
}
public TreeNode getCommonAnchedtor(TreeNode n1, TreeNode n2){
if (n1 == null || n2 == null || n1.parent == null || n2.parent == null) return null;
boolean leftNode = true;
if (n1.parent.right == n1) leftNode = false;
TreeNode p = n1.parent;
while (p.parent != null){
if (leftNode) if (includedIn(p.parent.right,n2)) return p.parent;
else if (includedIn(p.parent.left,n2)) return p.parent;
p = p.parent;
}
return null;
}
public boolean includedIn(TreeNode n, TreeNode nSearch){
if (n.left != null && includedIn(n.left,nSearch)) return true;
if (n.right != null && includedIn(n.right,nSearch)) return true;
if (n == nSearch) return true;
return false;
}
- edanir December 09, 2016