Skip to content

Commit 0e712f9

Browse files
authored
Merge pull request #1 from algorithm001/master
update
2 parents 9531ac6 + 94f0250 commit 0e712f9

File tree

587 files changed

+23612
-38
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

587 files changed

+23612
-38
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.DS_Store
2+
.idea
3+
.vscode
4+
.vs

Week_01/.DS_Store

6 KB
Binary file not shown.

Week_01/id_0/TestPush.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class TestPush {
2+
public static void main(String[] args) {
3+
System.out.println("Hello Geekbang~~");
4+
}
5+
}

Week_01/id_1/LeetCode_153_1.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
int len = nums.length;
4+
if (len == 1) {
5+
return nums[0];
6+
}
7+
8+
int min = 0;
9+
int max = len - 1;
10+
11+
if (nums[min] < nums[max]) {
12+
return nums[min];
13+
}
14+
15+
int mid = (min + max) / 2;
16+
while (min <= max) {
17+
if (nums[min] < nums[mid]) {
18+
min = mid;
19+
mid = (min + max) / 2;
20+
}else if (nums[min] > nums[mid]){
21+
max = mid;
22+
mid = (min + max) / 2;
23+
}else {
24+
break;
25+
}
26+
}
27+
28+
if (min < len -1) {
29+
if (nums[min] > nums[min + 1]) {
30+
return nums[min + 1];
31+
}
32+
}
33+
return nums[min];
34+
}
35+
}

Week_01/id_1/LeetCode_24_1.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
3+
ListNode prePairTail = null;
4+
5+
public ListNode swapPairs(ListNode head) {
6+
7+
if (head == null) {
8+
return null;
9+
}
10+
11+
if (head.next == null) {
12+
return head;
13+
}
14+
15+
ListNode result = head.next;
16+
17+
ListNode node = head;
18+
ListNode next = head.next;
19+
while (node != null && next != null) {
20+
21+
node = swap(node, next);
22+
23+
if (node == null) {
24+
break;
25+
}
26+
next = node.next;
27+
}
28+
return result;
29+
30+
}
31+
32+
private ListNode swap(ListNode node, ListNode next) {
33+
34+
ListNode tmpNext = next.next;
35+
next.next = node;
36+
node.next = tmpNext;
37+
if (prePairTail != null) {
38+
prePairTail.next = next;
39+
}
40+
prePairTail = node;
41+
return tmpNext;
42+
}
43+
44+
}

Week_01/id_1/LeetCode_25_1.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
class Solution {
3+
4+
ListNode preGroupTail = null;
5+
ListNode result = null;
6+
7+
public ListNode reverseKGroup(ListNode head, int k) {
8+
9+
if (head == null || head.next == null || k == 1) {
10+
return head;
11+
}
12+
13+
int index = 1;
14+
ListNode groupHead = head;
15+
ListNode groupTail = head;
16+
17+
while (true) {
18+
19+
if (index != k) { // 查找 k 个节点中的 head 和 tail 节点
20+
groupTail = groupTail.next;
21+
if (groupTail != null) {
22+
index ++;
23+
continue;
24+
}else {
25+
// 链表长度小于 K 的情况
26+
if (this.preGroupTail != null) {
27+
this.preGroupTail.next = groupHead;
28+
break;
29+
}else {
30+
return head;
31+
}
32+
}
33+
}else {
34+
// 个数达到 k 时执行交换,返回下一组的 head 节点
35+
ListNode node = this.swap(groupHead, groupTail);
36+
if (node == null) {
37+
break;
38+
}
39+
groupHead = node;
40+
groupTail = node;
41+
index = 1;
42+
}
43+
}
44+
return result;
45+
}
46+
47+
private ListNode swap(ListNode head, ListNode tail) {
48+
49+
// 反转链表
50+
ListNode preNode = null;
51+
ListNode currentNode = head;
52+
while (currentNode != tail) {
53+
ListNode next = currentNode.next;
54+
currentNode.next = preNode;
55+
preNode = currentNode;
56+
currentNode = next;
57+
}
58+
59+
// 当前组的 tail 节点执行 swap
60+
ListNode node= currentNode.next;
61+
currentNode.next = preNode;
62+
63+
// 反转完成,将上一组的 tail 节点指向当前组的 head 节点
64+
// 第一次反转时上一组 tail 为 null,因此要执行判空操作
65+
if (this.preGroupTail != null) {
66+
this.preGroupTail.next = currentNode;
67+
}else {
68+
// 上一组的 tail 为空说明是第一组执行交换
69+
// 结果一定是第一组交换后的 head 节点
70+
result = currentNode;
71+
}
72+
this.preGroupTail = head;
73+
return node;
74+
}
75+
}

Week_01/id_1/LeetCode_50_1.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
double myPow(double x, int n) {
3+
double result = this.pow(x, Math.abs(n));
4+
5+
if (n < 0) {
6+
return 1/result;
7+
}
8+
return result;
9+
}
10+
11+
private double pow(double x, int n) {
12+
if (n == 0) {
13+
return 1;
14+
}
15+
double half = pow(x, n / 2);
16+
if (n % 2 == 0) {
17+
return half * half;
18+
} else {
19+
return half * half * x;
20+
}
21+
}
22+
}
23+

