Skip to content

Commit c14ea74

Browse files
committed
Update solved leetcode question and answers
1 parent 1e513be commit c14ea74

17 files changed

+427
-14
lines changed

14_longestCommonPrefix.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public String longestCommonPrefix(String[] strs) {
3+
if(strs == null || strs.length == 0) return "";
4+
int minLen = Integer.MAX_VALUE;
5+
for(String str: strs){
6+
if(str.length() < minLen){
7+
minLen = str.length();
8+
}
9+
}
10+
11+
String prev = "";
12+
for(int i = 0; i < minLen; i++){
13+
char current = strs[0].charAt(i);
14+
for(int j = 1; j < strs.length; j++){
15+
if(current!=strs[j].charAt(i)){
16+
return prev;
17+
}
18+
}
19+
prev = strs[0].substring(0,i+1);
20+
}
21+
return prev;
22+
}
23+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int evalRPN(String[] tokens) {
3+
Stack<String> stack = new Stack<String>();
4+
for(String token: tokens){
5+
if("+-*/".contains(token)){
6+
int b = Integer.valueOf(stack.pop());
7+
int a = Integer.valueOf(stack.pop());
8+
int result = 0;
9+
if(token.equals("+")){
10+
result = a+b;
11+
} else if(token.equals("-")){
12+
result = a-b;
13+
} else if(token.equals("*")){
14+
result = a*b;
15+
} else if(token.equals("/")){
16+
result = a/b;
17+
}
18+
stack.push(String.valueOf(result));
19+
} else{
20+
stack.push(token);
21+
}
22+
}
23+
return Integer.valueOf(stack.pop());
24+
}
25+
}

152_maxProduct.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int maxProduct(int[] nums) {
3+
int[] max = new int[nums.length];
4+
int[] min = new int[nums.length];
5+
int result = nums[0];
6+
max[0] = nums[0];
7+
min[0] = nums[0];
8+
for(int i = 1; i < nums.length; i++){
9+
max[i] = Math.max(Math.max(nums[i],nums[i]*max[i-1]),nums[i]*min[i-1]);
10+
min[i] = Math.min(Math.min(nums[i],nums[i]*max[i-1]),nums[i]*min[i-1]);
11+
result = Math.max(max[i],result);
12+
}
13+
return result;
14+
}
15+
}

160_getIntersectionNode.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14+
ListNode runA = headA;
15+
ListNode runB = headB;
16+
int lengthA = 0;
17+
int lengthB = 0;
18+
while(runA!=null){
19+
runA = runA.next;
20+
lengthA++;
21+
}
22+
while(runB!=null){
23+
runB = runB.next;
24+
lengthB++;
25+
}
26+
int gap = lengthA-lengthB;
27+
if(gap >= 0){
28+
while( gap > 0){
29+
headA = headA.next;
30+
gap--;
31+
}
32+
} else{
33+
while( gap < 0){
34+
headB = headB.next;
35+
gap++;
36+
}
37+
}
38+
39+
while(headA!=headB){
40+
headA = headA.next;
41+
headB = headB.next;
42+
}
43+
return headA;
44+
}
45+
}

205_isIsomorphic.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public boolean isIsomorphic(String s, String t) {
3+
if(s == null && t == null) return true;
4+
if(s == null || t == null) return false;
5+
if(s.length()!=t.length()) return false;
6+
7+
HashMap<Character, Character> map1 = new HashMap<Character, Character>();
8+
HashMap<Character, Character> map2 = new HashMap<Character, Character>();
9+
10+
int length = s.length();
11+
for(int i = 0; i < length; i++){
12+
char m = s.charAt(i);
13+
char n = t.charAt(i);
14+
if(map1.containsKey(m)){
15+
if(map1.get(m)!=n) return false;
16+
}
17+
if(map2.containsKey(n)){
18+
if(map2.get(n)!=m) return false;
19+
}
20+
map1.put(m,n);
21+
map2.put(n,m);
22+
}
23+
return true;
24+
}
25+
}

21_mergeTwoList.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
public class Solution {
10+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11+
ListNode fakeNode = new ListNode(0);
12+
ListNode run = fakeNode;
13+
while(l1!=null && l2!=null){
14+
if(l1.val < l2.val){
15+
run.next = l1;
16+
run = run.next;
17+
l1 = l1.next;
18+
} else{
19+
run.next = l2;
20+
run = run.next;
21+
l2 = l2.next;
22+
}
23+
}
24+
25+
if(l1!=null){
26+
run.next = l1;
27+
}
28+
29+
if(l2!=null){
30+
run.next = l2;
31+
}
32+
return fakeNode.next;
33+
}
34+
}

235_lowestCommonAncestor_BST.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12+
// bottom to top
13+
if(root == null) return null;
14+
if(root.val < p.val && root.val > q.val) return root;
15+
if(root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right,p,q);
16+
if(root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left,p,q);
17+
return root;
18+
}
19+
}

