File tree Expand file tree Collapse file tree 5 files changed +107
-5
lines changed
solution/1300-1399/1346.Check If N and Its Double Exist Expand file tree Collapse file tree 5 files changed +107
-5
lines changed Original file line number Diff line number Diff line change 60
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
61
62
62
``` 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
64
72
```
65
73
66
74
### ** Java**
67
75
68
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
77
70
78
``` 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
+ ```
71
93
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
+ };
72
112
```
73
113
74
114
### **...**
Original file line number Diff line number Diff line change 55
55
### ** Python3**
56
56
57
57
``` 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
59
67
```
60
68
61
69
### ** Java**
62
70
63
71
``` 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
+ ```
64
86
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
+ };
65
105
```
66
106
67
107
### **...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change 1
- import java .util .*;
2
-
3
1
class Solution {
4
2
public boolean checkIfExist (int [] arr ) {
5
3
Map <Integer , Integer > map = new HashMap <>();
6
4
for (int i = 0 ; i < arr .length ; i ++) {
7
5
map .put (arr [i ], i );
8
6
}
9
7
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 ))
11
9
return true ;
12
10
}
13
11
return false ;
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments