This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int trap(int[] A) { | |
int count = 0; | |
int leftPeak = -1; | |
int rightPeak = -1; | |
int minPeak = -1; | |
boolean flag = false; | |
for(int i=0; i<A.length; i++){ | |
if(isPeak(A,i)){ | |
leftPeak = rightPeak; | |
rightPeak = i; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Solution { | |
public TreeNode buildTree(int[] preorder, int[] inorder) { | |
int len = preorder.length; | |
if(len == 0){ | |
return null; | |
} | |
return buildTreeHelper(preorder, inorder, 0, len-1, 0, len-1); | |
} | |
public TreeNode buildTreeHelper(int[] preorder, int [] inorder, int ps, int pe, int is, int ie){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Solution { | |
public TreeNode buildTree(int[] inorder, int[] postorder) { | |
int in_len = inorder.length; | |
int post_len = postorder.length; | |
if(post_len == 0) | |
return null; | |
TreeNode root=new TreeNode(postorder[post_len-1]); | |
int position=in_len-1; | |
while(inorder[position] != postorder[post_len-1]){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void rotate(int[][] matrix) { | |
int len = matrix.length; | |
int start, end, element; | |
for(int layer=0; layer<len/2;layer++){ | |
start = layer; | |
end = len-1-start; | |
for(int gp=0; gp<end-start;gp++){ | |
element = matrix[layer][start+gp]; | |
matrix[layer][start+gp] = matrix[end-gp][layer]; | |
matrix[end-gp][layer] = matrix[len-1-layer][end-gp]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int searchInsert(int[] A, int target) { | |
for(int i=0;i<A.length;i++){ | |
if(A[i] == target) | |
return i; | |
else if(A[i] > target) | |
return i; | |
else if(i == A.length-1) | |
return i+1; | |
} | |
return -1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Integer> spiralOrder(int[][] matrix) { | |
List<Integer> result = new ArrayList<Integer>(); | |
if(matrix == null || matrix.length == 0) | |
return result; | |
int endy=matrix.length; | |
int endx=matrix[0].length; | |
int starty = 0; | |
int startx = 0; | |
while(startx!=endx && starty!=endy){ | |
if(starty < endy){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int threeSumClosest(int[] num, int target) { | |
HashSet<Integer> partSum = new HashSet<Integer>(); | |
partSum.add(num[0]+num[1]); | |
int result=num[0]+num[1]+num[2]; | |
for(int i=2; i<num.length; i++){ | |
for(int n: partSum){ | |
if(Math.abs(n+num[i]-target)<Math.abs(result-target)){ | |
if(n+num[i] == target) | |
return target; | |
else result = n+num[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Hashtable; | |
public class Solution { | |
public int[] twoSum(int[] numbers, int target) { | |
Hashtable<Integer, Integer> check = new Hashtable<Integer, Integer>(); | |
for(int i = 0; i<numbers.length;i++){ | |
if(check.containsKey(target-numbers[i])) | |
return new int[]{check.get(target-numbers[i]),i+1}; | |
else check.put(numbers[i],i+1); | |
} | |
return null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Hashtable; | |
public class Solution { | |
public int majorityElement(int[] num) { | |
Hashtable<Integer,Integer> count = new Hashtable<Integer, Integer>(); | |
int len = num.length; | |
int curr; | |
for(int i=0; i<len; i++){ | |
curr = num[i]; | |
if(count.containsKey(curr)){ | |
int temp = count.get(curr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Integer> getRow(int rowIndex) { | |
List<Integer> result = new ArrayList<Integer>(); | |
for(int i=0; i<=rowIndex; i++){ | |
if(i == 0 ) | |
result.add(1); | |
else{ | |
List<Integer> temp = new ArrayList<Integer>(result); | |
for(int j=1;j<i;j++){ | |
result.set(j, temp.get(j-1) + temp.get(j)); | |
} |
NewerOlder