235_lowestCommonAncestor.java 236_lowestCommonAncestor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public class Solution {
1111
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12-
// bottom to top
12+
// top to bottom!!!
1313
if(root == null) return null;
1414
if(root == p||root == q) return root;
1515
TreeNode left = lowestCommonAncestor(root.left,p,q);

23_mergeKLists.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Time complexity: log(k)*n
2+
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* public class ListNode {
7+
* int val;
8+
* ListNode next;
9+
* ListNode(int x) { val = x; }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode mergeKLists(ListNode[] lists) {
14+
if(lists == null || lists.length == 0) return null;
15+
16+
PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){
17+
public int compare(ListNode l1, ListNode l2){
18+
return l1.val - l2.val;
19+
}
20+
});
21+
22+
for(ListNode l:lists){
23+
if(l!=null){
24+
pq.add(l);
25+
}
26+
}
27+
28+
ListNode fakeNode = new ListNode(0);
29+
ListNode run = fakeNode;
30+
31+
while(!pq.isEmpty()){
32+
ListNode current = pq.poll();
33+
run.next = current;
34+
current = current.next;
35+
run = run.next;
36+
if(current!=null){
37+
pq.add(current);
38+
}
39+
}
40+
41+
return fakeNode.next;
42+
}
43+
}

33_searchInRoatedArray.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution {
2+
public int search(int[] nums, int target) {
3+
int pivot = findPivot(nums);
4+
if (target >= nums[0]){
5+
return searchHelper(nums, target,0, pivot);
6+
} else{
7+
return searchHelper(nums, target,pivot+1, nums.length-1);
8+
}
9+
}
10+
11+
12+
public int searchHelper(int[] nums, int target, int low, int high){
13+
if(low > high) return -1;
14+
int mid = low + (high-low)/2;
15+
if(target == nums[mid]) return mid;
16+
else if(target > nums[mid]) return searchHelper(nums,target,mid+1, high);
17+
else return searchHelper(nums,target,low,mid-1);
18+
}
19+
20+
public int findPivot(int[] nums){
21+
int i = 0;
22+
while(i < nums.length -1){
23+
if(nums[i] > nums[i+1]){
24+
return i;
25+
}
26+
i++;
27+
}
28+
return i;
29+
}
30+
}

3_lengthOfLongestSubstring.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public int lengthOfLongestSubstring(String s) {
3+
if(s == null) return 0;
4+
boolean[] flag = new boolean[256];
5+
int start = 0;
6+
int result = 0;
7+
8+
for(int i = 0; i < s.length(); i++){
9+
char c = s.charAt(i);
10+
if(flag[c]){
11+
result = Math.max(result, i-start);
12+
for(int k = start; k < i; k++){
13+
if(s.charAt(k) == c){
14+
start = k+1;
15+
break;
16+
}
17+
}
18+
} else{
19+
flag[c] = true;
20+
}
21+
}
22+
return Math.max(result, s.length()-start);
23+
}
24+
}

46_permutation.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Solution {
2+
public List<List<Integer>> permute(int[] nums) {
3+
List<List<Integer>> result = new ArrayList<List<Integer>>();
4+
permuteHelper(nums,0,result);
5+
return result;
6+
}
7+
8+
public void permuteHelper(int[] nums, int start, List<List<Integer>> result){
9+
if(start == nums.length) {
10+
result.add(toList(nums));
11+
}
12+
for(int i = start; i < nums.length;i++){
13+
swap(nums,i,start);
14+
permuteHelper(nums,start+1,result);
15+
swap(nums,i,start);
16+
}
17+
}
18+
19+
public List<Integer> toList(int[] nums){
20+
List<Integer> result = new ArrayList<Integer>();
21+
for(int s: nums){
22+
result.add(s);
23+
}
24+
return result;
25+
}
26+
27+
public void swap(int[] nums, int i, int j){
28+
int temp = nums[i];
29+
nums[i] = nums[j];
30+
nums[j] = temp;
31+
}
32+
}

47_permuteUnique.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class Solution {
2+
public List<List<Integer>> permuteUnique(int[] nums) {
3+
List<List<Integer>> result = new ArrayList<List<Integer>>();
4+
int k = nums.length;
5+
dfs(nums,0,k,result);
6+
return result;
7+
}
8+
9+
public void dfs(int[] nums,int i, int k, List<List<Integer>> result){
10+
if(i == k){
11+
result.add(toList(nums));
12+
}
13+
for(int j = i; j < k; j++){
14+
if(checkNoDuplicate(nums,i,j)){ // Most Important condition to check, if there are no duplicate
15+
swap(nums,i,j);
16+
dfs(nums,i+1,k,result);
17+
swap(nums,i,j);
18+
}
19+
}
20+
}
21+
22+
public List<Integer> toList(int[] nums){
23+
List<Integer> result = new ArrayList<Integer>();
24+
for(int s: nums){
25+
result.add(s);
26+
}
27+
return result;
28+
}
29+
30+
public void swap(int[] nums, int i, int j){
31+
int temp = nums[i];
32+
nums[i] = nums[j];
33+
nums[j] = temp;
34+
}
35+
36+
public boolean checkNoDuplicate(int[] nums, int i, int j){
37+
for(int start = i; start< j; start++){
38+
if(nums[start] == nums[j]){
39+
return false;
40+
}
41+
}
42+
return true;
43+
}
44+
}

0 commit comments

Comments
 (0)