Skip to content

Commit 2ddd72b

Browse files
authored
Merge pull request #1 from algorithm001/master
首次合并主项目的提交
2 parents 186d6bf + 455c09c commit 2ddd72b

31 files changed

+737
-2
lines changed

.gitignore

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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
2.`fork` 后的仓库 `clone` 到本地,然后在本地新建、修改自己的代码作业文件,**注意:** 仅允许在和自己学号对应的目录下新建或修改自己的代码作业。作业完成后,将相关代码 `push` 到自己的 GitHub 远程仓库。
1414
3. 提交 `Pull Request` 给本仓库,同时备注自己的学号(主要是让学员作业有一个统一留存的地方,大家可以互相学习参考)。
1515
4. 在当周公布作业的 issue 下面提交自己仓库中的作业链接(由于提交和接受 pull request 之间有一些时间差,所以建议提交自己仓库对应代码作业的链接),供老师统一查看及点评。
16+
5. 除代码作业外,我们要求学员每周提交一篇当周的学习感想,直接发一个独立的 issue 即可,issue 标题格式参考:https://github.com/algorithm001/algorithm/issues/9 这个 issue 的链接和代码作业链接一并提交至当周的作业 issue 下面,这里可以参考第一周的作业 issue。
1617

1718
## 注意事项
1819

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/LeetCode24.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/LeetCode25.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_102/leetcode_153_102.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* 存两个临时变量,left和right, 如果left>right,则返回right;
3+
* 如果只有一个元素,则返回它自己;
4+
* 如果没有旋转过,则返回第一个元素
5+
*/
6+
class Solution {
7+
public:
8+
int findMin(vector<int>& nums) {
9+
int count = nums.size();
10+
if (count == 1) {
11+
return nums[0];
12+
}
13+
14+
int left = nums[0];
15+
int right = nums[1];
16+
if (left > right) {
17+
return right;
18+
}
19+
20+
int i;
21+
for (i = 2; i < count; i++) {
22+
left = right;
23+
right = nums[i];
24+
25+
if (left > right) {
26+
return right;
27+
}
28+
}
29+
30+
if (i == count) {
31+
return nums[0];
32+
}
33+
34+
return -1;
35+
}
36+
};
37+

Week_01/id_102/leetcode_20_102.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* 堆栈实现
3+
* 满足三个条件则有效
4+
* 1. “左” 压栈
5+
* 2. “右” 在栈顶查找匹配的括号, 如果找到弹出栈顶字符,否则返回false
6+
* 3. 最终"栈" 为空, 返回true, "栈" 非空, 返回false
7+
* 注意,对于第一次输入是')','}'或者']'的情况,一定要判断堆栈是否为空,否则会数组越界
8+
*/
9+
10+
class Solution {
11+
12+
public:
13+
bool isValid(string s) {
14+
vector<char> stack;
15+
map<char, char> spouse;
16+
17+
spouse[')'] = '(';
18+
spouse['}'] = '{';
19+
spouse[']'] = '[';
20+
21+
for (int i = 0; i< s.size(); ++i) {
22+
if (s[i] =='(' || s[i] =='[' || s[i] =='{') {
23+
stack.push_back(s[i]);
24+
} else {
25+
if (stack.empty() || spouse[s[i]] != stack[stack.size()-1]) {
26+
return false;
27+
}
28+
stack.pop_back();
29+
}
30+
}
31+
32+
return stack.empty();
33+
}
34+
};
35+

Week_01/id_127/21.jpeg

1.7 MB
Loading

Week_01/id_127/83.jpeg

1.82 MB
Loading

Week_01/id_127/LeetCode_21_127.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11+
if (l1 == null)
12+
return l2;
13+
if (l2 == null)
14+
return l1;
15+
16+
ListNode cur1 = l1;
17+
ListNode cur2 = l2;
18+
ListNode mergeCur = null;
19+
ListNode mergeHead = null;
20+
21+
if (cur1.val <= cur2.val) {
22+
mergeCur = cur1;
23+
mergeHead = cur1;
24+
cur1 = cur1.next;
25+
} else {
26+
mergeCur = cur2;
27+
mergeHead = cur2;
28+
cur2 = cur2.next;
29+
}
30+
31+
while (cur1 != null && cur2 != null) {
32+
if (cur1.val <= cur2.val) {
33+
mergeCur.next = cur1;
34+
cur1 = cur1.next;
35+
} else {
36+
mergeCur.next = cur2;
37+
cur2 = cur2.next;
38+
}
39+
mergeCur=mergeCur.next;
40+
}
41+
42+
if (cur1 != null) {
43+
mergeCur.next = cur1;
44+
}
45+
46+
if (cur2 != null) {
47+
mergeCur.next = cur2;
48+
}
49+
50+
return mergeHead;
51+
}
52+
53+
/**
54+
* 递归思路没有想到,参考网上的相关答案,理解之后写出
55+
*/
56+
public ListNode mergeTowListsRecursive(ListNode l1, ListNode l2) {
57+
if (l1 == null)
58+
return l2;
59+
if (l2 == null)
60+
return l1;
61+
62+
63+
if (l1.val <= l2.val) {
64+
ListNode head = l1;
65+
head.next = mergeTowListsRecursive(l1.next, l2);
66+
return head;
67+
} else {
68+
ListNode head = l2;
69+
head.next = mergeTowListsRecursive(l1, l2.next);
70+
return head;
71+
}
72+
}
73+
}

Week_01/id_127/LeetCode_83_127.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode deleteDuplicates(ListNode head) {
11+
ListNode cur = head;
12+
while (cur != null && cur.next != null) {
13+
if (cur.val == cur.next.val) {
14+
cur.next = cur.next.next;
15+
} else {
16+
cur = cur.next;
17+
}
18+
}
19+
return head;
20+
}
21+
}

Week_01/id_127/NOTE.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
# 学习笔记
1+
# 学习笔记
2+
3+
### LeetCode-83思路:
4+
5+
见图片: `83.jpeg`
6+
7+
### LeetCode-21思路:
8+

Week_01/id_27/LeetCode_142_027.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
14+
//暴力解法
15+
public ListNode detectCycle(ListNode head) {
16+
17+
18+
Set<ListNode> set = new HashSet<ListNode>();
19+
while (head != null) {
20+
if (set.contains(head))
21+
break;
22+
set.add(head);
23+
head = head.next;
24+
}
25+
return head;
26+
27+
28+
}
29+
30+
public ListNode detectCycle2( ListNode head ) {
31+
if( head == null || head.next == null ){
32+
return null;
33+
}
34+
35+
//1、寻找相遇点
36+
ListNode fp = head, sp = head;
37+
while( fp != null && fp.next != null){
38+
sp = sp.next;
39+
fp = fp.next.next;
40+
if( fp == sp ){
41+
break;
42+
}
43+
}
44+
45+
//2、没有环的情况直接退出
46+
if( fp == null || fp.next == null ){
47+
return null;
48+
}
49+
50+
//3、有环时,slow起点设置为head,fast从相遇点出发,相遇点即为入环点
51+
sp = head;
52+
while( fp != sp ){
53+
sp = sp.next;
54+
fp = fp.next;
55+
}
56+
return sp;
57+
}
58+
}

0 commit comments

Comments
 (0)