Skip to content

Commit 25882ba

Browse files
committedMay 20, 2021
feat: add solutions to lc problem: No.1721. Swapping Nodes in a Linked List
1 parent 7bffbaf commit 25882ba

File tree

11 files changed

+264
-12
lines changed

11 files changed

+264
-12
lines changed
 

‎solution/1700-1799/1721.Swapping Nodes in a Linked List/README.md

+46-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<li><code>0 <= Node.val <= 100</code></li>
5858
</ul>
5959

60-
6160
## 解法
6261

6362
<!-- 这里可写通用的实现逻辑 -->
@@ -69,15 +68,59 @@
6968
<!-- 这里可写当前语言的特殊实现逻辑 -->
7069

7170
```python
72-
71+
# Definition for singly-linked list.
72+
# class ListNode:
73+
# def __init__(self, val=0, next=None):
74+
# self.val = val
75+
# self.next = next
76+
class Solution:
77+
def swapNodes(self, head: ListNode, k: int) -> ListNode:
78+
fast = head
79+
for _ in range(k - 1):
80+
fast = fast.next
81+
p = fast
82+
slow = head
83+
while fast.next:
84+
slow, fast = slow.next, fast.next
85+
q = slow
86+
p.val, q.val = q.val, p.val
87+
return head
7388
```
7489

7590
### **Java**
7691

7792
<!-- 这里可写当前语言的特殊实现逻辑 -->
7893

7994
```java
80-
95+
/**
96+
* Definition for singly-linked list.
97+
* public class ListNode {
98+
* int val;
99+
* ListNode next;
100+
* ListNode() {}
101+
* ListNode(int val) { this.val = val; }
102+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
103+
* }
104+
*/
105+
class Solution {
106+
public ListNode swapNodes(ListNode head, int k) {
107+
ListNode fast = head;
108+
while (--k > 0) {
109+
fast = fast.next;
110+
}
111+
ListNode p = fast;
112+
ListNode slow = head;
113+
while (fast.next != null) {
114+
slow = slow.next;
115+
fast = fast.next;
116+
}
117+
ListNode q = slow;
118+
int t = p.val;
119+
p.val = q.val;
120+
q.val = t;
121+
return head;
122+
}
123+
}
81124
```
82125

83126
### **...**

‎solution/1700-1799/1721.Swapping Nodes in a Linked List/README_EN.md

+46-3
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,64 @@
5353
<li><code>0 &lt;= Node.val &lt;= 100</code></li>
5454
</ul>
5555

56-
5756
## Solutions
5857

5958
<!-- tabs:start -->
6059

6160
### **Python3**
6261

6362
```python
64-
63+
# Definition for singly-linked list.
64+
# class ListNode:
65+
# def __init__(self, val=0, next=None):
66+
# self.val = val
67+
# self.next = next
68+
class Solution:
69+
def swapNodes(self, head: ListNode, k: int) -> ListNode:
70+
fast = head
71+
for _ in range(k - 1):
72+
fast = fast.next
73+
p = fast
74+
slow = head
75+
while fast.next:
76+
slow, fast = slow.next, fast.next
77+
q = slow
78+
p.val, q.val = q.val, p.val
79+
return head
6580
```
6681

6782
### **Java**
6883

6984
```java
70-
85+
/**
86+
* Definition for singly-linked list.
87+
* public class ListNode {
88+
* int val;
89+
* ListNode next;
90+
* ListNode() {}
91+
* ListNode(int val) { this.val = val; }
92+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
93+
* }
94+
*/
95+
class Solution {
96+
public ListNode swapNodes(ListNode head, int k) {
97+
ListNode fast = head;
98+
while (--k > 0) {
99+
fast = fast.next;
100+
}
101+
ListNode p = fast;
102+
ListNode slow = head;
103+
while (fast.next != null) {
104+
slow = slow.next;
105+
fast = fast.next;
106+
}
107+
ListNode q = slow;
108+
int t = p.val;
109+
p.val = q.val;
110+
q.val = t;
111+
return head;
112+
}
113+
}
71114
```
72115

