Skip to content

Commit 46239ce

Browse files
committed
feat: add solutions to lc problem: No.1634
No.1634.Add Two Polynomials Represented as Linked Lists
1 parent ed1a518 commit 46239ce

File tree

8 files changed

+532
-113
lines changed

8 files changed

+532
-113
lines changed

solution/1600-1699/1632.Rank Transform of a Matrix/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ matrix[1][1] 的秩为 3 ,因为 matrix[1][1] > matrix[0][1], matrix[1][1] >
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77-
**方法一:贪心 + 并查集**
77+
**方法一:排序 + 并查集**
7878

79-
先考虑简化情形:没有相同的元素。那么显然最小的元素的秩为 $1$,第二小的元素则要考虑是否和最小元素同行或同列。于是得到贪心解法:从小到大遍历元素,并维护每行、每列的最大秩,该元素的秩即为同行、同列的最大秩加 $1$。见题目:[2371. Minimize Maximum Value in a Grid](/solution/2300-2399/2371.Minimize%20Maximum%20Value%20in%20a%20Grid/README.md)
79+
我们先考虑简化情形:没有相同的元素。那么显然最小的元素的秩为 $1$,第二小的元素则要考虑是否和最小元素同行或同列。于是得到贪心解法:从小到大遍历元素,并维护每行、每列的最大秩,该元素的秩即为同行、同列的最大秩加 $1$。见题目:[2371. 最小化网格中的最大值](/solution/2300-2399/2371.Minimize%20Maximum%20Value%20in%20a%20Grid/README.md)
8080

8181
存在相同元素时则较为复杂,假设两个相同元素同行(或同列),那么就要考虑到两个元素分别对应的行(或列)的最大秩。同时还可能出现联动,比如元素 `a``b` 同行,`b``c` 同列,那么要同时考虑这三个元素。
8282

83-
这种联动容易想到并查集,于是用并查集将相同元素分为几个连通块,对于每个连通块,里面所有元素对应的行或列的最大秩加 $1$,即为该连通块内所有元素的秩。
83+
这种联动容易想到并查集,于是我们用并查集将元素分为几个连通块,对于每个连通块,里面所有元素对应的行或列的最大秩加 $1$,即为该连通块内所有元素的秩。
8484

8585
时间复杂度 $O(m \times n \times \log(m \times n))$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
8686

solution/1600-1699/1634.Add Two Polynomials Represented as Linked Lists/README.md

+185-33
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75-
遍历两多项式链表,比较节点间的 power 值,进行节点串联。若两节点 coefficient 值相加和为 0,不串联此合并的节点。
75+
**方法一:遍历链表**
76+
77+
我们可以同时遍历两个链表,根据指数大小关系,将节点添加到结果链表中。
78+
79+
最后,如果链表 $1$ 或链表 $2$ 还有剩余节点,将其添加到结果链表中。
80+
81+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为两个链表中节点数的较大值。
7682

7783
<!-- tabs:start -->
7884

@@ -90,26 +96,24 @@
9096

9197

