Skip to content

Commit 03039a4

Browse files
committed
added a few questions
1 parent 26b8f53 commit 03039a4

File tree

13 files changed

+459
-0
lines changed

13 files changed

+459
-0
lines changed

.DS_Store

-1 KB
Binary file not shown.
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package countCompleteTreeNodes;
2+
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode(int x) { val = x; }
11+
* }
12+
*/
13+
public class Solution {
14+
public int countNodes(TreeNode root) {
15+
int res = 0;
16+
17+
TreeNode node = root;
18+
while(node != null){
19+
res++;
20+
// int leftDepth = getDepth(root.left);
21+
// int rightDepth = getDepth(root.right);
22+
int leftDepth = getDepth(node.left);
23+
int rightDepth = getDepth(node.right);
24+
if(leftDepth == rightDepth){
25+
res = leftDepth == 0 ? res : res + (1 << leftDepth) - 1;
26+
node = node.right;
27+
}else{
28+
res = rightDepth == 0 ? res : res + (1 << rightDepth) - 1;
29+
node = node.left;
30+
}
31+
}
32+
33+
return res;
34+
}
35+
36+
public int getDepth(TreeNode root){
37+
if(root == null)
38+
return 0;
39+
40+
return getDepth(root.left) + 1;
41+
}
42+
}

src/countCompleteTreeNodes/Test.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package countCompleteTreeNodes;
2+
3+
public class Test {
4+
public static void main(String[] args){
5+
TreeNode root = new TreeNode(1);
6+
TreeNode left = new TreeNode(2);
7+
TreeNode right = new TreeNode(3);
8+
root.left = left;
9+
root.right = right;
10+
11+
Solution sol = new Solution();
12+
System.out.println(sol.countNodes(root));
13+
14+
System.out.println(sol.getDepth(root));
15+
System.out.println(sol.getDepth(left));
16+
System.out.println(sol.getDepth(right));
17+
18+
19+
}
20+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package countCompleteTreeNodes;
2+
3+
public class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
8+
TreeNode(int x) {
9+
val = x;
10+
}
11+
}

src/courseScheduleII/Solution.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package courseScheduleII;
2+
import java.util.*;
3+
4+
public class Solution {
5+
public int[] findOrder(int numCourses, int[][] prerequisites) {
6+
int[] res = new int[numCourses];
7+
if(prerequisites == null){
8+
return null;
9+
}
10+
11+
Map<Integer, Integer> indegree = new HashMap<Integer, Integer>();
12+
Map<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
13+
for(int i = 0; i < prerequisites.length; i++){
14+
for(int j = 0; j < prerequisites[0].length; j++){
15+
// prerequisites[i][j], j --> i, indegree of i + 1
16+
if(!indegree.containsKey(i)){
17+
indegree.put(i, 1);
18+
}else{
19+
indegree.put(i, indegree.get(i) + 1);
20+
}
21+
if(!map.containsKey(j)){
22+
// assume no duplicate pairs
23+
ArrayList<Integer> val = new ArrayList<Integer>();
24+
val.add(i);
25+
map.put(j, val);
26+
}else{
27+
map.get(j).add(i);
28+
}
29+
}
30+
}
31+
32+
Queue<Integer> queue = new LinkedList<Integer>();
33+
34+
for(int k = 0; k < numCourses; k++){
35+
// if(!map.containsKey(k)){
36+
if(indegree.get(k) == null){
37+
queue.offer(k);
38+
}
39+
}
40+
41+
int index = 0;
42+
while(!queue.isEmpty()){
43+
int cur = queue.poll();
44+
res[index++] = cur;
45+
if(map.containsKey(cur)){
46+
ArrayList<Integer> neighbors = map.get(cur);
47+
for(Integer in : neighbors){
48+
if(indegree.containsKey(in)){
49+
indegree.put(in, indegree.get(in) - 1);
50+
if(indegree.get(in) == 0){
51+
queue.offer(in);
52+
}
53+
}
54+
}
55+
}
56+
57+
}
58+
59+
return res;
60+
}
61+
}

src/courseScheduleII/Test.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package courseScheduleII;
2+
3+
public class Test {
4+
5+
}

