Skip to content

Commit b41d0f8

Browse files
committed
feat: add solutions to lc problem: No.1347
No.1347.Minimum Number of Steps to Make Two Strings Anagram
1 parent 3f71372 commit b41d0f8

File tree

9 files changed

+436
-277
lines changed

9 files changed

+436
-277
lines changed

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

+143-87
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ class Solution:
8282
return any(v << 1 in m and m[v << 1] != i for i, v in enumerate(arr))
8383
```
8484

85+
```python
86+
class Solution:
87+
def checkIfExist(self, arr: List[int]) -> bool:
88+
s = set()
89+
for v in arr:
90+
if v * 2 in s or (v % 2 == 0 and v // 2 in s):
91+
return True
92+
s.add(v)
93+
return False
94+
```
95+
8596
```python
8697
class Solution:
8798
def checkIfExist(self, arr: List[int]) -> bool:
@@ -118,6 +129,21 @@ class Solution {
118129
}
119130
```
120131

132+
```java
133+
class Solution {
134+
public boolean checkIfExist(int[] arr) {
135+
Set<Integer> s = new HashSet<>();
136+
for (int v : arr) {
137+
if (s.contains(v * 2) || (v % 2 == 0 && s.contains(v / 2))) {
138+
return true;
139+
}
140+
s.add(v);
141+
}
142+
return false;
143+
}
144+
}
145+
```
146+
121147
```java
122148
class Solution {
123149
public boolean checkIfExist(int[] arr) {
@@ -144,6 +170,123 @@ class Solution {
144170
}
145171
```
146172

173+
### **C++**
174+
175+
```cpp
176+
class Solution {
177+
public:
178+
bool checkIfExist(vector<int>& arr) {
179+
unordered_map<int, int> m;
180+
int n = arr.size();
181+
for (int i = 0; i < n; ++i) m[arr[i]] = i;
182+
for (int i = 0; i < n; ++i)
183+
if (m.count(arr[i] * 2) && m[arr[i] * 2] != i)
184+
return true;
185+
return false;
186+
}
187+
};
188+
```
189+
190+
```cpp
191+
class Solution {
192+
public:
193+
bool checkIfExist(vector<int>& arr) {
194+
unordered_set<int> s;
195+
for (int& v : arr) {
196+
if (s.count(v * 2) || (v % 2 == 0 && s.count(v / 2))) {
197+
return true;
198+
}
199+
s.insert(v);
200+
}
201+
return false;
202+
}
203+
};
204+
```
205+
206+
```cpp
207+
class Solution {
208+
public:
209+
bool checkIfExist(vector<int>& arr) {
210+
int cnt = 0;
211+
for (int& v : arr) if (v == 0) ++cnt;
212+
if (cnt > 1) return true;
213+
sort(arr.begin(), arr.end());
214+
int n = arr.size();
215+
for (int& v : arr)
216+
{
217+
if (v == 0) continue;
218+
int idx = lower_bound(arr.begin(), arr.end(), v * 2) - arr.begin();
219+
if (idx != n && arr[idx] == v * 2) return true;
220+
}
221+
return false;
222+
}
223+
};
224+
```
225+
226+
227+
### **Go**
228+
229+
```go
230+
func checkIfExist(arr []int) bool {
231+
m := make(map[int]int)
232+
for i, v := range arr {
233+
m[v] = i
234+
}
235+
for i, v := range arr {
236+
if j, ok := m[v*2]; ok && j != i {
237+
return true
238+
}
239+
}
240+
return false
241+
}
242+
```
243+
244+
```go
245+
func checkIfExist(arr []int) bool {
246+
s := map[int]bool{}
247+
for _, v := range arr {
248+
if s[v*2] || (v%2 == 0 && s[v/2]) {
249+
return true
250+
}
251+
s[v] = true
252+
}
253+
return false
254+
}
255+
```
256+
257+
```go
258+
func checkIfExist(arr []int) bool {
259+
cnt := 0
260+
for _, v := range arr {
261+
if v == 0 {
262+
cnt++
263+
if cnt > 1 {
264+
return true
265+
}
266+
}
267+
}
268+
sort.Ints(arr)
269+
n := len(arr)
270+
for _, v := range arr {
271+
if v != 0 {
272+
left, right := 0, n
273+
for left < right {
274+
mid := (left + right) >> 1
275+
if arr[mid] >= v*2 {
276+
right = mid
277+
} else {
278+
left = mid + 1
279+
}
280+
}
281+
if right != n && arr[left] == v*2 {
282+
return true
283+
}
284+
}
285+
}
286+
return false
287+
}
288+
```
289+
147290
### **TypeScript**
148291

149292
```ts
@@ -193,43 +336,6 @@ function checkIfExist(arr: number[]): boolean {
193336
}
194337
```
195338

196-
### **C++**
197-
198-
```cpp
199-
class Solution {
200-
public:
201-
bool checkIfExist(vector<int>& arr) {
202-
unordered_map<int, int> m;
203-
int n = arr.size();
204-
for (int i = 0; i < n; ++i) m[arr[i]] = i;
205-
for (int i = 0; i < n; ++i)
206-
if (m.count(arr[i] * 2) && m[arr[i] * 2] != i)
207-
return true;
208-
return false;
209-
}
210-
};
211-
```
212-
213-
```cpp
214-
class Solution {
215-
public:
216-
bool checkIfExist(vector<int>& arr) {
217-
int cnt = 0;
218-
for (int& v : arr) if (v == 0) ++cnt;
219-
if (cnt > 1) return true;
220-
sort(arr.begin(), arr.end());
221-
int n = arr.size();
222-
for (int& v : arr)
223-
{
224-
if (v == 0) continue;
225-
int idx = lower_bound(arr.begin(), arr.end(), v * 2) - arr.begin();
226-
if (idx != n && arr[idx] == v * 2) return true;
227-
}
228-
return false;
229-
}
230-
};
231-
```
232-
233339
### **JavaScript**
234340

235341
```js
@@ -287,56 +393,6 @@ var checkIfExist = function (arr) {
287393
};
288394
```
289395

290-
### **Go**
291-
292-
```go
293-
func checkIfExist(arr []int) bool {
294-
m := make(map[int]int)
295-
for i, v := range arr {
296-
m[v] = i
297-
}
298-
for i, v := range arr {
299-
if j, ok := m[v*2]; ok && j != i {
300-
return true
301-
}
302-
}
303-
return false
304-
}
305-
```
306-
307-
```go
308-
func checkIfExist(arr []int) bool {
309-
cnt := 0
310-
for _, v := range arr {
311-
if v == 0 {
312-
cnt++
313-
if cnt > 1 {
314-
return true
315-
}
316-
}
317-
}
318-
sort.Ints(arr)
319-
n := len(arr)
320-
for _, v := range arr {
321-
if v != 0 {
322-
left, right := 0, n
323-
for left < right {
324-
mid := (left + right) >> 1
325-
if arr[mid] >= v*2 {
326-
right = mid
327-
} else {
328-
left = mid + 1
329-
}
330-
}
331-
if right != n && arr[left] == v*2 {
332-
return true
333-
}
334-
}
335-
}
336-
return false
337-
}
338-
```
339-
340396
### **Rust**
341397

342398
```rust

0 commit comments

Comments
 (0)