Skip to content

Commit ce8bb87

Browse files
committed
feat(141): finish 141
1 parent 61c447b commit ce8bb87

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

Helper/LinkList.go

+22
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,25 @@ func Ints2LinkList(ints []int) *ListNode {
3131
}
3232
return root
3333
}
34+
35+
36+
// Ints2ListWithCycle returns a list whose tail point to pos-indexed node
37+
// head's index is 0
38+
// if pos = -1, no cycle
39+
func Ints2ListWithCycle(nums []int, pos int) *ListNode {
40+
head := Ints2LinkList(nums)
41+
if pos == -1 {
42+
return head
43+
}
44+
c := head
45+
for pos > 0 {
46+
c = c.Next
47+
pos--
48+
}
49+
tail := c
50+
for tail.Next != nil {
51+
tail = tail.Next
52+
}
53+
tail.Next = c
54+
return head
55+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [485. Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)
2+
3+
## 2019/06/05
4+
5+
### 题目 💗[easy]
6+
7+
Given a binary array, find the maximum number of consecutive 1s in this array.
8+
9+
给定一个二进制数组,找到最大连续的数字
10+
11+
Example 1:
12+
13+
```bash
14+
Input: [1,1,0,1,1,1]
15+
Output: 3
16+
Explanation: The first two digits or the last three digits are consecutive 1s.
17+
The maximum number of consecutive 1s is 3.
18+
```
19+
20+
Note:
21+
22+
1. The input array will only contain 0 and 1.
23+
做这个输入数组将会是只是 0/1
24+
25+
2. The length of input array is a positive integer and will not exceed 10,000
26+
这个数组长度是整数,正数,不会超过 10000

algorithm/141.LinkedListCycle/main.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package hasCycle
2+
3+
import (
4+
"www/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+
// 快慢指针
18+
func hasCycle(head *ListNode) bool {
19+
fast, slow := head, head
20+
for fast != nil && slow != nil && fast.Next != nil {
21+
fast = fast.Next.Next
22+
slow = slow.Next
23+
if fast == slow {
24+
return true
25+
}
26+
}
27+
return false
28+
}
29+
30+
// for
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package hasCycle
2+
3+
import (
4+
"testing"
5+
"www/leetcode/Helper"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
var tcs = []struct {
11+
N1 *ListNode
12+
ans bool
13+
}{
14+
{
15+
Helper.Ints2ListWithCycle([]int{3, 2, 0, -4}, 1),
16+
true,
17+
},
18+
}
19+
20+
func Test_bitwiseComplement(t *testing.T) {
21+
ast := assert.New(t)
22+
for _, tc := range tcs {
23+
ast.Equal(tc.ans, hasCycle(tc.N1), "输入:%v", tc)
24+
}
25+
}

0 commit comments

Comments
 (0)