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.Map; | |
import java.util.HashMap; | |
public class Solution { | |
public boolean wordBreak(String s, Set<String> dict) { | |
if( (dict.size()==0)||(s.length()==0)) | |
return false; | |
if(s.length()==1 && !dict.contains(s)) |
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
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = 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
public void setZeroes(int[][] matrix) { | |
boolean firstRow =false,firstCol=false; | |
for(int i=0;i<matrix.length;i++){ | |
if(matrix[i][0]==0){ | |
firstCol=true; | |
break; | |
} | |
} |
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 class Solution { | |
public boolean isValid(String s) { | |
if(s==null){ | |
return false; | |
}else if( s.length()==0 ){ | |
return true; | |
}else if( (s.length()%2)!=0 ){ | |
return false; | |
} | |
Stack<Character> st = new Stack<Character>(); |
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 double findMedianSortedArrays(int A[], int B[]) { | |
int M= A.length; | |
int N= B.length; | |
int mid = (M+N)/2; | |
if(M<=N) | |
return median(A,B,Math.max(0,mid-N),Math.min(M-1,mid)); | |
else |
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[] twoSum(int[] numbers, int target) { | |
int [] index = new int[2]; | |
if(numbers.length==0) | |
return index; | |
int N = numbers.length; | |
HashMap<Integer,Integer> map = new HashMap<Integer, Integer>(); | |
for(int i=0;i<N;i++){ | |
map.put(numbers[i],i); | |
} | |
for(int i=0;i<N;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
public static List<List<Integer>> trip(int[] num) { | |
List<List<Integer>> result = new ArrayList<List<Integer>> (); | |
ArrayList<Integer> sol = new ArrayList<Integer>(); | |
if(num.length==0 || num.length<3){ | |
return result; | |
} | |
Arrays.sort(num); | |
for(int i=0;i<num.length;i++){ | |
int target = 0-num[i]; | |
int st = i+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 int threeSumClosest(int[] num, int target) { | |
if( (num==null) || (num.length<3) ){ | |
return target; | |
} | |
Arrays.sort(num); | |
int diff= Integer.MAX_VALUE; | |
int closest =0; | |
for(int i=0;i<num.length;i++){ | |
int st=i+1; | |
int end=num.length-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 int maxProfit(int[] prices) { | |
int min = Integer.MAX_VALUE; | |
int maxProfit=0; | |
for(int i=0;i<prices.length;i++){ | |
if(min>prices[i]){ | |
min=prices[i]; | |
} | |
if( (prices[i]-min)>maxProfit ){ | |
maxProfit = prices[i]-min; | |
} |
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 maxProfit(int[] prices) { | |
int maxProfit=0; | |
for(int i=1;i<prices.length;i++){ | |
if( (prices[i]-prices[i-1])>0 ){ | |
maxProfit += prices[i]-prices[i-1]; | |
} | |
} | |
return maxProfit; | |
} |