Week_01/id_1/LeetCode_698_1.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
public class Solution {
3+
4+
private int average;
5+
private boolean[] mark;
6+
public boolean canPartitionKSubsets(int[] nums, int k) {
7+
8+
if (k == 1) {
9+
return true;
10+
}
11+
12+
int sum = 0;
13+
int len = nums.length;
14+
15+
for (int i = 0; i < len; i ++) {
16+
sum += nums[i];
17+
}
18+
19+
if (sum % k != 0) {
20+
return false;
21+
}
22+
23+
this.average = sum / k;
24+
mark = new boolean[len];
25+
26+
return calSubSum(nums, 0, average, 0, k);
27+
28+
}
29+
30+
public boolean calSubSum(int[] nums, int curSum, int subSum, int start, int k) {
31+
32+
if (k == 1) {
33+
return true;
34+
}
35+
36+
if (curSum == subSum) {
37+
return calSubSum(nums, 0, subSum, 0, k - 1);
38+
}
39+
40+
for (int i = start; i < nums.length; i ++) {
41+
if (mark[i]) {
42+
continue;
43+
}
44+
mark[i] = true;
45+
46+
if (this.calSubSum(nums, curSum + nums[i], subSum, i + 1, k)) {
47+
return true;
48+
}
49+
mark[i] = false;
50+
}
51+
return false;
52+
53+
54+
}
55+
56+
}

Week_01/id_1/NOTE.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,92 @@
1-
# 学习笔记
1+
### 一. 第一周学习总结
2+
3+
#### 1. 关于链表题目的做题总结
4+
5+
做了作业中两道关于链表的题目,结合以前做的链表题目,发现自己用的方法基本有三种:
6+
7+
- 遍历:链表和数组一样,基本的遍历是逃不了的,重点是怎么提高遍历效率的问题。
8+
- 反转:反转也是常用的方式,关于反转最主要的是记录三个节点:当前节点,当前节点的 prev 节点和 next 节点。整个反转过程就是这三个节点的不断变化。然后在反转的过程中可能需要做一些特殊的处理,比如本周的 LeetCode 24、25两道题目,都是要在反转的时候要讲前一组的尾节点的 next 指向本次反转后的头节点。
9+
- 快慢指针:包括查看链表是否有环,获取链表的中间节点等题目都可以用快慢指针的方式进行,当快指针到达末尾时,满指针刚好到达链表的中间,有环的话两者最终有机会相遇。
10+
11+
12+
#### 2. pow(x, n) 解题思路与递归简记
13+
14+
最先想到的就是循环相乘了,先暴力搞出来再说,代码如下
15+
16+
```Java
17+
18+
public class Solution {
19+
double myPow(double x, int n) {
20+
int result = 1;
21+
for (int i = 1; i <= n; i ++) {
22+
result *= x;
23+
}
24+
return result;
25+
}
26+
}
27+
```
28+
29+
这种解法时间复杂度为 O(N),实际提交可能会超时,因此需要改进。这里用到数学知识:
30+
31+
> a^m * a^n = a^(m+n)
32+
33+
因此以 2 的 10 次方为例:
34+
35+
```
36+
2^10 = 2^5 * 2^5
37+
2^5 = 2^2 * 2^2 * 2
38+
2^2 = 2^1 * 2^1
39+
2^1 = 2^0 * 2
40+
```
41+
42+
因此每一个指数幂的计算都可以进行折半计算,这样就可以通过递归的形式进行计算。
43+
44+
递归公式为:
45+
46+
```
47+
n 为偶数: x^n = x^(n/2) * x^(n/2)
48+
n 为奇数: x^n = x^(n/2) * x^(n/2) * x
49+
```
50+
51+
终止条件为:
52+
53+
```
54+
n = 0, x^0 = 1
55+
```
56+
57+
实现代码如下:
58+
59+
```Java
60+
public class Solution {
61+
double myPow(double x, int n) {
62+
63+
double result = this.pow(x, Math.abs(n));
64+
65+
if (n < 0) {
66+
return -n;
67+
}
68+
return n;
69+
}
70+
71+
private double pow(double x, int n) {
72+
if (n == 0) {
73+
return 1;
74+
}
75+
double half = pow(x, n / 2);
76+
if (n % 2 == 0) {
77+
return half * half;
78+
} else {
79+
return half * half * x;
80+
}
81+
}
82+
}
83+
```
84+
85+
最终返回结果要考虑 n 为负数的情况,因此先取绝对值进行计算,如果为负数则返回计算结果的倒数。整个算法的复杂度由 O(N) 变为了 O(logN)。
86+
87+
关于递归最重要的还是要找到递归公式,如王争老师在专栏中提到的三步:
88+
89+
- 分解出子问题,子问题的解题思路通用:这里的 a^m * a^n = a^(m+n) 就是典型的案例。
90+
- 递归过程的计算:每次计算的结果是子问题的结果汇总,这里就是指数为奇数和偶数时的不同的计算公式。
91+
- 终止条件:这里的终止条件就是幂为 0 的时候。
92+

Week_01/id_101/LeetCode_141_101.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package linkedlist.linkedListCycle;
2+
3+
import linkedlist.ListNode;
4+
5+
/**
6+
* @author Seina
7+
* @version 2019-01-14 20:37:52
8+
*/
9+
public class LinkedListCycle {
10+
11+
public boolean hasCycle(ListNode head) {
12+
if (head == null || head.next == null){
13+
return false;
14+
}
15+
ListNode fast = head;
16+
ListNode low = head;
17+
while (fast != null && fast.next != null){
18+
fast = fast.next.next;
19+
low = low.next;
20+
if (fast == low){
21+
return true;
22+
}
23+
}
24+
return false;
25+
}
26+
}

0 commit comments

Comments
 (0)