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 6bbda81

Browse files
Refactor threeSum method to eliminate duplicates and improve result handling
1 parent 4d40192 commit 6bbda81

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

‎leetcode/15.3sum.dart

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,85 @@ void main(List<String> args) {
1313
}
1414

1515
class Solution {
16-
////! Success Submitted TC: O(n^2) + Sort TC
16+
////! Success Submitted TC: O(n^2) + Sort TC (O(NLog(N)))
17+
////! Enhance
1718
List<List<int>> threeSum(List<int> nums) {
1819
nums.sort();
1920

20-
final Map<String, List<int>> result = {};
21+
final List<List<int>> result = [];
2122
for (int i = 0; i < nums.length - 2; i++) {
23+
/// this line to break because we will not reach to target at the sorted array
2224
if (nums[i] > 0) break;
23-
int target = -1 * nums[i];
25+
26+
/// To get rid of the duplicate So we will take the first number appeared in the list
27+
/// So if the number appears for the second time we will continue
28+
if (i != 0 && nums[i - 1] == nums[i]) continue;
29+
30+
int target = 0 - nums[i];
2431
int left = i + 1;
2532
int right = nums.length - 1;
2633

2734
while (left < right) {
2835
int sum = nums[right] + nums[left];
2936
if (sum == target) {
30-
result.putIfAbsent(
31-
"${nums[i]}${nums[left]}${nums[right]}",
32-
() => [nums[i], nums[left], nums[right]],
33-
);
34-
right--;
37+
result.add([nums[i], nums[left], nums[right]]);
38+
39+
/// move to the next
3540
left++;
41+
right--;
42+
43+
/// Skip all the left duplicates
44+
/// If the new left is the same as the old then
45+
while (left < right && nums[left] == nums[left - 1]) {
46+
left++;
47+
}
48+
49+
/// Skip all the right duplicate
50+
while (left < right &&
51+
right < nums.length - 1 &&
52+
nums[right] == nums[right + 1]) {
53+
right--;
54+
}
3655
} else if (sum > target) {
3756
right--;
3857
} else {
3958
left++;
4059
}
4160
}
4261
}
43-
return result.values.toList();
62+
return result;
4463
}
4564

65+
////! Success Submitted TC: O(n^2) + Sort TC (O(NLog(N)))
66+
// List<List<int>> threeSum(List<int> nums) {
67+
// nums.sort();
68+
69+
// final Map<String, List<int>> result = {};
70+
// for (int i = 0; i < nums.length - 2; i++) {
71+
// if (nums[i] > 0) break;
72+
// int target = -1 * nums[i];
73+
// int left = i + 1;
74+
// int right = nums.length - 1;
75+
76+
// while (left < right) {
77+
// int sum = nums[right] + nums[left];
78+
// if (sum == target) {
79+
// result.putIfAbsent(
80+
// "${nums[i]}${nums[left]}${nums[right]}",
81+
// () => [nums[i], nums[left], nums[right]],
82+
// );
83+
// right--;
84+
// left++;
85+
// } else if (sum > target) {
86+
// right--;
87+
// } else {
88+
// left++;
89+
// }
90+
// }
91+
// }
92+
// return result.values.toList();
93+
// }
94+
4695
////! Time Limit Solution --> TC: O(n^3) + Sort TC
4796
//// ! Brute Force .
4897
// List<List<int>> threeSum(List<int> nums) {

0 commit comments

Comments
(0)

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