Skip to content

Commit 42e99e8

Browse files
authored
feat: update solutions to lc problems: No.1346,1486 (#2752)
1 parent 7e70ba5 commit 42e99e8

32 files changed

+282
-1075
lines changed

solution/1300-1399/1346.Check If N and Its Double Exist/README.md

+21-346
Large diffs are not rendered by default.

solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md

+22-334
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
class Solution {
2-
public:
3-
bool checkIfExist(vector<int>& arr) {
4-
unordered_map<int, int> m;
5-
int n = arr.size();
6-
for (int i = 0; i < n; ++i) m[arr[i]] = i;
7-
for (int i = 0; i < n; ++i)
8-
if (m.count(arr[i] * 2) && m[arr[i] * 2] != i)
9-
return true;
10-
return false;
11-
}
1+
class Solution {
2+
public:
3+
bool checkIfExist(vector<int>& arr) {
4+
unordered_set<int> s;
5+
for (int x : arr) {
6+
if (s.contains(x * 2) || (x % 2 == 0 && s.contains(x / 2))) {
7+
return true;
8+
}
9+
s.insert(x);
10+
}
11+
return false;
12+
}
1213
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
func checkIfExist(arr []int) bool {
2-
m := make(map[int]int)
3-
for i, v := range arr {
4-
m[v] = i
5-
}
6-
for i, v := range arr {
7-
if j, ok := m[v*2]; ok && j != i {
8-
return true
9-
}
10-
}
11-
return false
1+
func checkIfExist(arr []int) bool {
2+
s := map[int]bool{}
3+
for _, x := range arr {
4+
if s[x*2] || (x%2 == 0 && s[x/2]) {
5+
return true
6+
}
7+
s[x] = true
8+
}
9+
return false
1210
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
class Solution {
2-
public boolean checkIfExist(int[] arr) {
3-
Map<Integer, Integer> m = new HashMap<>();
4-
int n = arr.length;
5-
for (int i = 0; i < n; ++i) {
6-
m.put(arr[i], i);
7-
}
8-
for (int i = 0; i < n; ++i) {
9-
if (m.containsKey(arr[i] << 1) && m.get(arr[i] << 1) != i) {
10-
return true;
11-
}
12-
}
13-
return false;
14-
}
1+
class Solution {
2+
public boolean checkIfExist(int[] arr) {
3+
Set<Integer> s = new HashSet<>();
4+
for (int x : arr) {
5+
if (s.contains(x * 2) || ((x % 2 == 0 && s.contains(x / 2)))) {
6+
return true;
7+
}
8+
s.add(x);
9+
}
10+
return false;
11+
}
1512
}

solution/1300-1399/1346.Check If N and Its Double Exist/Solution.js

-14
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution.php

-17
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
class Solution:
2-
def checkIfExist(self, arr: List[int]) -> bool:
3-
m = {v: i for i, v in enumerate(arr)}
4-
return any(v << 1 in m and m[v << 1] != i for i, v in enumerate(arr))
1+
class Solution:
2+
def checkIfExist(self, arr: List[int]) -> bool:
3+
s = set()
4+
for x in arr:
5+
if x * 2 in s or (x % 2 == 0 and x // 2 in s):
6+
return True
7+
s.add(x)
8+
return False

solution/1300-1399/1346.Check If N and Its Double Exist/Solution.rs

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
function checkIfExist(arr: number[]): boolean {
2-
const s = new Set();
3-
for (const v of arr) {
4-
if (s.has(v << 1) || s.has(v / 2)) {
2+
const s: Set<number> = new Set();
3+
for (const x of arr) {
4+
if (s.has(x * 2) || (x % 2 === 0 && s.has((x / 2) | 0))) {
55
return true;
66
}
7-
s.add(v);
7+
s.add(x);
88
}
99
return false;
1010
}

solution/1300-1399/1346.Check If N and Its Double Exist/Solution2.cpp

-13
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution2.go

-10
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution2.java

-12
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution2.js

-35
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution2.py

-8
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution2.rs

-30
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution2.ts

-31
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution3.cpp

-17
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution3.go

-30
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution3.java

-23
This file was deleted.

solution/1300-1399/1346.Check If N and Its Double Exist/Solution3.py

-11
This file was deleted.

0 commit comments

Comments
 (0)