We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 885208e commit 10d5f2eCopy full SHA for 10d5f2e
problem47/main.dart
@@ -0,0 +1,38 @@
1
+
2
+//16. 3Sum Closest
3
4
+void main(List<String> args) {
5
+ print(threeSumClosest([0,1,-1], 3));
6
+}
7
8
+threeSumClosest(List<int> nums, int target){
9
10
+ nums.sort();
11
12
+ var result=[];
13
+ for(int i=0;i<nums.length-2;i++){
14
15
+ int left=i+1;
16
+ int right=nums.length-1;
17
+ while(left<right){
18
+ var sum=nums[i]+nums[left]+nums[right];
19
20
+ if(sum >target){
21
+ right --;
22
+ }
23
+ if(sum<target){
24
+ left ++;
25
26
27
+ if(sum==target){
28
+ return sum;
29
30
+ result.add(sum);
31
32
33
34
+ return result.reduce((prev, curr) {
35
+ return (curr -target).abs()<(prev-target).abs()?curr:prev;
36
+ });
37
38
0 commit comments