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 4395bf6

Browse files
committed
feat: add solutions to lc problem: No.1346. Check If N and Its Double
Exist
1 parent d8b94f1 commit 4395bf6

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,55 @@
6060
<!-- 这里可写当前语言的特殊实现逻辑 -->
6161

6262
```python
63-
63+
class Solution:
64+
def checkIfExist(self, arr: List[int]) -> bool:
65+
map = collections.defaultdict(int)
66+
for i, num in enumerate(arr):
67+
map[num] = i
68+
for i, num in enumerate(arr):
69+
if num << 1 in map and i != map[num << 1]:
70+
return True
71+
return False
6472
```
6573

6674
### **Java**
6775

6876
<!-- 这里可写当前语言的特殊实现逻辑 -->
6977

7078
```java
79+
class Solution {
80+
public boolean checkIfExist(int[] arr) {
81+
Map<Integer, Integer> map = new HashMap<>();
82+
for (int i = 0; i < arr.length; i++) {
83+
map.put(arr[i], i);
84+
}
85+
for (int i = 0; i < arr.length; i++) {
86+
if (map.containsKey(arr[i] << 1) && i != map.get(arr[i] << 1))
87+
return true;
88+
}
89+
return false;
90+
}
91+
}
92+
```
7193

94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
bool checkIfExist(vector<int>& arr) {
100+
unordered_map<int, int> map;
101+
for (int i = 0; i < arr.size(); ++i) {
102+
map[arr[i]] = i;
103+
}
104+
for (int i = 0; i < arr.size(); ++i) {
105+
if (map.find(arr[i] * 2) != map.end() && i != map[arr[i] * 2]) {
106+
return true;
107+
}
108+
}
109+
return false;
110+
}
111+
};
72112
```
73113
74114
### **...**

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,53 @@
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def checkIfExist(self, arr: List[int]) -> bool:
60+
map = collections.defaultdict(int)
61+
for i, num in enumerate(arr):
62+
map[num] = i
63+
for i, num in enumerate(arr):
64+
if num << 1 in map and i != map[num << 1]:
65+
return True
66+
return False
5967
```
6068

6169
### **Java**
6270

6371
```java
72+
class Solution {
73+
public boolean checkIfExist(int[] arr) {
74+
Map<Integer, Integer> map = new HashMap<>();
75+
for (int i = 0; i < arr.length; i++) {
76+
map.put(arr[i], i);
77+
}
78+
for (int i = 0; i < arr.length; i++) {
79+
if (map.containsKey(arr[i] << 1) && i != map.get(arr[i] << 1))
80+
return true;
81+
}
82+
return false;
83+
}
84+
}
85+
```
6486

87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
bool checkIfExist(vector<int>& arr) {
93+
unordered_map<int, int> map;
94+
for (int i = 0; i < arr.size(); ++i) {
95+
map[arr[i]] = i;
96+
}
97+
for (int i = 0; i < arr.size(); ++i) {
98+
if (map.find(arr[i] * 2) != map.end() && i != map[arr[i] * 2]) {
99+
return true;
100+
}
101+
}
102+
return false;
103+
}
104+
};
65105
```
66106
67107
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool checkIfExist(vector<int>& arr) {
4+
unordered_map<int, int> map;
5+
for (int i = 0; i < arr.size(); ++i) {
6+
map[arr[i]] = i;
7+
}
8+
for (int i = 0; i < arr.size(); ++i) {
9+
if (map.find(arr[i] * 2) != map.end() && i != map[arr[i] * 2]) {
10+
return true;
11+
}
12+
}
13+
return false;
14+
}
15+
};

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import java.util.*;
2-
31
class Solution {
42
public boolean checkIfExist(int[] arr) {
53
Map<Integer, Integer> map = new HashMap<>();
64
for (int i = 0; i < arr.length; i++) {
75
map.put(arr[i], i);
86
}
97
for (int i = 0; i < arr.length; i++) {
10-
if (map.containsKey(arr[i] * 2) && i != map.get(arr[i] * 2))
8+
if (map.containsKey(arr[i] << 1) && i != map.get(arr[i] << 1))
119
return true;
1210
}
1311
return false;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def checkIfExist(self, arr: List[int]) -> bool:
3+
map = collections.defaultdict(int)
4+
for i, num in enumerate(arr):
5+
map[num] = i
6+
for i, num in enumerate(arr):
7+
if num << 1 in map and i != map[num << 1]:
8+
return True
9+
return False

0 commit comments

Comments
(0)

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