Skip to content

Commit c4f1640

Browse files
committed
Solved 3 problems
1 parent 2ea1094 commit c4f1640

3 files changed

+68
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
class Solution {
11+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12+
13+
if (root.val < p.val && root.val < q.val) {
14+
return lowestCommonAncestor(root.right, p, q);
15+
}
16+
else if (root.val > p.val && root.val > q.val) {
17+
return lowestCommonAncestor(root.left, p, q);
18+
}
19+
else {
20+
return root;
21+
}
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
int low = 0 ;
4+
int high = nums.length-1;
5+
while(low<high) {
6+
int mid = (low+high)/2;
7+
if (nums[mid] > nums[high]) {
8+
low = mid + 1;
9+
}
10+
else if (nums[mid] < nums[high]) {
11+
high = mid;
12+
}
13+
}
14+
15+
return nums[low];
16+
}
17+
}

Medium/Sort Colors.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public void sortColors(int[] nums) {
3+
int numZero = 0;
4+
int numOne = 0;
5+
int numTwo = 0;
6+
7+
for(int i=0;i<nums.length;i++) {
8+
if(nums[i] == 0) numZero++;
9+
if(nums[i] == 1) numOne++;
10+
if(nums[i] == 2) numTwo++;
11+
}
12+
13+
for(int i=0;i<nums.length;i++) {
14+
if (numZero != 0) {
15+
nums[i] = 0;
16+
numZero--;
17+
}
18+
else if(numZero == 0 && numOne != 0) {
19+
nums[i] = 1;
20+
numOne--;
21+
}
22+
else {
23+
nums[i] = 2;
24+
numTwo--;
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)