73116
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode swapNodes(ListNode head, int k) {
13+
ListNode fast = head;
14+
while (--k > 0) {
15+
fast = fast.next;
16+
}
17+
ListNode p = fast;
18+
ListNode slow = head;
19+
while (fast.next != null) {
20+
slow = slow.next;
21+
fast = fast.next;
22+
}
23+
ListNode q = slow;
24+
int t = p.val;
25+
p.val = q.val;
26+
q.val = t;
27+
return head;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def swapNodes(self, head: ListNode, k: int) -> ListNode:
8+
fast = head
9+
for _ in range(k - 1):
10+
fast = fast.next
11+
p = fast
12+
slow = head
13+
while fast.next:
14+
slow, fast = slow.next, fast.next
15+
q = slow
16+
p.val, q.val = q.val, p.val
17+
return head
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [1867. ](https://leetcode-cn.com/problems/orders-with-maximum-quantity-above-average)
2+
3+
[English Version](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
None
10+
11+
## 解法
12+
13+
<!-- 这里可写通用的实现逻辑 -->
14+
15+
<!-- tabs:start -->
16+
17+
### **SQL**
18+
19+
```sql
20+
21+
```
22+
23+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [1867. Orders With Maximum Quantity Above Average](https://leetcode.com/problems/orders-with-maximum-quantity-above-average)
2+
3+
[中文文档](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>OrdersDetails</code></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| order_id | int |
14+
| product_id | int |
15+
| quantity | int |
16+
+-------------+------+
17+
(order_id, product_id) is the primary key&nbsp;for this table.
18+
A single order is represented as multiple rows, one row for each product in the order.
19+
Each row of this table contains the quantity ordered of the product product_id in the order order_id.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>You are running an ecommerce site that is looking for <strong>imbalanced orders</strong>. An <strong>imbalanced order</strong> is one whose <strong>maximum</strong> quantity is <strong>strictly greater</strong> than the <strong>average</strong> quantity of <strong>every order (including itself)</strong>.</p>
25+
26+
<p>The <strong>average </strong>quantity of an order is calculated as <code>(total quantity of all products in the order) / (number of different products in the order)</code>. The <strong>maximum</strong> quantity of an order is the highest <code>quantity</code> of any single product in the order.</p>
27+
28+
<p>Write an SQL query to find the <code>order_id</code> of all <strong>imbalanced orders</strong>.</p>
29+
30+
<p>Return the result table in <strong>any order</strong>.</p>
31+
32+
<p>The query result format is in the following example:</p>
33+
34+
<p>&nbsp;</p>
35+
36+
<pre>
37+
OrdersDetails table:
38+
+----------+------------+----------+
39+
| order_id | product_id | quantity |
40+
+----------+------------+----------+
41+
| 1 | 1 | 12 |
42+
| 1 | 2 | 10 |
43+
| 1 | 3 | 15 |
44+
| 2 | 1 | 8 |
45+
| 2 | 4 | 4 |
46+
| 2 | 5 | 6 |
47+
| 3 | 3 | 5 |
48+
| 3 | 4 | 18 |
49+
| 4 | 5 | 2 |
50+
| 4 | 6 | 8 |
51+
| 5 | 7 | 9 |
52+
| 5 | 8 | 9 |
53+
| 3 | 9 | 20 |
54+
| 2 | 9 | 4 |
55+
+----------+------------+----------+
56+
57+
Result table:
58+
+----------+
59+
| order_id |
60+
+----------+
61+
| 1 |
62+
| 3 |
63+
+----------+
64+
65+
The average quantity of each order is:
66+
- order_id=1: (12+10+15)/3 = 12.3333333
67+
- order_id=2: (8+4+6+4)/4 = 5.5
68+
- order_id=3: (5+18+20)/3 = 14.333333
69+
- order_id=4: (2+8)/2 = 5
70+
- order_id=5: (9+9)/2 = 9
71+
72+
The maximum quantity of each order is:
73+
- order_id=1: max(12, 10, 15) = 15
74+
- order_id=2: max(8, 4, 6, 4) = 8
75+
- order_id=3: max(5, 18, 20) = 20
76+
- order_id=4: max(2, 8) = 8
77+
- order_id=5: max(9, 9) = 9
78+
79+
Orders 1 and 3 are imbalanced because they have a maximum quantity that exceeds the average quantity of every order.
80+
</pre>
81+
82+
83+
## Solutions
84+
85+
<!-- tabs:start -->
86+
87+
### **SQL**
88+
89+
```sql
90+
91+
```
92+
93+
<!-- tabs:end -->

‎solution/README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -1851,9 +1851,9 @@
18511851
| [1838](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element) | [最高频元素的频数](/solution/1800-1899/1838.Frequency%20of%20the%20Most%20Frequent%20Element/README.md) | `贪心算法` | 中等 | |
18521852
| [1839](https://leetcode-cn.com/problems/longest-substring-of-all-vowels-in-order) | [所有元音按顺序排布的最长子字符串](/solution/1800-1899/1839.Longest%20Substring%20Of%20All%20Vowels%20in%20Order/README.md) | `双指针`,`字符串` | 中等 | |
18531853
| [1840](https://leetcode-cn.com/problems/maximum-building-height) | [最高建筑高度](/solution/1800-1899/1840.Maximum%20Building%20Height/README.md) | `贪心算法`,`二分查找` | 困难 | |
1854-
| [1841](https://leetcode-cn.com/problems/league-statistics) | [League Statistics](/solution/1800-1899/1841.League%20Statistics/README_EN.md) | | 中等 | |
1855-
| [1842](https://leetcode-cn.com/problems/next-palindrome-using-same-digits) | [Next Palindrome Using Same Digits](/solution/1800-1899/1842.Next%20Palindrome%20Using%20Same%20Digits/README_EN.md) | `贪心算法` | 困难 | |
1856-
| [1843](https://leetcode-cn.com/problems/suspicious-bank-accounts) | [Suspicious Bank Accounts](/solution/1800-1899/1843.Suspicious%20Bank%20Accounts/README_EN.md) | | 中等 | |
1854+
| [1841](https://leetcode-cn.com/problems/league-statistics) | [League Statistics](/solution/1800-1899/1841.League%20Statistics/README_EN.md) | | 中等 | 🔒 |
1855+
| [1842](https://leetcode-cn.com/problems/next-palindrome-using-same-digits) | [Next Palindrome Using Same Digits](/solution/1800-1899/1842.Next%20Palindrome%20Using%20Same%20Digits/README_EN.md) | `贪心算法` | 困难 | 🔒 |
1856+
| [1843](https://leetcode-cn.com/problems/suspicious-bank-accounts) | [Suspicious Bank Accounts](/solution/1800-1899/1843.Suspicious%20Bank%20Accounts/README_EN.md) | | 中等 | 🔒 |
18571857
| [1844](https://leetcode-cn.com/problems/replace-all-digits-with-characters) | [将所有数字用字符替换](/solution/1800-1899/1844.Replace%20All%20Digits%20with%20Characters/README.md) | `字符串` | 简单 | |
18581858
| [1845](https://leetcode-cn.com/problems/seat-reservation-manager) | [座位预约管理系统](/solution/1800-1899/1845.Seat%20Reservation%20Manager/README.md) | ``,`设计` | 中等 | |
18591859
| [1846](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging) | [减小和重新排列数组后的最大元素](/solution/1800-1899/1846.Maximum%20Element%20After%20Decreasing%20and%20Rearranging/README.md) | `贪心算法`,`排序` | 中等 | |
@@ -1862,13 +1862,13 @@
18621862
| [1849](https://leetcode-cn.com/problems/splitting-a-string-into-descending-consecutive-values) | [将字符串拆分为递减的连续值](/solution/1800-1899/1849.Splitting%20a%20String%20Into%20Descending%20Consecutive%20Values/README.md) | `递归`,`字符串`,`回溯算法` | 中等 | |
18631863
| [1850](https://leetcode-cn.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number) | [邻位交换的最小次数](/solution/1800-1899/1850.Minimum%20Adjacent%20Swaps%20to%20Reach%20the%20Kth%20Smallest%20Number/README.md) | `贪心算法`,`字符串` | 中等 | |
18641864
| [1851](https://leetcode-cn.com/problems/minimum-interval-to-include-each-query) | [包含每个查询的最小区间](/solution/1800-1899/1851.Minimum%20Interval%20to%20Include%20Each%20Query/README.md) | | 困难 | |
1865-
| [1852](https://leetcode-cn.com/problems/distinct-numbers-in-each-subarray) | [Distinct Numbers in Each Subarray](/solution/1800-1899/1852.Distinct%20Numbers%20in%20Each%20Subarray/README_EN.md) | `数组`,`哈希表` | 中等 | |
1865+
| [1852](https://leetcode-cn.com/problems/distinct-numbers-in-each-subarray) | [Distinct Numbers in Each Subarray](/solution/1800-1899/1852.Distinct%20Numbers%20in%20Each%20Subarray/README_EN.md) | `数组`,`哈希表` | 中等 | 🔒 |
18661866
| [1853](https://leetcode-cn.com/problems/convert-date-format) | [Convert Date Format](/solution/1800-1899/1853.Convert%20Date%20Format/README_EN.md) | | 简单 | 🔒 |
18671867
| [1854](https://leetcode-cn.com/problems/maximum-population-year) | [人口最多的年份](/solution/1800-1899/1854.Maximum%20Population%20Year/README.md) | `数组` | 简单 | |
18681868
| [1855](https://leetcode-cn.com/problems/maximum-distance-between-a-pair-of-values) | [下标对中的最大距离](/solution/1800-1899/1855.Maximum%20Distance%20Between%20a%20Pair%20of%20Values/README.md) | `贪心算法`,`双指针`,`二分查找` | 中等 | |
18691869
| [1856](https://leetcode-cn.com/problems/maximum-subarray-min-product) | [子数组最小乘积的最大值](/solution/1800-1899/1856.Maximum%20Subarray%20Min-Product/README.md) | `排序`,`并查集`,`队列`,`二分查找`,`动态规划` | 中等 | |
18701870
| [1857](https://leetcode-cn.com/problems/largest-color-value-in-a-directed-graph) | [有向图中最大颜色值](/solution/1800-1899/1857.Largest%20Color%20Value%20in%20a%20Directed%20Graph/README.md) | `拓扑排序`,`动态规划` | 困难 | |
1871-
| [1858](https://leetcode-cn.com/problems/longest-word-with-all-prefixes) | [Longest Word With All Prefixes](/solution/1800-1899/1858.Longest%20Word%20With%20All%20Prefixes/README_EN.md) | `深度优先搜索`,`字典树`,`哈希表` | 中等 | |
1871+
| [1858](https://leetcode-cn.com/problems/longest-word-with-all-prefixes) | [Longest Word With All Prefixes](/solution/1800-1899/1858.Longest%20Word%20With%20All%20Prefixes/README_EN.md) | `深度优先搜索`,`字典树`,`哈希表` | 中等 | 🔒 |
18721872
| [1859](https://leetcode-cn.com/problems/sorting-the-sentence) | [将句子排序](/solution/1800-1899/1859.Sorting%20the%20Sentence/README.md) | `排序`,`字符串` | 简单 | |
18731873
| [1860](https://leetcode-cn.com/problems/incremental-memory-leak) | [增长的内存泄露](/solution/1800-1899/1860.Incremental%20Memory%20Leak/README.md) | `数学` | 中等 | |
18741874
| [1861](https://leetcode-cn.com/problems/rotating-the-box) | [旋转盒子](/solution/1800-1899/1861.Rotating%20the%20Box/README.md) | `数组`,`双指针` | 中等 | |
@@ -1877,6 +1877,7 @@
18771877
| [1864](https://leetcode-cn.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating) | [构成交替字符串需要的最小交换次数](/solution/1800-1899/1864.Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README.md) | `贪心算法` | 中等 | |
18781878
| [1865](https://leetcode-cn.com/problems/finding-pairs-with-a-certain-sum) | [找出和为指定值的下标对](/solution/1800-1899/1865.Finding%20Pairs%20With%20a%20Certain%20Sum/README.md) | `设计`,`哈希表` | 中等 | |
18791879
| [1866](https://leetcode-cn.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible) | [恰有 K 根木棍可以看到的排列数目](/solution/1800-1899/1866.Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README.md) | `动态规划` | 困难 | |
1880+
| [1867](https://leetcode-cn.com/problems/orders-with-maximum-quantity-above-average) | [Orders With Maximum Quantity Above Average](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README_EN.md) | | 中等 | 🔒 |
18801881

18811882
## 版权
18821883

‎solution/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,7 @@ Press <kbd>Control</kbd>+<kbd>F</kbd>(or <kbd>Command</kbd>+<kbd>F</kbd> on the
18751875
| [1864](https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating) | [Minimum Number of Swaps to Make the Binary String Alternating](/solution/1800-1899/1864.Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README_EN.md) | `Greedy` | Medium | |
18761876
| [1865](https://leetcode.com/problems/finding-pairs-with-a-certain-sum) | [Finding Pairs With a Certain Sum](/solution/1800-1899/1865.Finding%20Pairs%20With%20a%20Certain%20Sum/README_EN.md) | `Design`,`Hash Table`,`Ordered Map` | Medium | |
18771877
| [1866](https://leetcode.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible) | [Number of Ways to Rearrange Sticks With K Sticks Visible](/solution/1800-1899/1866.Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README_EN.md) | `Dynamic Programming` | Hard | |
1878+
| [1867](https://leetcode.com/problems/orders-with-maximum-quantity-above-average) | [Orders With Maximum Quantity Above Average](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README_EN.md) | | Medium | 🔒 |
18781879

18791880
## Copyright
18801881

‎solution/result.json

+1-1
Large diffs are not rendered by default.

‎solution/summary.md

+1
Original file line numberDiff line numberDiff line change
@@ -1902,3 +1902,4 @@
19021902
- [1864.Minimum Number of Swaps to Make the Binary String Alternating](/solution/1800-1899/1864.Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README.md)
19031903
- [1865.Finding Pairs With a Certain Sum](/solution/1800-1899/1865.Finding%20Pairs%20With%20a%20Certain%20Sum/README.md)
19041904
- [1866.Number of Ways to Rearrange Sticks With K Sticks Visible](/solution/1800-1899/1866.Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README.md)
1905+
- [1867.Orders With Maximum Quantity Above Average](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README.md)

‎solution/summary_en.md

+1
Original file line numberDiff line numberDiff line change
@@ -1902,3 +1902,4 @@
19021902
- [1864.Minimum Number of Swaps to Make the Binary String Alternating](/solution/1800-1899/1864.Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README_EN.md)
19031903
- [1865.Finding Pairs With a Certain Sum](/solution/1800-1899/1865.Finding%20Pairs%20With%20a%20Certain%20Sum/README_EN.md)
19041904
- [1866.Number of Ways to Rearrange Sticks With K Sticks Visible](/solution/1800-1899/1866.Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README_EN.md)
1905+
- [1867.Orders With Maximum Quantity Above Average](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README_EN.md)

0 commit comments

Comments
 (0)