Skip to content

Commit 48313df

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 59af695 commit 48313df

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@
431431

432432
[258. Add Digits](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/258.%20Add%20Digits.md)
433433

434+
[263. Ugly Number](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/263.%20Ugly%20Number.md)
435+
436+
[264. Ugly Number II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/264.%20Ugly%20Number%20II.md)
437+
438+
[268. Missing Number](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/268.%20Missing%20Number.md)
439+
440+
434441
## Algorithm(4th_Edition)
435442
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
436443
All java realization codes are placed in different packages.

leetcode/263. Ugly Number.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,19 @@ class Solution {
3939
return false;
4040
}
4141
}
42+
```
43+
44+
### Second time
45+
1. I still used the method of Recursion.
46+
```Java
47+
class Solution {
48+
public boolean isUgly(int num) {
49+
if(num == 1) return true;
50+
if(num <= 0) return false;
51+
if(num % 2 == 0) return isUgly(num / 2);
52+
else if(num % 3 == 0) return isUgly(num / 3);
53+
else if(num % 5 == 0) return isUgly(num / 5);
54+
else return false;
55+
}
56+
}
4257
```

leetcode/264. Ugly Number II.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,27 @@ class Solution {
3131
return false;
3232
}
3333
}
34+
```
35+
36+
### Second time
37+
1. This question can be solved by dp
38+
2. except for 1, all the other values in dp array should be 2,3 or 5 multipling a previous value in the array.
39+
3. We hold three indices for 2, 3 and 5 so we can know what is the "current" value this value is multipling to.
40+
4. For each index in dp array, we have a change to select which numbers(may duplicate) by comparing the value, and easy to understand, dp[i] save the minimum number hold by (2, 3, 5).
41+
```Java
42+
class Solution {
43+
public int nthUglyNumber(int n) {
44+
int[] dp = new int[n + 1];
45+
dp[0] = 1;
46+
int index2 = 0, index3 = 0, index5 = 0;
47+
int factor2 = 2, factor3 = 3, factor5 = 5;
48+
for(int i = 1; i <= n; i++){
49+
dp[i] = Math.min(factor2, Math.min(factor3, factor5));
50+
if(dp[i] == factor2) factor2 = dp[++index2] * 2;
51+
if(dp[i] == factor3) factor3 = dp[++index3] * 3;
52+
if(dp[i] == factor5) factor5 = dp[++index5] * 5;
53+
}
54+
return dp[n - 1];
55+
}
56+
}
3457
```

leetcode/268. Missing Number.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,19 @@ class Solution {
3737
return sum;
3838
}
3939
}
40+
```
41+
42+
###Second time
43+
1. We first get the sum of the array and the missing value is the deduction between expect sum and sum.
44+
```Java
45+
class Solution {
46+
public int missingNumber(int[] nums) {
47+
int sum = 0;
48+
for(int num : nums){
49+
sum += num;
50+
}
51+
int expect = (nums.length) * (nums.length + 1) / 2;
52+
return expect - sum;
53+
}
54+
}
4055
```

0 commit comments

Comments
 (0)