This file contains hidden or 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 int minDistance(String word1, String word2) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int row=word1.length()+1; | |
int col=word2.length()+1; | |
if(row==0) return col; | |
if(col==0) return row; | |
int d[][]=new int[row][col]; | |
for(int i=0; i<row; i++) |
This file contains hidden or 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 searchMatrix(int[][] matrix, int target) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int height=matrix.length-1; | |
int width=matrix[0].length-1; | |
int i=width; | |
int j=0; | |
while(i>=0 && j<=height) | |
{ |
This file contains hidden or 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 ArrayList<ArrayList<Integer>> combine(int n, int k) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>(); | |
if(k>n || k<=0 ||n<=0) return result; | |
combineResult(result, new ArrayList<Integer>() ,k, 1, n); | |
return result; | |
} | |
void combineResult(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int k, int n, int target) |
This file contains hidden or 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 ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
ArrayList<ArrayList<String>> results=new ArrayList<ArrayList<String>>(); | |
if(start.length()!=end.length()) return results; | |
Queue<ArrayList<String>> que=new LinkedList<ArrayList<String>>(); | |
ArrayList<String> list=new ArrayList<String>(); | |
list.add(start); | |
que.add(list); |
This file contains hidden or 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 int ladderLength(String start, String end, HashSet<String> dict) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
if(start.length()!=end.length()) return 0; | |
if(start.compareTo("")==0) return 1; | |
Queue<String> que=new LinkedList<String>(); | |
HashSet<String> set=new HashSet<String>(); | |
que.add(start); | |
set.add(start); |
This file contains hidden or 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 int longestConsecutive(int[] num) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int length=num.length; | |
HashSet<Integer> set=new HashSet<Integer>(); | |
for(int i: num) | |
{ | |
set.add(i); | |
} |
This file contains hidden or 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 binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { |
This file contains hidden or 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 ArrayList<ArrayList<Integer>> subsets(int[] S) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>(); | |
result.add(new ArrayList<Integer>()); | |
if(S.length==0) return result; | |
Arrays.sort(S); | |
for(int i=0; i<S.length; i++) | |
{ |
This file contains hidden or 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 exist(char[][] board, String word) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int m=board.length; | |
int n=board[0].length; | |
if(m==0) return n==0; | |
boolean [][] visited=new boolean [m][n]; | |
for(int i=0; i<m; i++) | |
{ |
This file contains hidden or 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 int removeDuplicates(int[] A) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int length=A.length; | |
int counter=0; | |
int i=0; | |
while(i<length) | |
{ | |
int current=A[i]; |