JosephEl
BAN USERThis solution in java
public int findPivot(int[] nums){
if(nums.length == 0){
return -1;
}
int sumLeft = nums[0];
int sumRight = 0;
int left = 0, right = nums.length ;
while( left<=right ){
if(sumLeft > sumRight){
sumRight += nums[--right];
}else if(sumLeft < sumRight){
sumLeft += nums[++left];
}else{
if(left+1 == right-1){
return left+1;
}
if(nums[right-1] == 0){
right--;
}else if(nums[left+1] == 0){
left++;
}else {
sumRight += nums[right--];
sumLeft += nums[left++];
}
}
}
return -1;
}
public class Lamp {
private boolean[][] matrix;
public Lamp(boolean[][] matrix){
this.matrix = matrix;
}
public boolean isIlluminated(int x, int y){
if(matrix[y][x]){
return true;
}
int len = matrix.length;
for(int i = 0; i < len; i++){
if(matrix[y][i] || matrix[i][x]){
return true;
}
if(y-i >= 0 && x-i >= 0 && matrix[y-i][x-i]){
return true;
}
if(y-i < len && x-i < len && matrix[y+i][x+i]){
return true;
}
}
return false;
}
}
public List<String> getAllPaths(TreeNode node){
List<String> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(node);
Map<Integer, Integer> parents = new HashMap<>();
parents.put(node.val, -1);
while(!stack.isEmpty()){
TreeNode tmp = stack.pop();
if(tmp.left == null && tmp.right == null){
LinkedList<Integer> tmpRes = new LinkedList<>();
int num = tmp.val;
while(num != -1){
tmpRes.addFirst(num);
num = parents.get(num);
}
result.add(createPath(tmpRes));
}
if(tmp.left != null) {
stack.push(tmp.left);
parents.put(tmp.left.val, tmp.val);
}
if(tmp.right != null){
stack.push(tmp.right);
parents.put(tmp.right.val, tmp.val );
}
}
return result;
}
public String createPath(LinkedList list){
if(list.isEmpty()){
return "";
}
StringBuilder sb = new StringBuilder(list.removeFirst().toString());
while(list.isEmpty()){
sb.append("->");
sb.append(list.removeFirst().toString());
}
return sb.toString();
}
- JosephEl November 17, 2016