Skip to content

Commit 8bb792b

Browse files
committed
feat: finish those
1 parent 89afc4e commit 8bb792b

File tree

9 files changed

+210
-157
lines changed

9 files changed

+210
-157
lines changed

.idea/workspace.xml

Lines changed: 21 additions & 155 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists)
2+
3+
## 2019/09/03
4+
5+
### 题目 💗[easy]
6+
7+
Write a program to find the node at which the intersection of two singly linked lists begins.
8+
9+
For example, the following two linked lists:
10+
11+
begin to intersect at node c1.
12+
13+
Example 1:
14+
15+
```bash
16+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
17+
Output: Reference of the node with value = 8
18+
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
19+
```
20+
21+
Example 2:
22+
23+
```bash
24+
Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
25+
Output: Reference of the node with value = 2
26+
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
27+
```
28+
29+
Example 3:
30+
31+
```bash
32+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
33+
Output: null
34+
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
35+
Explanation: The two lists do not intersect, so return null.
36+
```
37+
38+
Notes:
39+
40+
- If the two linked lists have no intersection at all, return null.
41+
- The linked lists must retain their original structure after the function returns.
42+
- You may assume there are no cycles anywhere in the entire linked structure.
43+
- Your code should preferably run in O(n) time and use only O(1) memory.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package getIntersectionNode
2+
3+
import (
4+
"github.com/pengliheng/leetcode/Helper"
5+
)
6+
7+
/**
8+
* Definition for singly-linked list.
9+
* type ListNode struct {
10+
* Val int
11+
* Next *ListNode
12+
* }
13+
*/
14+
15+
type ListNode = Helper.ListNode
16+
17+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
18+
a, b := headA, headB
19+
for a != b {
20+
if a == nil {
21+
a = headB
22+
} else {
23+
a = a.Next
24+
}
25+
if b == nil {
26+
b = headA
27+
} else {
28+
b = b.Next
29+
}
30+
}
31+
return a
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package getIntersectionNode
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 *Helper.ListNode
13+
N2 *Helper.ListNode
14+
ans int
15+
}{
16+
{
17+
Helper.Ints2LinkList([]int{4, 1, 8, 4, 5}),
18+
Helper.Ints2LinkList([]int{5, 0, 1, 8, 4, 5}),
19+
8,
20+
},
21+
}
22+
23+
func Test_bitwiseComplement(t *testing.T) {
24+
ast := assert.New(t)
25+
for _, tc := range tcs {
26+
ast.Equal(tc.ans, getIntersectionNode(tc.N1, tc.N2), "输入:%v", tc)
27+
}
28+
}

algorithm/55.JumpGame/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [55. Jump Game](https://leetcode.com/problems/jump-game/)
2+
3+
## 2019/09/03
4+
5+
### 题目 💗[easy]
6+
7+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
8+
9+
Each element in the array represents your maximum jump length at that position.
10+
11+
Determine if you are able to reach the last index.
12+
13+
Example 1:
14+
15+
```bash
16+
Input: [2,3,1,1,4]
17+
Output: true
18+
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
19+
```
20+
21+
Example 2:
22+
23+
```bash
24+
Input: [3,2,1,0,4]
25+
Output: false
26+
Explanation: You will always arrive at index 3 no matter what. Its maximum
27+
jump length is 0, which makes it impossible to reach the last index.
28+
```

algorithm/55.JumpGame/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package canJump
2+
3+
func canJump(nums []int) bool {
4+
res := helper(nums)
5+
return res < 3
6+
}
7+
8+
func helper(nums []int) int {
9+
if len(nums) == 1 {
10+
return 0
11+
}
12+
stepRange := nums[0]
13+
if stepRange >= len(nums)-1 {
14+
return 0
15+
} else {
16+
container := []int{}
17+
for i := 1; i <= stepRange; i++ {
18+
container = append(container, 1+helper(nums[i:]))
19+
}
20+
return min(container)
21+
}
22+
}
23+
24+
func min(nums []int) int {
25+
res := nums[0]
26+
for _, e := range nums {
27+
if res > e {
28+
res = e
29+
}
30+
}
31+
return res
32+
}

0 commit comments

Comments
 (0)