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 10ea5c4

Browse files
Add Find All Numbers Disappeared in an Array solution and tests; update README with new problem link
1 parent a7fef69 commit 10ea5c4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- https://leetcode.com/problems/ransom-note/
2525
- https://leetcode.com/problems/contains-duplicate/
2626
- https://leetcode.com/problems/contains-duplicate-ii/description/
27+
- https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/
2728

2829

2930

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/
2+
3+
void main() {
4+
final stopwatch = Stopwatch()..start();
5+
6+
print(Solution().findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1]));
7+
8+
stopwatch.stop();
9+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec');
10+
// GOOD: Function Execution Time : 000 micro sec
11+
// BAD : Function Execution Time : 000 micro sec
12+
}
13+
14+
class Solution {
15+
////! TC: O(2n) O(n) :: SC(2N)
16+
////! Use HashMap is better than Contain
17+
List<int> findDisappearedNumbers(List<int> nums) {
18+
final List<int> list = List.filled(nums.length + 1, 0);
19+
for (int i = 0; i < nums.length; i++) {
20+
list[nums[i]] = 1;
21+
}
22+
final List<int> result = [];
23+
for (int i = 1; i < list.length; i++) {
24+
if (list[i] == 0) {
25+
result.add(i);
26+
}
27+
}
28+
return result;
29+
}
30+
31+
/// (Udemy) Use The same nums list but with the help on negative as a flag
32+
// List<int> findDisappearedNumbers(List<int> nums) {
33+
// int index = 0;
34+
// for (int i = 0; i < nums.length; i++) {
35+
// index = nums[i].abs() - 1;
36+
// if (nums[index] > 0) {
37+
// nums[index] = nums[index] * -1;
38+
// }
39+
// }
40+
// final List<int> result = [];
41+
// for (int i = 0; i < nums.length; i++) {
42+
// if (nums[i] > 0) {
43+
// result.add(i + 1);
44+
// }
45+
// }
46+
// return result;
47+
// }
48+
49+
////! My First Solution TC: O(n)
50+
////! Time Limit Exceeded --> Contain is the problem
51+
// List<int> findDisappearedNumbers(List<int> nums) {
52+
// final List<int> result = [];
53+
// final count = nums.length;
54+
// for (int i = 1; i <= count; i++) {
55+
// if (!nums.contains(i)) {
56+
// result.add(i);
57+
// }
58+
// }
59+
// return result;
60+
// }
61+
}

0 commit comments

Comments
(0)

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