Skip to content

Commit e26a69a

Browse files
authored
feat: add solutions to lc problems: No.2956,2957 (#2077)
* No.2956.Find Common Elements Between Two Arrays * No.2957.Remove Adjacent Almost-Equal Characters
1 parent fc2da90 commit e26a69a

File tree

14 files changed

+485
-12
lines changed

14 files changed

+485
-12
lines changed

solution/2900-2999/2956.Find Common Elements Between Two Arrays/README.md

+97-3
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,128 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:哈希表或数组**
56+
57+
我们可以用两个哈希表或数组 $s1$ 和 $s2$ 分别记录两个数组中出现的元素。
58+
59+
接下来,我们创建一个长度为 $2$ 的数组 $ans$,其中 $ans[0]$ 表示 $nums1$ 中出现在 $s2$ 中的元素个数,$ans[1]$ 表示 $nums2$ 中出现在 $s1$ 中的元素个数。
60+
61+
然后,我们遍历数组 $nums1$ 中的每个元素 $x$,如果 $x$ 在 $s2$ 中出现过,则将 $ans[0]$ 加一。接着,我们遍历数组 $nums2$ 中的每个元素 $x$,如果 $x$ 在 $s1$ 中出现过,则将 $ans[1]$ 加一。
62+
63+
最后,我们返回数组 $ans$ 即可。
64+
65+
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $nums1$ 和 $nums2$ 的长度。
66+
5567
<!-- tabs:start -->
5668

5769
### **Python3**
5870

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

6173
```python
62-
74+
class Solution:
75+
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
76+
s1, s2 = set(nums1), set(nums2)
77+
return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)]
6378
```
6479

6580
### **Java**
6681

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

6984
```java
70-
85+
class Solution {
86+
public int[] findIntersectionValues(int[] nums1, int[] nums2) {
87+
int[] s1 = new int[101];
88+
int[] s2 = new int[101];
89+
for (int x : nums1) {
90+
s1[x] = 1;
91+
}
92+
for (int x : nums2) {
93+
s2[x] = 1;
94+
}
95+
int[] ans = new int[2];
96+
for (int x : nums1) {
97+
ans[0] += s2[x];
98+
}
99+
for (int x : nums2) {
100+
ans[1] += s1[x];
101+
}
102+
return ans;
103+
}
104+
}
71105
```
72106

73107
### **C++**
74108

75109
```cpp
76-
110+
class Solution {
111+
public:
112+
vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
113+
int s1[101]{};
114+
int s2[101]{};
115+
for (int& x : nums1) {
116+
s1[x] = 1;
117+
}
118+
for (int& x : nums2) {
119+
s2[x] = 1;
120+
}
121+
vector<int> ans(2);
122+
for (int& x : nums1) {
123+
ans[0] += s2[x];
124+
}
125+
for (int& x : nums2) {
126+
ans[1] += s1[x];
127+
}
128+
return ans;
129+
}
130+
};
77131
```
78132
79133
### **Go**
80134
81135
```go
136+
func findIntersectionValues(nums1 []int, nums2 []int) []int {
137+
s1 := [101]int{}
138+
s2 := [101]int{}
139+
for _, x := range nums1 {
140+
s1[x] = 1
141+
}
142+
for _, x := range nums2 {
143+
s2[x] = 1
144+
}
145+
ans := make([]int, 2)
146+
for _, x := range nums1 {
147+
ans[0] += s2[x]
148+
}
149+
for _, x := range nums2 {
150+
ans[1] += s1[x]
151+
}
152+
return ans
153+
}
154+
```
82155

156+
### **TypeScript**
157+
158+
```ts
159+
function findIntersectionValues(nums1: number[], nums2: number[]): number[] {
160+
const s1: number[] = Array(101).fill(0);
161+
const s2: number[] = Array(101).fill(0);
162+
for (const x of nums1) {
163+
s1[x] = 1;
164+
}
165+
for (const x of nums2) {
166+
s2[x] = 1;
167+
}
168+
const ans: number[] = Array(2).fill(0);
169+
for (const x of nums1) {
170+
ans[0] += s2[x];
171+
}
172+
for (const x of nums2) {
173+
ans[1] += s1[x];
174+
}
175+
return ans;
176+
}
83177
```
84178

85179
### **...**

solution/2900-2999/2956.Find Common Elements Between Two Arrays/README_EN.md

+97-3
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,124 @@
4646

4747
## Solutions
4848

49+
**Solution 1: Hash Table or Array**
50+
51+
We can use two hash tables or arrays $s1$ and $s2$ to record the elements that appear in the two arrays respectively.
52+
53+
Next, we create an array $ans$ of length $2$, where $ans[0]$ represents the number of elements in $nums1$ that appear in $s2$, and $ans[1]$ represents the number of elements in $nums2$ that appear in $s1$.
54+
55+
Then, we traverse each element $x$ in the array $nums1$. If $x$ has appeared in $s2$, we increment $ans[0]$. After that, we traverse each element $x$ in the array $nums2$. If $x$ has appeared in $s1$, we increment $ans[1]$.
56+
57+
Finally, we return the array $ans$.
58+
59+
The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Here, $n$ and $m$ are the lengths of the arrays $nums1$ and $nums2$ respectively.
60+
4961
<!-- tabs:start -->
5062

5163
### **Python3**
5264

5365
```python
54-
66+
class Solution:
67+
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
68+
s1, s2 = set(nums1), set(nums2)
69+
return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)]
5570
```
5671

5772
### **Java**
5873

5974
```java
60-
75+
class Solution {
76+
public int[] findIntersectionValues(int[] nums1, int[] nums2) {
77+
int[] s1 = new int[101];
78+
int[] s2 = new int[101];
79+
for (int x : nums1) {
80+
s1[x] = 1;
81+
}
82+
for (int x : nums2) {
83+
s2[x] = 1;
84+
}
85+
int[] ans = new int[2];
86+
for (int x : nums1) {
87+
ans[0] += s2[x];
88+
}
89+
for (int x : nums2) {
90+
ans[1] += s1[x];
91+
}
92+
return ans;
93+
}
94+
}
6195
```
6296

6397
### **C++**
6498

6599
```cpp
66-
100+
class Solution {
101+
public:
102+
vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
103+
int s1[101]{};
104+
int s2[101]{};
105+
for (int& x : nums1) {
106+
s1[x] = 1;
107+
}
108+
for (int& x : nums2) {
109+
s2[x] = 1;
110+
}
111+
vector<int> ans(2);
112+
for (int& x : nums1) {
113+
ans[0] += s2[x];
114+
}
115+
for (int& x : nums2) {
116+
ans[1] += s1[x];
117+
}
118+
return ans;
119+
}
120+
};
67121
```
68122
69123
### **Go**
70124
71125
```go
126+
func findIntersectionValues(nums1 []int, nums2 []int) []int {
127+
s1 := [101]int{}
128+
s2 := [101]int{}
129+
for _, x := range nums1 {
130+
s1[x] = 1
131+
}
132+
for _, x := range nums2 {
133+
s2[x] = 1
134+
}
135+
ans := make([]int, 2)
136+
for _, x := range nums1 {
137+
ans[0] += s2[x]
138+
}
139+
for _, x := range nums2 {
140+
ans[1] += s1[x]
141+
}
142+
return ans
143+
}
144+
```
72145

146+
### **TypeScript**
147+
148+
```ts
149+
function findIntersectionValues(nums1: number[], nums2: number[]): number[] {
150+
const s1: number[] = Array(101).fill(0);
151+
const s2: number[] = Array(101).fill(0);
152+
for (const x of nums1) {
153+
s1[x] = 1;
154+
}
155+
for (const x of nums2) {
156+
s2[x] = 1;
157+
}
158+
const ans: number[] = Array(2).fill(0);
159+
for (const x of nums1) {
160+
ans[0] += s2[x];
161+
}
162+
for (const x of nums2) {
163+
ans[1] += s1[x];
164+
}
165+
return ans;
166+
}
73167
```
74168

75169
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
4+
int s1[101]{};
5+
int s2[101]{};
6+
for (int& x : nums1) {
7+
s1[x] = 1;
8+
}
9+
for (int& x : nums2) {
10+
s2[x] = 1;
11+
}
12+
vector<int> ans(2);
13+
for (int& x : nums1) {
14+
ans[0] += s2[x];
15+
}
16+
for (int& x : nums2) {
17+
ans[1] += s1[x];
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func findIntersectionValues(nums1 []int, nums2 []int) []int {
2+
s1 := [101]int{}
3+
s2 := [101]int{}
4+
for _, x := range nums1 {
5+
s1[x] = 1
6+
}
7+
for _, x := range nums2 {
8+
s2[x] = 1
9+
}
10+
ans := make([]int, 2)
11+
for _, x := range nums1 {
12+
ans[0] += s2[x]
13+
}
14+
for _, x := range nums2 {
15+
ans[1] += s1[x]
16+
}
17+
return ans
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] findIntersectionValues(int[] nums1, int[] nums2) {
3+
int[] s1 = new int[101];
4+
int[] s2 = new int[101];
5+
for (int x : nums1) {
6+
s1[x] = 1;
7+
}
8+
for (int x : nums2) {
9+
s2[x] = 1;
10+
}
11+
int[] ans = new int[2];
12+
for (int x : nums1) {
13+
ans[0] += s2[x];
14+
}
15+
for (int x : nums2) {
16+
ans[1] += s1[x];
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
3+
s1, s2 = set(nums1), set(nums2)
4+
return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function findIntersectionValues(nums1: number[], nums2: number[]): number[] {
2+
const s1: number[] = Array(101).fill(0);
3+
const s2: number[] = Array(101).fill(0);
4+
for (const x of nums1) {
5+
s1[x] = 1;
6+
}
7+
for (const x of nums2) {
8+
s2[x] = 1;
9+
}
10+
const ans: number[] = Array(2).fill(0);
11+
for (const x of nums1) {
12+
ans[0] += s2[x];
13+
}
14+
for (const x of nums2) {
15+
ans[1] += s1[x];
16+
}
17+
return ans;
18+
}

0 commit comments

Comments
 (0)