Skip to content

Commit 5793bd3

Browse files
committed
1118
1 parent e4164a7 commit 5793bd3

File tree

5 files changed

+79
-47
lines changed

5 files changed

+79
-47
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ Feel free to submit pull requests, add issues and be a contributer.
260260
## Math
261261
| Website | Title | Solution | Time | Space | Difficulty | Note|
262262
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
263+
| Leetcode | [204. Count Primes](https://leetcode.com/problems/count-primes/description/) | [Java](./java/countPrimes.java) | _O(n)_ | _O(n)_ | Easy | |
263264
| Leetcode | [223. Rectangle Area](https://leetcode.com/problems/rectangle-area/description/) | [Java](./java/computeArea.java) | _O(1)_ | _O(1)_ | Medium | |
264265
| Leetcode | [319. Bulb Switcher](https://leetcode.com/problems/bulb-switcher/description/) | [Java](./java/bulbSwitch.java) | _O(n)_ | _O(1)_ | Medium | |
265266

java/countPrimes.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int countPrimes(int n) {
3+
boolean [] notPrime = new boolean[n+1];
4+
int count = 0;
5+
for(int i=2; i<n; i++)
6+
{
7+
if(notPrime[i] == false)
8+
{
9+
count++;
10+
for(int j=2; i*j<n; j++)
11+
notPrime[i*j] = true;
12+
}
13+
}
14+
return count;
15+
}
16+
}

java/isBalanced.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +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-
class Solution {
1+
public class Solution {
112
public boolean isBalanced(TreeNode root) {
12-
13-
if(root == null)
14-
return true;
15-
16-
return getHeight(root) != -1;
17-
3+
return getDepth(root)!=-1;
184
}
195

20-
public int getHeight(TreeNode root)
21-
{
22-
if(root == null)
23-
return -1;
24-
25-
int l = getHeight(root.left);
26-
int r = getHeight(root.right);
27-
28-
if(l == -1 || r == -1 || Math.abs(l-r) > 1)
29-
return -1;
30-
31-
return 1 + Math.max(l,r);
6+
public int getDepth(TreeNode root){
7+
if(root==null){
8+
return 0;
9+
}
10+
int left = getDepth(root.left);
11+
if(left!=-1){
12+
int right = getDepth(root.right);
13+
if(right!=-1){
14+
return Math.abs(left-right)<=1?1+Math.max(left,right):-1;
15+
}
16+
}
17+
return -1;
3218
}
3319
}

java/nthUglyNumber.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,22 @@
88
We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, …) multiply 2, 3, 5. Then we use similar merge method as merge sort, to get every ugly number from the three subsequence. Every step we choose the smallest one, and move one step after.
99
1010
*/
11-
12-
class Solution {
11+
public class Solution {
1312
public int nthUglyNumber(int n) {
14-
15-
if(n == 1) return 1;
16-
17-
int[] dp = new int[n+1];
18-
dp[1]=1;
19-
int p2 = 1, p3 = 1, p5 = 1;
20-
21-
for(int i=2; i<=n; i++)
22-
{
23-
dp[i] = Math.min(p2*2, Math.min(p3*3, p5*5));
24-
25-
if(dp[i] == 2*dp[p2]) p2++;
26-
if(dp[i] == 3*dp[p3]) p3++;
27-
if(dp[i] == 5*dp[p5]) p5++;
28-
}
29-
return dp[n];
30-
13+
int[] ugly = new int[n];
14+
ugly[0] = 1;
15+
int index2 = 0, index3 = 0, index5 = 0;
16+
int factor2 = 2, factor3 = 3, factor5 = 5;
17+
for(int i=1;i<n;i++){
18+
int min = Math.min(Math.min(factor2,factor3),factor5);
19+
ugly[i] = min;
20+
if(factor2 == min)
21+
factor2 = 2*ugly[++index2];
22+
if(factor3 == min)
23+
factor3 = 3*ugly[++index3];
24+
if(factor5 == min)
25+
factor5 = 5*ugly[++index5];
26+
}
27+
return ugly[n-1];
3128
}
3229
}

java/sumOfLeftLeaves.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 int sumOfLeftLeaves(TreeNode root) {
12+
if(root == null) return 0;
13+
int ans = 0;
14+
Stack<TreeNode> stack = new Stack<TreeNode>();
15+
stack.push(root);
16+
17+
while(!stack.empty()) {
18+
TreeNode node = stack.pop();
19+
if(node.left != null) {
20+
if (node.left.left == null && node.left.right == null)
21+
ans += node.left.val;
22+
else
23+
stack.push(node.left);
24+
}
25+
if(node.right != null) {
26+
if (node.right.left != null || node.right.right != null)
27+
stack.push(node.right);
28+
}
29+
}
30+
return ans;
31+
}
32+
}

0 commit comments

Comments
 (0)