Skip to content

Commit 8068731

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 21766c3 commit 8068731

7 files changed

+165
-0
lines changed

Algorithm(4th_Edition)/.idea/workspace.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@
371371

372372
[212. Word Search II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/212.%20Word%20Search%20II.md)
373373

374+
[213. House Robber II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/213.%20House%20Robber%20II.md)
375+
376+
[214. Shortest Palindrome](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/214.%20Shortest%20Palindrome.md)
377+
378+
[215. Kth Largest Element in an Array](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/215.%20Kth%20Largest%20Element%20in%20an%20Array.md)
379+
380+
[216. Combination Sum III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/216.%20Combination%20Sum%20III.md)
381+
382+
[217. Contains Duplicate](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/217.%20Contains%20Duplicate.md)
383+
374384
## Algorithm(4th_Edition)
375385
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
376386
All java realization codes are placed in different packages.

leetcode/213. House Robber II.md

+25
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,29 @@ class Solution {
4848
return Math.max(dp[len], dpp[len]);
4949
}
5050
}
51+
```
52+
53+
### 二刷
54+
1. 这道题肯定是用DP解决,但是要分成两种情况:
55+
* 如果抢了第一家,我们就不能抢最后一家。
56+
* 如果我们抢了最后一家,就不能抢第一家。
57+
* 这两种情况需要我们遍历两次循环,通过for循环控制。
58+
```Java
59+
class Solution {
60+
public int rob(int[] nums) {
61+
if(nums == null || nums.length < 1) return 0;
62+
int len = nums.length;
63+
if(1 == len) return nums[0];
64+
int result = 0;
65+
int[] dp = new int[len + 1];
66+
for(int i = 1; i < len; i++){
67+
dp[i] = Math.max(dp[i - 1], nums[i - 1] + (i >= 2 ? dp[i - 2] : 0));
68+
}
69+
int[] dp1 = new int[len + 1];
70+
for(int i = 2; i <= len; i++){
71+
dp1[i] = Math.max(dp1[i - 1], nums[i - 1] + dp1[i - 2]);
72+
}
73+
return Math.max(dp[len - 1], dp1[len]);
74+
}
75+
}
5176
```

leetcode/214. Shortest Palindrome.md

+44
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,47 @@ class Solution {
6767
}
6868
}
6969
```
70+
71+
### 二刷
72+
1. We can analyze one of the questions: aacecaaa
73+
* We first assume we have a result, a + aacecaaa, so we want to find the "a".
74+
* We create a new String aacecaaa # aaacecaa, where the third part is the reverse of the string and the first part is the original string. So the created string is palindrone.
75+
* We want to find the longest common prefix and suffix. Where we find the a is the remain part of s, if not only a, we need to reverse the remain string.
76+
```Java
77+
class Solution {
78+
public String shortestPalindrome(String s) {
79+
String ss = s + "#" + new StringBuilder(s).reverse().toString();
80+
int len = ss.length();
81+
int[] next = new int[len];
82+
next[0] = -1;
83+
for(int i = 1, j = -1; i < len; i++){
84+
while(j > -1 && ss.charAt(i) != ss.charAt(j + 1)){
85+
j = next[j];
86+
}
87+
if(ss.charAt(j + 1) == ss.charAt(i)) j++;
88+
next[i] = j;
89+
}
90+
return new StringBuilder(ss.substring(next[len - 1] + 1, s.length())).reverse().toString() + s;
91+
}
92+
}
93+
```
94+
95+
### Reference:
96+
1. [KMP](https://seanforfun.github.io/datastructure/2018/11/07/kmp.html)
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+

leetcode/215. Kth Largest Element in an Array.md

+35
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,38 @@ class Solution {
5050
}
5151
}
5252
```
53+
54+
55+
### 二刷
56+
1. Write quick sort by myself though the speed is not fast compared with Arrays.sort()
57+
```Java
58+
class Solution {
59+
public int findKthLargest(int[] nums, int k) {
60+
quickSort(nums, 0, nums.length - 1);
61+
return nums[nums.length - k];
62+
}
63+
private void quickSort(int[] nums, int low, int high){
64+
if(low >= high) return;
65+
int pivot = partition(nums, low, high);
66+
quickSort(nums, low, pivot - 1);
67+
quickSort(nums, pivot + 1, high);
68+
}
69+
private int partition(int[] nums, int low, int high){
70+
int i = low, j = high + 1;
71+
int cmp = nums[low];
72+
while(true){
73+
while(nums[++i] <= cmp) if(i == high) break;
74+
while(nums[--j] > cmp) if(j == low) break;
75+
if(i >= j) break;
76+
swap(nums, i, j);
77+
}
78+
swap(nums, low, j);
79+
return j;
80+
}
81+
private void swap(int[] nums, int i, int j){
82+
int temp = nums[i];
83+
nums[i] = nums[j];
84+
nums[j] = temp;
85+
}
86+
}
87+
```

leetcode/216. Combination Sum III.md

+29
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,32 @@ class Solution {
5050
}
5151
}
5252
```
53+
54+
### 二刷
55+
1. Use backtrace to evaluate.
56+
2. Break out point: check the size of the array and the sum of the numbers in current array.
57+
```Java
58+
class Solution {
59+
public List<List<Integer>> combinationSum3(int k, int n) {
60+
List<List<Integer>> result = new LinkedList<>();
61+
List<Integer> temp = new LinkedList<>();
62+
backtrace(k, n, temp, result, 0, 1);
63+
return result;
64+
}
65+
66+
private void backtrace(int k, int n, List<Integer> temp, List<List<Integer>> result, int sum, int cur){
67+
if(temp.size() == k){
68+
if(sum == n){
69+
List<Integer> copy = new LinkedList<>(temp);
70+
result.add(copy);
71+
}
72+
}else{
73+
for(int i = cur; i <= 9; i++){
74+
temp.add(i);
75+
backtrace(k, n, temp, result, sum + i, i + 1);
76+
temp.remove(temp.size() - 1);
77+
}
78+
}
79+
}
80+
}
81+
```

leetcode/217. Contains Duplicate.md

+15
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,19 @@ class Solution {
2727
return false;
2828
}
2929
}
30+
```
31+
32+
### 二刷
33+
1. Sort the array first and traverse the array from the second index.
34+
2. check if current number equals the previous one, if equal, return true, else false;
35+
```Java
36+
class Solution {
37+
public boolean containsDuplicate(int[] nums) {
38+
Arrays.sort(nums);
39+
for(int i = 1; i < nums.length; i++){
40+
if(nums[i] == nums[i - 1]) return true;
41+
}
42+
return false;
43+
}
44+
}
3045
```

0 commit comments

Comments
 (0)