9298
class Solution:
93-
def addPoly(self, poly1: 'PolyNode', poly2: 'PolyNode') -> 'PolyNode':
94-
dummy = PolyNode()
95-
cur = dummy
96-
while poly1 or poly2:
97-
if poly1 is None or (poly2 and poly2.power > poly1.power):
98-
cur.next = poly2
99-
cur = cur.next
100-
poly2 = poly2.next
101-
elif poly2 is None or (poly1 and poly1.power > poly2.power):
102-
cur.next = poly1
103-
cur = cur.next
99+
def addPoly(self, poly1: "PolyNode", poly2: "PolyNode") -> "PolyNode":
100+
dummy = curr = PolyNode()
101+
while poly1 and poly2:
102+
if poly1.power > poly2.power:
103+
curr.next = poly1
104104
poly1 = poly1.next
105+
curr = curr.next
106+
elif poly1.power < poly2.power:
107+
curr.next = poly2
108+
poly2 = poly2.next
109+
curr = curr.next
105110
else:
106-
val = poly1.coefficient + poly2.coefficient
107-
if val != 0:
108-
cur.next = PolyNode(x=val, y=poly1.power)
109-
cur = cur.next
111+
if c := poly1.coefficient + poly2.coefficient:
112+
curr.next = PolyNode(c, poly1.power)
113+
curr = curr.next
110114
poly1 = poly1.next
111115
poly2 = poly2.next
112-
cur.next = None
116+
curr.next = poly1 or poly2
113117
return dummy.next
114118
```
115119

@@ -126,40 +130,188 @@ class Solution:
126130
127131
* PolyNode() {}
128132
* PolyNode(int x, int y) { this.coefficient = x; this.power = y; }
129-
* PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next =
130-
next; }
133+
* PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next = next; }
131134
* }
132135
*/
133136

134-
class Solution {
137+
class Solution {
135138
public PolyNode addPoly(PolyNode poly1, PolyNode poly2) {
136139
PolyNode dummy = new PolyNode();
137-
PolyNode cur = dummy;
138-
while (poly1 != null || poly2 != null) {
139-
if (poly1 == null || (poly2 != null && poly2.power > poly1.power)) {
140-
cur.next = poly2;
141-
cur = cur.next;
140+
PolyNode curr = dummy;
141+
while (poly1 != null && poly2 != null) {
142+
if (poly1.power > poly2.power) {
143+
curr.next = poly1;
144+
poly1 = poly1.next;
145+
curr = curr.next;
146+
} else if (poly1.power < poly2.power) {
147+
curr.next = poly2;
142148
poly2 = poly2.next;
143-
} else if (poly2 == null || (poly1 != null && poly1.power > poly2.power)) {
144-
cur.next = poly1;
145-
cur = cur.next;
149+
curr = curr.next;
150+
} else {
151+
int c = poly1.coefficient + poly2.coefficient;
152+
if (c != 0) {
153+
curr.next = new PolyNode(c, poly1.power);
154+
curr = curr.next;
155+
}
156+
poly1 = poly1.next;
157+
poly2 = poly2.next;
158+
}
159+
}
160+
if (poly1 == null) {
161+
curr.next = poly2;
162+
}
163+
if (poly2 == null) {
164+
curr.next = poly1;
165+
}
166+
return dummy.next;
167+
}
168+
}
169+
```
170+
171+
### **C++**
172+
173+
```cpp
174+
/**
175+
* Definition for polynomial singly-linked list->
176+
* struct PolyNode {
177+
* int coefficient, power;
178+
* PolyNode *next;
179+
* PolyNode(): coefficient(0), power(0), next(nullptr) {};
180+
* PolyNode(int x, int y): coefficient(x), power(y), next(nullptr) {};
181+
* PolyNode(int x, int y, PolyNode* next): coefficient(x), power(y), next(next) {};
182+
* };
183+
*/
184+
185+
class Solution {
186+
public:
187+
PolyNode* addPoly(PolyNode* poly1, PolyNode* poly2) {
188+
PolyNode* dummy = new PolyNode();
189+
PolyNode* curr = dummy;
190+
while (poly1 && poly2) {
191+
if (poly1->power > poly2->power) {
192+
curr->next = poly1;
193+
poly1 = poly1->next;
194+
curr = curr->next;
195+
} else if (poly1->power < poly2->power) {
196+
curr->next = poly2;
197+
poly2 = poly2->next;
198+
curr = curr->next;
199+
} else {
200+
int c = poly1->coefficient + poly2->coefficient;
201+
if (c != 0) {
202+
curr->next = new PolyNode(c, poly1->power);
203+
curr = curr->next;
204+
}
205+
poly1 = poly1->next;
206+
poly2 = poly2->next;
207+
}
208+
}
209+
if (!poly1) {
210+
curr->next = poly2;
211+
}
212+
if (!poly2) {
213+
curr->next = poly1;
214+
}
215+
return dummy->next;
216+
}
217+
};
218+
```
219+
220+
### **C#**
221+
222+
```cs
223+
/**
224+
* Definition for polynomial singly-linked list.
225+
* public class PolyNode {
226+
* public int coefficient, power;
227+
* public PolyNode next;
228+
*
229+
* public PolyNode(int x=0, int y=0, PolyNode next=null) {
230+
* this.coefficient = x;
231+
* this.power = y;
232+
* this.next = next;
233+
* }
234+
* }
235+
*/
236+
237+
public class Solution {
238+
public PolyNode AddPoly(PolyNode poly1, PolyNode poly2) {
239+
PolyNode dummy = new PolyNode();
240+
PolyNode curr = dummy;
241+
while (poly1 != null && poly2 != null) {
242+
if (poly1.power > poly2.power) {
243+
curr.next = poly1;
146244
poly1 = poly1.next;
245+
curr = curr.next;
246+
} else if (poly1.power < poly2.power) {
247+
curr.next = poly2;
248+
poly2 = poly2.next;
249+
curr = curr.next;
147250
} else {
148-
int val = poly1.coefficient + poly2.coefficient;
149-
if (val != 0) {
150-
cur.next = new PolyNode(val, poly1.power);
151-
cur = cur.next;
251+
int c = poly1.coefficient + poly2.coefficient;
252+
if (c != 0) {
253+
curr.next = new PolyNode(c, poly1.power);
254+
curr = curr.next;
152255
}
153256
poly1 = poly1.next;
154257
poly2 = poly2.next;
155258
}
156259
}
157-
cur.next = null;
260+
if (poly1 == null) {
261+
curr.next = poly2;
262+
}
263+
if (poly2 == null) {
264+
curr.next = poly1;
265+
}
158266
return dummy.next;
159267
}
160268
}
161269
```
162270

271+
### **JavaScript**
272+
273+
```js
274+
/**
275+
* Definition for polynomial singly-linked list.
276+
* function PolyNode(x=0, y=0, next=null) {
277+
* this.coefficient = x;
278+
* this.power = y;
279+
* this.next = next;
280+
* }
281+
*/
282+
283+
/**
284+
* @param {PolyNode} poly1
285+
* @param {PolyNode} poly2
286+
* @return {PolyNode}
287+
*/
288+
var addPoly = function (poly1, poly2) {
289+
const dummy = new PolyNode();
290+
let curr = dummy;
291+
while (poly1 && poly2) {
292+
if (poly1.power > poly2.power) {
293+
curr.next = poly1;
294+
poly1 = poly1.next;
295+
curr = curr.next;
296+
} else if (poly1.power < poly2.power) {
297+
curr.next = poly2;
298+
poly2 = poly2.next;
299+
curr = curr.next;
300+
} else {
301+
const c = poly1.coefficient + poly2.coefficient;
302+
if (c != 0) {
303+
curr.next = new PolyNode(c, poly1.power);
304+
curr = curr.next;
305+
}
306+
poly1 = poly1.next;
307+
poly2 = poly2.next;
308+
}
309+
}
310+
curr.next = poly1 || poly2;
311+
return dummy.next;
312+
};
313+
```
314+
163315
### **...**
164316

165317
```

0 commit comments

Comments
 (0)