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 fb2902f

Browse files
solved: 3 Sum Closest
1 parent 732169a commit fb2902f

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

‎README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [Valid number](#valid-number)
2525
- [Text Justification](#text-justification)
2626
- [3 Sum](#3-sum)
27+
- [3 Sum Closest](#3-sum-closest)
2728

2829
### String to Int - Atoi
2930

@@ -639,4 +640,40 @@ List<List<int>> threeSum(List<int> nums) {
639640
640641
return result;
641642
}
643+
```
644+
645+
### 3 Sum Closest
646+
647+
```dart
648+
int threeSumClosest(List<int> nums, int target) {
649+
int result = -1;
650+
int diff = -1;
651+
652+
for (int i = 0; i < nums.length - 2; i++) {
653+
int left = i + 1;
654+
int right = nums.length - 1;
655+
656+
while (left < right) {
657+
final int sum = nums[i] + nums[left] + nums[right];
658+
659+
if (diff < 0 || (sum - target).abs() < diff) {
660+
diff = (sum - target).abs();
661+
result = sum;
662+
}
663+
664+
if (diff == 0) {
665+
return result;
666+
}
667+
668+
if (right == left + 1) {
669+
left++;
670+
right = nums.length - 1;
671+
} else {
672+
right--;
673+
}
674+
}
675+
}
676+
677+
return result;
678+
}
642679
```

‎src/3_sum_closest.dart‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import '../helpers/stop_watch.dart';
2+
3+
void main(List<String> args) {
4+
List<List> cases = [
5+
// [
6+
// [-1, 2, 1, -4],
7+
// 1
8+
// ],
9+
// [
10+
// [0, 0, 0],
11+
// 1
12+
// ],
13+
// [
14+
// [1, 1, 1, 0],
15+
// 100
16+
// ],
17+
[
18+
[
19+
40,
20+
-53,
21+
36,
22+
89,
23+
-38,
24+
-51,
25+
80,
26+
11,
27+
-10,
28+
76,
29+
-30,
30+
46,
31+
-39,
32+
-15,
33+
4,
34+
72,
35+
83,
36+
-25,
37+
33,
38+
-69,
39+
-73,
40+
-100,
41+
-23,
42+
-37,
43+
-13,
44+
-62,
45+
-26,
46+
-54,
47+
36,
48+
-84,
49+
-65,
50+
-51,
51+
11,
52+
98,
53+
-21,
54+
49,
55+
51,
56+
78,
57+
-58,
58+
-40,
59+
95,
60+
-81,
61+
41,
62+
-17,
63+
-70,
64+
83,
65+
-88,
66+
-14,
67+
-75,
68+
-10,
69+
-44,
70+
-21,
71+
6,
72+
68,
73+
-81,
74+
-1,
75+
41,
76+
-61,
77+
-82,
78+
-24,
79+
45,
80+
19,
81+
6,
82+
-98,
83+
11,
84+
9,
85+
-66,
86+
50,
87+
-97,
88+
-2,
89+
58,
90+
17,
91+
51,
92+
-13,
93+
88,
94+
-16,
95+
-77,
96+
31,
97+
35,
98+
98,
99+
-2,
100+
0,
101+
-70,
102+
6,
103+
-34,
104+
-8,
105+
78,
106+
22,
107+
-1,
108+
-93,
109+
-39,
110+
-88,
111+
-77,
112+
-65,
113+
80,
114+
91,
115+
35,
116+
-15,
117+
7,
118+
-37,
119+
-96,
120+
65,
121+
3,
122+
33,
123+
-22,
124+
60,
125+
1,
126+
76,
127+
-32,
128+
22
129+
],
130+
292
131+
]
132+
];
133+
134+
for (final testcase in cases) {
135+
Duration timeWasted = stopwatch(() {
136+
print(threeSumClosest(testcase.first, testcase.last));
137+
});
138+
139+
print("Spent: ${timeWasted.inMilliseconds}ms");
140+
}
141+
}
142+
143+
int threeSumClosest(List<int> nums, int target) {
144+
int result = -1;
145+
int diff = -1;
146+
147+
for (int i = 0; i < nums.length - 2; i++) {
148+
int left = i + 1;
149+
int right = nums.length - 1;
150+
151+
while (left < right) {
152+
final int sum = nums[i] + nums[left] + nums[right];
153+
154+
if (diff < 0 || (sum - target).abs() < diff) {
155+
diff = (sum - target).abs();
156+
result = sum;
157+
}
158+
159+
if (diff == 0) {
160+
return result;
161+
}
162+
163+
if (right == left + 1) {
164+
left++;
165+
right = nums.length - 1;
166+
} else {
167+
right--;
168+
}
169+
}
170+
}
171+
172+
return result;
173+
}

0 commit comments

Comments
(0)

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