src/threeSum/Solution.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package threeSum;
2+
import java.util.*;
3+
4+
public class Solution {
5+
public static List<List<Integer>> threeSum(int[] nums) {
6+
List<List<Integer>> res = new ArrayList<List<Integer>>();
7+
if(nums == null || nums.length == 0)
8+
return res;
9+
10+
Arrays.sort(nums);
11+
int i = nums.length - 1;
12+
while(i >= 2){
13+
if(i != nums.length - 1 && nums[i] == nums[i + 1]){
14+
continue;
15+
}
16+
List<List<Integer>> curRes = twoSum(nums, i - 1, -nums[i]);
17+
for(int j = 0; j < curRes.size(); j++){
18+
curRes.get(j).add(nums[i]);
19+
}
20+
res.addAll(curRes);
21+
i--;
22+
}
23+
return res;
24+
}
25+
26+
private static List<List<Integer>> twoSum(int[] sums, int end, int target){
27+
List<List<Integer>> result = new ArrayList<List<Integer>>();
28+
if(sums == null || sums.length == 0)
29+
return result;
30+
int left = 0;
31+
int right = end;
32+
while(left < right){
33+
if(sums[left] + sums[right] == target){
34+
List<Integer> curList = new ArrayList<Integer>();
35+
curList.add(sums[left]);
36+
curList.add(sums[right]);
37+
result.add(curList);
38+
left++;
39+
right--;
40+
while(left < end && sums[left] == sums[left - 1]){
41+
left++;
42+
}
43+
while(right > 0 && sums[right] == sums[right + 1]){
44+
right--;
45+
}
46+
}else if(sums[left] + sums[right] > target){
47+
right--;
48+
}else{
49+
left++;
50+
}
51+
}
52+
53+
return result;
54+
}
55+
56+
public static void main(String[] args){
57+
int[] input = new int[]{0,0,0,0};
58+
List<List<Integer>> res = threeSum(input);
59+
System.out.print(res.toString());
60+
61+
}
62+
}

src/wildcardMatching/DpSolution.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package wildcardMatching;
2+
3+
public class DpSolution {
4+
public boolean isMatch(String s, String p) {
5+
if(p.length()==0)
6+
return s.length()==0;
7+
boolean[] res = new boolean[s.length()+1];
8+
res[0] = true;
9+
for(int j=0;j<p.length();j++)
10+
{
11+
if(p.charAt(j)!='*')
12+
{
13+
for(int i=s.length()-1;i>=0;i--)
14+
{
15+
res[i+1] = res[i]&&(p.charAt(j)=='?'||s.charAt(i)==p.charAt(j));
16+
}
17+
}
18+
else
19+
{
20+
int i = 0;
21+
while(i<=s.length() && !res[i])
22+
i++;
23+
for(;i<=s.length();i++)
24+
{
25+
res[i] = true;
26+
}
27+
}
28+
res[0] = res[0]&&p.charAt(j)=='*';
29+
}
30+
return res[s.length()];
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package wildcardMatching;
2+
3+
public class RecursiveSolution {
4+
public boolean isMatch(String s, String p) {
5+
return isMatch(s, p, 0, 0);
6+
}
7+
8+
private boolean isMatch(String s, String p, int i, int j){
9+
if(j == p.length())
10+
return i == s.length();
11+
// if current char at p is not *
12+
if(p.charAt(j) != '*'){
13+
if(i == s.length() || p.charAt(j) != s.charAt(i) && p.charAt(j) != '?'){
14+
return false;
15+
}else{
16+
return isMatch(s, p, i + 1, j + 1);
17+
}
18+
}else{
19+
// p.charAt(j) is '*', then three case :
20+
// 1), '*' == '', return isMatch(s, p, i, j + 1)
21+
// 2), '*' == '?', return isMatch(s, p, i + 1, j + 1)
22+
// 3), '*' == '?????' for i < s.length(), return isMatch(s, p, i, p + 1), i++
23+
// in summary, validate isMatch(s, p, i, j + 1) for [i, s.length())
24+
while(i < s.length() && (p.charAt(j) == s.charAt(i) || p.charAt(j) == '?')){
25+
if(isMatch(s, p, i, j + 1)){
26+
return true;
27+
}
28+
i++;
29+
}
30+
}
31+
32+
return isMatch(s, p, i, j + 1); //consider this case : s is "aac", p is "a*b*"
33+
// return false;
34+
}
35+
}

src/wildcardMatching/Test.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package wildcardMatching;
2+
3+
public class Test {
4+
public static void main(String[] args){
5+
RecursiveSolution rs = new RecursiveSolution();
6+
DpSolution dp = new DpSolution();
7+
String s = "aaac";
8+
String p = "a*b";
9+
System.out.println(rs.isMatch(s, p));
10+
System.out.println(dp.isMatch(s, p));
11+
12+
System.out.println("Match s1");
13+
String s1 = "abbbbaaab";
14+
System.out.println(rs.isMatch(s1, p));
15+
System.out.println(dp.isMatch(s1, p));
16+
17+
String s2 = "aabc";
18+
System.out.println("Match s2");
19+
System.out.println(rs.isMatch("aabc", "a*d"));
20+
System.out.println(dp.isMatch("aabc", "a*d"));
21+
22+
System.out.println("Match s3");
23+
String s3 = "a";
24+
System.out.println(rs.isMatch(s3, p));
25+
System.out.println(dp.isMatch(s3, p));
26+
27+
28+
System.out.println("Match s4");
29+
String s4 = "abbaab";
30+
System.out.println(rs.isMatch(s4, p));
31+
System.out.println(dp.isMatch(s4, p));
32+
33+
System.out.println("Match s5");
34+
String s5 = "ac";
35+
System.out.println(rs.isMatch(s5, p));
36+
System.out.println(dp.isMatch(s5, p));
37+
38+
39+
}
40+
}

0 commit comments

Comments
 (0)