Skip to content

Commit 201a40f

Browse files
committed
feat: update solutions to lc problem: No.1743
No.1743.Restore the Array From Adjacent Pairs
1 parent 84ab62b commit 201a40f

File tree

7 files changed

+202
-240
lines changed

7 files changed

+202
-240
lines changed

solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060

6161
**方法一:数组 + 模拟**
6262

63-
根据题意我们知道,小球的编号最大不超过 $10^5$,每个编号的各个位数之和的最大值小于 $50$因此,我们可以用一个长度为 $50$ 的数组来统计每个编号的各个位数之和的数量
63+
观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$因此,我们可以直接开一个长度为 $50$ 的数组 `cnt` 来统计每个编号的各个位数之和的数量
6464

65-
答案就是这个数组中的最大值
65+
答案就是数组 `cnt` 中的最大值
6666

6767
时间复杂度 $O(n \times \log_{10}m)$。其中 $n = highLimit - lowLimit + 1$,而 $m = highLimit$。
6868

@@ -94,8 +94,8 @@ class Solution {
9494
public int countBalls(int lowLimit, int highLimit) {
9595
int[] cnt = new int[50];
9696
for (int i = lowLimit; i <= highLimit; ++i) {
97-
int x = i, y = 0;
98-
for (; x > 0; x /= 10) {
97+
int y = 0;
98+
for (int x = i; x > 0; x /= 10) {
9999
y += x % 10;
100100
}
101101
++cnt[y];
@@ -114,8 +114,8 @@ public:
114114
int cnt[50] = {0};
115115
int ans = 0;
116116
for (int i = lowLimit; i <= highLimit; ++i) {
117-
int x = i, y = 0;
118-
for (; x; x /= 10) {
117+
int y = 0;
118+
for (int x = i; x; x /= 10) {
119119
y += x % 10;
120120
}
121121
ans = max(ans, ++cnt[y]);
@@ -131,8 +131,8 @@ public:
131131
func countBalls(lowLimit int, highLimit int) (ans int) {
132132
cnt := [50]int{}
133133
for i := lowLimit; i <= highLimit; i++ {
134-
x, y := i, 0
135-
for ; x > 0; x /= 10 {
134+
y := 0
135+
for x := i; x > 0; x /= 10 {
136136
y += x % 10
137137
}
138138
cnt[y]++

solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class Solution {
7676
public int countBalls(int lowLimit, int highLimit) {
7777
int[] cnt = new int[50];
7878
for (int i = lowLimit; i <= highLimit; ++i) {
79-
int x = i, y = 0;
80-
for (; x > 0; x /= 10) {
79+
int y = 0;
80+
for (int x = i; x > 0; x /= 10) {
8181
y += x % 10;
8282
}
8383
++cnt[y];
@@ -96,8 +96,8 @@ public:
9696
int cnt[50] = {0};
9797
int ans = 0;
9898
for (int i = lowLimit; i <= highLimit; ++i) {
99-
int x = i, y = 0;
100-
for (; x; x /= 10) {
99+
int y = 0;
100+
for (int x = i; x; x /= 10) {
101101
y += x % 10;
102102
}
103103
ans = max(ans, ++cnt[y]);
@@ -113,8 +113,8 @@ public:
113113
func countBalls(lowLimit int, highLimit int) (ans int) {
114114
cnt := [50]int{}
115115
for i := lowLimit; i <= highLimit; i++ {
116-
x, y := i, 0
117-
for ; x > 0; x /= 10 {
116+
y := 0
117+
for x := i; x > 0; x /= 10 {
118118
y += x % 10
119119
}
120120
cnt[y]++

solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class Solution {
44
int cnt[50] = {0};
55
int ans = 0;
66
for (int i = lowLimit; i <= highLimit; ++i) {
7-
int x = i, y = 0;
8-
for (; x; x /= 10) {
7+
int y = 0;
8+
for (int x = i; x; x /= 10) {
99
y += x % 10;
1010
}
1111
ans = max(ans, ++cnt[y]);

solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
func countBalls(lowLimit int, highLimit int) (ans int) {
22
cnt := [50]int{}
33
for i := lowLimit; i <= highLimit; i++ {
4-
x, y := i, 0
5-
for ; x > 0; x /= 10 {
4+
y := 0
5+
for x := i; x > 0; x /= 10 {
66
y += x % 10
77
}
88
cnt[y]++

solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Solution {
22
public int countBalls(int lowLimit, int highLimit) {
33
int[] cnt = new int[50];
44
for (int i = lowLimit; i <= highLimit; ++i) {
5-
int x = i, y = 0;
6-
for (; x > 0; x /= 10) {
5+
int y = 0;
6+
for (int x = i; x > 0; x /= 10) {
77
y += x % 10;
88
}
99
++cnt[y];

solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md

Lines changed: 91 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,19 @@ class Solution:
9393
```python
9494
class Solution:
9595
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
96-
def dfs(i):
97-
if i in vis:
98-
return
99-
vis.add(i)
96+
def dfs(i, fa):
10097
ans.append(i)
10198
for j in g[i]:
102-
dfs(j)
99+
if j != fa:
100+
dfs(j, i)
103101

104102
g = defaultdict(list)
105103
for a, b in adjacentPairs:
106104
g[a].append(b)
107105
g[b].append(a)
106+
i = next(i for i, v in g.items() if len(v) == 1)
108107
ans = []
109-
vis = set()
110-
start = -1
111-
for i, v in g.items():
112-
if len(v) == 1:
113-
start = i
114-
break
115-
dfs(start)
108+
dfs(i, 1e6)
116109
return ans
117110
```
118111

@@ -149,103 +142,37 @@ class Solution {
149142

150143
```java
151144
class Solution {
145+
private Map<Integer, List<Integer>> g = new HashMap<>();
146+
private int[] ans;
147+
152148
public int[] restoreArray(int[][] adjacentPairs) {
153-
int n = adjacentPairs.length + 1;
154-
Map<Integer, List<Integer>> g = new HashMap<>();
155-
for (int[] e : adjacentPairs) {
149+
for (var e : adjacentPairs) {
156150
int a = e[0], b = e[1];
157151
g.computeIfAbsent(a, k -> new ArrayList<>()).add(b);
158152
g.computeIfAbsent(b, k -> new ArrayList<>()).add(a);
159153
}
160-
List<Integer> ans = new ArrayList<>();
161-
Set<Integer> vis = new HashSet<>();
162-
int start = -1;
163-
for (Map.Entry<Integer, List<Integer>> entry : g.entrySet()) {
164-
if (entry.getValue().size() == 1) {
165-
start = entry.getKey();
154+
int n = adjacentPairs.length + 1;
155+
ans = new int[n];
156+
for (var e : g.entrySet()) {
157+
if (e.getValue().size() == 1) {
158+
dfs(e.getKey(), 1000000, 0);
166159
break;
167160
}
168161
}
169-
dfs(g, ans, vis, start);
170-
return ans.stream().mapToInt(Integer::valueOf).toArray();
162+
return ans;
171163
}
172164

173-
private void dfs(Map<Integer, List<Integer>> g, List<Integer> ans, Set<Integer> vis, int i) {
174-
if (vis.contains(i)) {
175-
return;
176-
}
177-
vis.add(i);
178-
ans.add(i);
165+
private void dfs(int i, int fa, int k) {
166+
ans[k++] = i;
179167
for (int j : g.get(i)) {
180-
dfs(g, ans, vis, j);
168+
if (j != fa) {
169+
dfs(j, i, k);
170+
}
181171
}
182172
}
183173
}
184174
```
185175

186-
### **Go**
187-
188-
```go
189-
func restoreArray(adjacentPairs [][]int) []int {
190-
n := len(adjacentPairs) + 1
191-
g := map[int][]int{}
192-
for _, e := range adjacentPairs {
193-
a, b := e[0], e[1]
194-
g[a] = append(g[a], b)
195-
g[b] = append(g[b], a)
196-
}
197-
ans := make([]int, n)
198-
for k, v := range g {
199-
if len(v) == 1 {
200-
ans[0] = k
201-
ans[1] = v[0]
202-
break
203-
}
204-
}
205-
for i := 2; i < n; i++ {
206-
v := g[ans[i-1]]
207-
ans[i] = v[0]
208-
if v[0] == ans[i-2] {
209-
ans[i] = v[1]
210-
}
211-
}
212-
return ans
213-
}
214-
```
215-
216-
```go
217-
func restoreArray(adjacentPairs [][]int) []int {
218-
g := map[int][]int{}
219-
for _, e := range adjacentPairs {
220-
a, b := e[0], e[1]
221-
g[a] = append(g[a], b)
222-
g[b] = append(g[b], a)
223-
}
224-
ans := []int{}
225-
vis := map[int]bool{}
226-
var start int
227-
for i, v := range g {
228-
if len(v) == 1 {
229-
start = i
230-
break
231-
}
232-
}
233-
var dfs func(i int)
234-
dfs = func(i int) {
235-
if vis[i] {
236-
return
237-
}
238-
vis[i] = true
239-
ans = append(ans, i)
240-
for _, j := range g[i] {
241-
dfs(j)
242-
}
243-
}
244-
dfs(start)
245-
return ans
246-
}
247-
```
248-
249176
### **C++**
250177

251178
```cpp
@@ -280,37 +207,91 @@ public:
280207
class Solution {
281208
public:
282209
vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {
283-
int n = adjacentPairs.size() + 1;
284210
unordered_map<int, vector<int>> g;
285211
for (auto& e : adjacentPairs) {
286212
int a = e[0], b = e[1];
287-
g[a].push_back(b);
288-
g[b].push_back(a);
213+
g[a].emplace_back(b);
214+
g[b].emplace_back(a);
289215
}
216+
int n = adjacentPairs.size() + 1;
290217
vector<int> ans;
291-
unordered_set<int> vis;
292-
int start = -1;
293-
for (auto& [k, v] : g) {
218+
function<void(int, int)> dfs = [&](int i, int fa) {
219+
ans.emplace_back(i);
220+
for (int& j : g[i]) {
221+
if (j != fa) {
222+
dfs(j, i);
223+
}
224+
}
225+
};
226+
for (auto& [i, v] : g) {
294227
if (v.size() == 1) {
295-
start = k;
228+
dfs(i, 1e6);
296229
break;
297230
}
298231
}
299-
dfs(g, ans, vis, start);
300232
return ans;
301233
}
302-
303-
void dfs(unordered_map<int, vector<int>>& g, vector<int>& ans, unordered_set<int>& vis, int i) {
304-
if (vis.count(i)) return;
305-
ans.push_back(i);
306-
vis.insert(i);
307-
for (int j : g[i]) {
308-
dfs(g, ans, vis, j);
309-
}
310-
}
311234
};
312235
```
313236

237+
### **Go**
238+
239+
```go
240+
func restoreArray(adjacentPairs [][]int) []int {
241+
n := len(adjacentPairs) + 1
242+
g := map[int][]int{}
243+
for _, e := range adjacentPairs {
244+
a, b := e[0], e[1]
245+
g[a] = append(g[a], b)
246+
g[b] = append(g[b], a)
247+
}
248+
ans := make([]int, n)
249+
for k, v := range g {
250+
if len(v) == 1 {
251+
ans[0] = k
252+
ans[1] = v[0]
253+
break
254+
}
255+
}
256+
for i := 2; i < n; i++ {
257+
v := g[ans[i-1]]
258+
ans[i] = v[0]
259+
if v[0] == ans[i-2] {
260+
ans[i] = v[1]
261+
}
262+
}
263+
return ans
264+
}
265+
```
266+
267+
```go
268+
func restoreArray(adjacentPairs [][]int) []int {
269+
g := map[int][]int{}
270+
for _, e := range adjacentPairs {
271+
a, b := e[0], e[1]
272+
g[a] = append(g[a], b)
273+
g[b] = append(g[b], a)
274+
}
275+
ans := []int{}
276+
var dfs func(i, fa int)
277+
dfs = func(i, fa int) {
278+
ans = append(ans, i)
279+
for _, j := range g[i] {
280+
if j != fa {
281+
dfs(j, i)
282+
}
283+
}
284+
}
285+
for i, v := range g {
286+
if len(v) == 1 {
287+
dfs(i, 1000000)
288+
break
289+
}
290+
}
291+
return ans
292+
}
293+
```
294+
314295
### **...**
315296

316297
```

0 commit comments

Comments
 (0)