Skip to content

Commit 2f2c3e1

Browse files
authored
Merge pull request #1 from algorithm001/master
sync remote
2 parents c078a7f + add1218 commit 2f2c3e1

Some content is hidden

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

57 files changed

+2328
-3
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

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_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_122/LeetCode_142_122.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def detectCycle(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
if head is None or head.next is None or head.next.next is None:
14+
return
15+
p1 = head.next
16+
p2 = head.next.next
17+
while(p2 is not None and p2.next is not None and p2.next.next is not None):
18+
p1 = p1.next
19+
p2 = p2.next.next
20+
if p1 is p2:
21+
p1 = head
22+
while p1 is not p2:
23+
p1 = p1.next
24+
p2 = p2.next
25+
return p1
26+
return

Week_01/id_122/LeetCode_21_122.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: yuanyunxu
5+
* Date: 19/4/16
6+
* Time: ÏÂÎç3:41
7+
*/
8+
9+
/**
10+
* Definition for a singly-linked list.
11+
* class ListNode {
12+
* public $val = 0;
13+
* public $next = null;
14+
* function __construct($val) { $this->val = $val; }
15+
* }
16+
*/
17+
18+
class ListNode {
19+
public $val = 0;
20+
public $next = null;
21+
function __construct($val) { $this->val = $val; }
22+
}
23+
24+
class Solution {
25+
26+
/**
27+
* @param ListNode $l1
28+
* @param ListNode $l2
29+
* @return ListNode
30+
*/
31+
function mergeTwoLists($l1, $l2) {
32+
if ($l1 == null) {
33+
return $l2;
34+
}
35+
36+
if ($l2 == null) {
37+
return $l1;
38+
}
39+
40+
$first = $l1;
41+
$second = $l2;
42+
43+
if ($first->val <= $second->val) {
44+
$head = $first;
45+
$first = $first->next;
46+
} else {
47+
$head = $second;
48+
$second = $second->next;
49+
}
50+
51+
$now = $head;
52+
53+
while (true) {
54+
if ($first == null) {
55+
$now->next = $second;
56+
break;
57+
}
58+
if ($second == null) {
59+
$now->next = $first;
60+
break;
61+
}
62+
63+
if ($first->val <= $second->val) {
64+
$now->next = $first;
65+
$first = $first->next;
66+
$now = $now->next;
67+
continue;
68+
}
69+
$now->next = $second;
70+
$second = $second->next;
71+
$now = $now->next;
72+
}
73+
return $head;
74+
}
75+
}

Week_01/id_122/LeetCode_24_122.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Definition for a singly-linked list.
4+
* class ListNode {
5+
* public $val = 0;
6+
* public $next = null;
7+
* function __construct($val) { $this->val = $val; }
8+
* }
9+
*/
10+
class ListNode {
11+
public $val = 0;
12+
public $next = null;
13+
function __construct($val) { $this->val = $val; }
14+
}
15+
16+
class Solution {
17+
18+
/**
19+
* @param ListNode $head
20+
* @return ListNode
21+
*/
22+
function swapPairs($head) {
23+
24+
//没有元素,或者只有一个元素
25+
if ($head == null || $head->next == null) {
26+
return $head;
27+
}
28+
$shaoBing = new ListNode(null);
29+
$shaoBing->next = $head->next;
30+
31+
//到这里至少链表中至少有两个元素
32+
$prev = $head;
33+
$prior = $prev->next;
34+
$next = $prior->next;
35+
36+
while ($next && $next->next) {
37+
$prev->next = $next->next;
38+
$prior->next = $prev;
39+
$prev = $next;
40+
$prior = $next->next;
41+
$next = $prior->next;
42+
}
43+
44+
//最后交换末尾两个元素, 针对单数个链表元素做处理
45+
$prev->next = $next ? $next : null;
46+
$prior->next = $prev;
47+
return $shaoBing->next;
48+
}
49+
}

Week_01/id_122/LeetCode_83_122.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: yuanyunxu
5+
* Date: 19/4/16
6+
* Time: ÏÂÎç3:15
7+
*/
8+
9+
/**
10+
* Definition for a singly-linked list.
11+
* class ListNode {
12+
* public $val = 0;
13+
* public $next = null;
14+
* function __construct($val) { $this->val = $val; }
15+
* }
16+
*/
17+
18+
class ListNode {
19+
public $val = 0;
20+
public $next = null;
21+
function __construct($val) { $this->val = $val; }
22+
}
23+
24+
class Solution {
25+
26+
/**
27+
* @param ListNode $head
28+
* @return ListNode
29+
*/
30+
function deleteDuplicates($head) {
31+
if ($head == null) {
32+
return $head;
33+
}
34+
$prior = $head;
35+
while ($prior->next) {
36+
$next = $prior->next;
37+
if ($prior->val == $next->val) {
38+
$prior->next = $next->next;
39+
$next->next = null;
40+
continue;
41+
}
42+
$prior = $prior->next;
43+
}
44+
return $head;
45+
}
46+
}

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+

0 commit comments

Comments
 (0)