davie.lin76
BAN USERpublic int power(int base, int ext) {
int result = 1;
for(int head = 1, tail = ext;head <= tail; head++, tail--){
if(tail == head) {
result *= base;
} else {
result = base * base;
}
}
return result;
}
// Recursive, but O(logN) space
public int power(int base, int exp) {
if(exp == 0)
return 1;
if(exp == 1)
return base;
boolean isExpOdd = exp % 2 == 1;
exp = exp / 2;
return power(base, exp) * power(base, isExpOdd ? exp + 1 : exp);
}
public void assignNeighborNode(Node root){
Queue<Node> outerQue = new Queue<>();
Queue<Node> innerQue = new Queue<>();
outerQue.enqueue(root);
while(!outerQue.isEmpty()){
while(!outerQue.isEmpty())
innerQue.enqueue(outerQue.dequeue());
Node pre = null;
while(!innerQue.isEmpty()){
Node cur = innerQue.dequeue();
if(pre != null){
pre.neighbor = cur;
}
outerQue.enqueue(cur.left);
outerQue.enqueue(cur.right);
pre = cur;
}
}
}
- davie.lin76 December 16, 2015forgot to add
return -1; outside of outer for loop
Assuming int array
public int firstNonRepeat(int[] input){
for(int i = 0; i < input.length; i++) {
boolean foundDuplicate = false;
for(int j = 1; j < input.length && !foundDuplicate; j++) {
foundDuplicate = input[j] == input[i];
}
if(!foundDuplicate)
return input[i];
}
}
The question is asking a single Unix command
- davie.lin76 December 05, 2015I think if input contains only 1 integer the size being returned would be wrong. Why is the "size" variable necessary, you should just
return mp.size()
if list is null, then the first return statement would throw NPE
- davie.lin76 November 25, 2015public int concateListOfInt(List<Integer> intList) {
Collections.sort(intList, (a, b) -> {
return lexicalCompareInt(a, b);
})
}
private int lexicalCompareInt(int a, int b) {
int digitA = findHowManyDigit(a);
int digitB = findHowManyDigit(b);
while(digitA >= 1 || digitB >= 1) {
if(getValueForDigit(a, digitA) > getValueForDigit(b, digitB)) {
return 1;
} else if (getValueForDigit(a, digitA) < getValueForDigit(b, digitB)) {
return -1;
} else {
if(digitA > 1)
--digitA;
if(digitB > 1)
--digitB;
}
}
}
private int findHowManyDigit(int value) {
return Math.log10(value) + 1;
}
private int getValueForDigit(int input, int digit) {
return intput / (10 ^ --digit);
}
- davie.lin76 November 21, 2015
RepTinaaJohn, Cloud Support Associate at AMD
Hi,Everyone.I am an art teacher at Woodson Institute.Foster and encourage critical thinking and analysis about art and ...
Repmarybritt, Android Engineer at Centro
I am Mary from Wilmington. I am working as a Graphic Designer in Super Enterprises. I also write articles and ...
RepI am from london. I am 27 year old. I work in a balanced fortune as a personal care aide ...
Assumptions
* Everyone pays/receive payment in bills (no change)
* the input is sorted
- davie.lin76 January 15, 2018