Skip to content

Commit b8c72f6

Browse files
committed
✨ [217] Thought process 200 but hash map is still faster than 2 pointer
1 parent dd4fc12 commit b8c72f6

File tree

3 files changed

+70
-31
lines changed

3 files changed

+70
-31
lines changed

217/my_solution.js

+44-21
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,57 @@
44
*/
55
const containsDuplicate = (nums) => {
66
// nums = [1, 2, 3, 1]
7-
let isDuplicated = false, l = 0, r = nums.length - 1;
7+
let map = {}, l = 0, r = nums.length - 1;
8+
// const map = new Map(nums.map((value, index) => [value, index]));
9+
// console.log(map)
810

9-
// for
10-
// think about other edge cases ...
11-
// for (let i = 0; i < nums.length; i++) {
12-
// if (l == r) {
13-
//
14-
// }
15-
16-
while (r >= l) {
17-
if (l === r) {
18-
l++;
19-
r = nums.length - 1;
11+
for (let i = 0; i < nums.length; i++) {
12+
if (undefined === map[nums[i]]) {
13+
map[nums[i]] = i;
2014
continue;
2115
}
2216

23-
if (nums[l] === nums[r]) {
24-
isDuplicated = true;
25-
break;
26-
} else {
27-
r--;
28-
}
17+
return true;
2918
}
19+
console.log("??")
20+
console.log(map)
3021

31-
return isDuplicated;
22+
return false;
3223
}
3324

34-
containsDuplicate([1, 2, 3, 1])
25+
// containsDuplicate([1, 2, 3, 1])
3526
containsDuplicate([1, 2, 3, 4])
36-
containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])
27+
// containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])
3728
// containsDuplicate()
29+
30+
// 2 pointer solution
31+
// const containsDuplicate = (nums) => {
32+
// // nums = [1, 2, 3, 1]
33+
// let isDuplicated = false, l = 0, r = nums.length - 1;
34+
// const map = new Map(nums.map((value, index) => [value, index]));
35+
// console.log(map)
36+
//
37+
// // for
38+
// // think about other edge cases ...
39+
// // for (let i = 0; i < nums.length; i++) {
40+
// // if (l == r) {
41+
// //
42+
// // }
43+
//
44+
// while (r >= l) {
45+
// if (l === r) {
46+
// l++;
47+
// r = nums.length - 1;
48+
// continue;
49+
// }
50+
//
51+
// if (nums[l] === nums[r]) {
52+
// isDuplicated = true;
53+
// break;
54+
// } else {
55+
// r--;
56+
// }
57+
// }
58+
//
59+
// return isDuplicated;
60+
// }

217/solution.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,36 @@
33
* @return {boolean}
44
*/
55
const containsDuplicate = (nums) => {
6-
let l = 0, r = nums.length - 1;
6+
let map = {};
77

8-
while (r >= l) {
9-
if (l === r) {
10-
l++;
11-
r = nums.length - 1;
8+
for (let i = 0; i < nums.length; i++) {
9+
if (undefined === map[nums[i]]) {
10+
map[nums[i]] = i;
1211
continue;
1312
}
1413

15-
if (nums[l] === nums[r]) {
16-
return true
17-
} else {
18-
r--;
19-
}
14+
return true;
2015
}
2116

2217
return false;
2318
}
19+
20+
// const containsDuplicate = (nums) => {
21+
// let l = 0, r = nums.length - 1;
22+
//
23+
// while (r >= l) {
24+
// if (l === r) {
25+
// l++;
26+
// r = nums.length - 1;
27+
// continue;
28+
// }
29+
//
30+
// if (nums[l] === nums[r]) {
31+
// return true
32+
// } else {
33+
// r--;
34+
// }
35+
// }
36+
//
37+
// return false;
38+
// }

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ Quick create in bash
4949
```ssh
5050
chapter=217 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js
5151
# And TOC above
52+
alias x="node ./$chapter/my_solution"
5253
```

0 commit comments

Comments
 (0)