Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81d9fdd

Browse files
committed
feat: update solutions to lc/lcof2 problems
lc No.0187.Repeated DNA Sequences lc No.0206, lcof2 No.024.Reverse Linked List
1 parent 13e2cef commit 81d9fdd

File tree

16 files changed

+480
-219
lines changed

16 files changed

+480
-219
lines changed

‎lcof2/剑指 Offer II 024. 反转链表/README.md‎

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@
5050

5151
<p><meta charset="UTF-8" />注意:本题与主站 206&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/reverse-linked-list/">https://leetcode-cn.com/problems/reverse-linked-list/</a></p>
5252

53-
5453
## 解法
5554

56-
<!-- 这里可写通用的实现逻辑 -->
55+
定义指针 `p``q` 分别指向头节点和下一个节点,`pre` 指向头节点的前一个节点。
56+
57+
遍历链表,改变指针 `p` 指向的节点的指向,将其指向 `pre` 指针指向的节点,即 `p.next = pre`。然后 `pre` 指针指向 `p`,`p``q` 指针往前走。
58+
59+
当遍历结束后,返回 `pre` 指针即可。
5760

5861
<!-- tabs:start -->
5962

@@ -133,44 +136,105 @@ class Solution {
133136
```js
134137
/**
135138
* Definition for singly-linked list.
136-
* function ListNode(val) {
137-
* this.val = val;
138-
* this.next = null;
139+
* function ListNode(val, next) {
140+
* this.val = (val===undefined ? 0 : val)
141+
* this.next = (next===undefined ? null : next)
139142
* }
140143
*/
141144
/**
142145
* @param {ListNode} head
143146
* @return {ListNode}
144147
*/
145-
var reverseList = function (head) {
146-
let node = head;
147-
let pre = null;
148-
while (node) {
149-
let cur = node;
150-
node = cur.next;
151-
cur.next = pre;
152-
pre = cur;
153-
}
154-
return pre;
148+
var reverseList = function(head) {
149+
let pre = null;
150+
for (let p = head; p;) {
151+
let q = p.next;
152+
p.next = pre;
153+
pre = p;
154+
p = q;
155+
}
156+
return pre;
155157
};
156158
```
157159

158160
### **Go**
159161

160162
```go
163+
/**
164+
* Definition for singly-linked list.
165+
* type ListNode struct {
166+
* Val int
167+
* Next *ListNode
168+
* }
169+
*/
161170
func reverseList(head *ListNode) *ListNode {
162-
if head == nil ||head.Next == nil {
163-
return head
171+
var pre *ListNode
172+
for p := head; p != nil; {
173+
q := p.Next
174+
p.Next = pre
175+
pre = p
176+
p = q
177+
}
178+
return pre
179+
}
180+
```
181+
182+
### **C++**
183+
184+
```cpp
185+
/**
186+
* Definition for singly-linked list.
187+
* struct ListNode {
188+
* int val;
189+
* ListNode *next;
190+
* ListNode() : val(0), next(nullptr) {}
191+
* ListNode(int x) : val(x), next(nullptr) {}
192+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
193+
* };
194+
*/
195+
class Solution {
196+
public:
197+
ListNode* reverseList(ListNode* head) {
198+
ListNode* pre = nullptr;
199+
ListNode* p = head;
200+
while (p)
201+
{
202+
ListNode* q = p->next;
203+
p->next = pre;
204+
pre = p;
205+
p = q;
206+
}
207+
return pre;
164208
}
165-
dummyHead := &ListNode{}
166-
cur := head
167-
for cur != nil {
168-
tmp := cur.Next
169-
cur.Next = dummyHead.Next
170-
dummyHead.Next = cur
171-
cur = tmp
209+
};
210+
```
211+
212+
### **C#**
213+
214+
```cs
215+
/**
216+
* Definition for singly-linked list.
217+
* public class ListNode {
218+
* public int val;
219+
* public ListNode next;
220+
* public ListNode(int val=0, ListNode next=null) {
221+
* this.val = val;
222+
* this.next = next;
223+
* }
224+
* }
225+
*/
226+
public class Solution {
227+
public ListNode ReverseList(ListNode head) {
228+
ListNode pre = null;
229+
for (ListNode p = head; p != null;)
230+
{
231+
ListNode t = p.next;
232+
p.next = pre;
233+
pre = p;
234+
p = t;
235+
}
236+
return pre;
172237
}
173-
return dummyHead.Next
174238
}
175239
```
176240

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* reverseList(ListNode* head) {
14+
ListNode* pre = nullptr;
15+
ListNode* p = head;
16+
while (p)
17+
{
18+
ListNode* q = p->next;
19+
p->next = pre;
20+
pre = p;
21+
p = q;
22+
}
23+
return pre;
24+
}
25+
};
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
112
public class Solution {
213
public ListNode ReverseList(ListNode head) {
3-
ListNode newHead = null;
4-
while(head!= null)
14+
ListNode pre = null;
15+
for(ListNodep=head;p!= null;)
516
{
6-
varnext = head.next;
7-
head.next = newHead;
8-
newHead = head;
9-
head = next;
17+
ListNodet = p.next;
18+
p.next = pre;
19+
pre = p;
20+
p = t;
1021
}
11-
return newHead;
22+
return pre;
1223
}
1324
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
18
func reverseList(head *ListNode) *ListNode {
2-
if head == nil ||head.Next == nil {
3-
return head
4-
}
5-
dummyHead := &ListNode{}
6-
cur := head
7-
for cur != nil {
8-
tmp := cur.Next
9-
cur.Next = dummyHead.Next
10-
dummyHead.Next = cur
11-
cur = tmp
12-
}
13-
return dummyHead.Next
9+
var pre *ListNode
10+
for p := head; p != nil; {
11+
q := p.Next
12+
p.Next = pre
13+
pre = p
14+
p = q
15+
}
16+
return pre
1417
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
/**
22
* Definition for singly-linked list.
3-
* function ListNode(val) {
4-
* this.val = val;
5-
* this.next = null;
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
66
* }
77
*/
88
/**
99
* @param {ListNode} head
1010
* @return {ListNode}
1111
*/
12-
var reverseList = function (head) {
13-
let node = head;
14-
let pre = null;
15-
while (node) {
16-
let cur = node;
17-
node = cur.next;
18-
cur.next = pre;
19-
pre = cur;
20-
}
21-
return pre;
22-
};
12+
var reverseList = function(head) {
13+
let pre = null;
14+
for (let p = head; p;) {
15+
let q = p.next;
16+
p.next = pre;
17+
pre = p;
18+
p = q;
19+
}
20+
return pre;
21+
};

‎solution/0100-0199/0187.Repeated DNA Sequences/README.md‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@
4949
```python
5050
class Solution:
5151
def findRepeatedDnaSequences(self, s: str) -> List[str]:
52-
n = 10
53-
subs = set()
54-
res = set()
55-
for i in range(len(s) -n + 1):
56-
sub = s[i:i + n]
57-
ifsubin subs:
58-
res.add(sub)
59-
subs.add(sub)
60-
return list(res)
52+
n = len(s) -10
53+
cnt = collections.Counter()
54+
ans = []
55+
for i in range(n + 1):
56+
sub = s[i:i + 10]
57+
cnt[sub] +=1
58+
if cnt[sub] ==2:
59+
ans.append(sub)
60+
return ans
6161
```
6262

6363
### **Java**
@@ -67,17 +67,17 @@ class Solution:
6767
```java
6868
class Solution {
6969
public List<String> findRepeatedDnaSequences(String s) {
70-
int len = 10;
71-
Set<String> subs = new HashSet<>();
72-
Set<String> res = new HashSet<>();
73-
for (int i = 0; i < s.length() - len + 1; ++i) {
74-
String sub = s.substring(i, i + len);
75-
if (subs.contains(sub)) {
76-
res.add(sub);
70+
int n = s.length() - 10;
71+
Map<String, Integer> cnt = new HashMap<>();
72+
List<String> ans = new ArrayList<>();
73+
for (int i = 0; i <= n; ++i) {
74+
String sub = s.substring(i, i + 10);
75+
cnt.put(sub, cnt.getOrDefault(sub, 0) + 1);
76+
if (cnt.get(sub) == 2) {
77+
ans.add(sub);
7778
}
78-
subs.add(sub);
7979
}
80-
return newArrayList<>(res);
80+
return ans;
8181
}
8282
}
8383
```
@@ -90,17 +90,17 @@ class Solution {
9090
* @return {string[]}
9191
*/
9292
var findRepeatedDnaSequences = function(s) {
93-
let n = 10;
94-
let subs = new Set();
95-
let res = new Set();
96-
for (let i = 0; i < s.length - n + 1; i++) {
97-
let sub = s.slice(i, i + n);
98-
if (subs.has(sub)) {
99-
res.add(sub);
93+
const n = s.length - 10;
94+
let cnt = new Map();
95+
let ans = [];
96+
for (let i = 0; i <= n; ++i) {
97+
let sub = s.slice(i, i + 10);
98+
cnt[sub] = (cnt[sub] || 0) + 1;
99+
if (cnt[sub] == 2) {
100+
ans.push(sub);
100101
}
101-
subs.add(sub);
102102
}
103-
return [...res];
103+
return ans;
104104
};
105105
```
106106

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /