Skip to content

feat: add solutions to lc problems: No.2956,2957 #2077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,128 @@

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

**方法一:哈希表或数组**

我们可以用两个哈希表或数组 $s1$ 和 $s2$ 分别记录两个数组中出现的元素。

接下来,我们创建一个长度为 $2$ 的数组 $ans$,其中 $ans[0]$ 表示 $nums1$ 中出现在 $s2$ 中的元素个数,$ans[1]$ 表示 $nums2$ 中出现在 $s1$ 中的元素个数。

然后,我们遍历数组 $nums1$ 中的每个元素 $x$,如果 $x$ 在 $s2$ 中出现过,则将 $ans[0]$ 加一。接着,我们遍历数组 $nums2$ 中的每个元素 $x$,如果 $x$ 在 $s1$ 中出现过,则将 $ans[1]$ 加一。

最后,我们返回数组 $ans$ 即可。

时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $nums1$ 和 $nums2$ 的长度。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
s1, s2 = set(nums1), set(nums2)
return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)]
```

### **Java**

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

```java

class Solution {
public int[] findIntersectionValues(int[] nums1, int[] nums2) {
int[] s1 = new int[101];
int[] s2 = new int[101];
for (int x : nums1) {
s1[x] = 1;
}
for (int x : nums2) {
s2[x] = 1;
}
int[] ans = new int[2];
for (int x : nums1) {
ans[0] += s2[x];
}
for (int x : nums2) {
ans[1] += s1[x];
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
int s1[101]{};
int s2[101]{};
for (int& x : nums1) {
s1[x] = 1;
}
for (int& x : nums2) {
s2[x] = 1;
}
vector<int> ans(2);
for (int& x : nums1) {
ans[0] += s2[x];
}
for (int& x : nums2) {
ans[1] += s1[x];
}
return ans;
}
};
```

### **Go**

```go
func findIntersectionValues(nums1 []int, nums2 []int) []int {
s1 := [101]int{}
s2 := [101]int{}
for _, x := range nums1 {
s1[x] = 1
}
for _, x := range nums2 {
s2[x] = 1
}
ans := make([]int, 2)
for _, x := range nums1 {
ans[0] += s2[x]
}
for _, x := range nums2 {
ans[1] += s1[x]
}
return ans
}
```

### **TypeScript**

```ts
function findIntersectionValues(nums1: number[], nums2: number[]): number[] {
const s1: number[] = Array(101).fill(0);
const s2: number[] = Array(101).fill(0);
for (const x of nums1) {
s1[x] = 1;
}
for (const x of nums2) {
s2[x] = 1;
}
const ans: number[] = Array(2).fill(0);
for (const x of nums1) {
ans[0] += s2[x];
}
for (const x of nums2) {
ans[1] += s1[x];
}
return ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,124 @@

## Solutions

**Solution 1: Hash Table or Array**

We can use two hash tables or arrays $s1$ and $s2$ to record the elements that appear in the two arrays respectively.

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$.

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]$.

Finally, we return the array $ans$.

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.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
s1, s2 = set(nums1), set(nums2)
return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)]
```

### **Java**

```java

class Solution {
public int[] findIntersectionValues(int[] nums1, int[] nums2) {
int[] s1 = new int[101];
int[] s2 = new int[101];
for (int x : nums1) {
s1[x] = 1;
}
for (int x : nums2) {
s2[x] = 1;
}
int[] ans = new int[2];
for (int x : nums1) {
ans[0] += s2[x];
}
for (int x : nums2) {
ans[1] += s1[x];
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
int s1[101]{};
int s2[101]{};
for (int& x : nums1) {
s1[x] = 1;
}
for (int& x : nums2) {
s2[x] = 1;
}
vector<int> ans(2);
for (int& x : nums1) {
ans[0] += s2[x];
}
for (int& x : nums2) {
ans[1] += s1[x];
}
return ans;
}
};
```

### **Go**

```go
func findIntersectionValues(nums1 []int, nums2 []int) []int {
s1 := [101]int{}
s2 := [101]int{}
for _, x := range nums1 {
s1[x] = 1
}
for _, x := range nums2 {
s2[x] = 1
}
ans := make([]int, 2)
for _, x := range nums1 {
ans[0] += s2[x]
}
for _, x := range nums2 {
ans[1] += s1[x]
}
return ans
}
```

### **TypeScript**

```ts
function findIntersectionValues(nums1: number[], nums2: number[]): number[] {
const s1: number[] = Array(101).fill(0);
const s2: number[] = Array(101).fill(0);
for (const x of nums1) {
s1[x] = 1;
}
for (const x of nums2) {
s2[x] = 1;
}
const ans: number[] = Array(2).fill(0);
for (const x of nums1) {
ans[0] += s2[x];
}
for (const x of nums2) {
ans[1] += s1[x];
}
return ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
int s1[101]{};
int s2[101]{};
for (int& x : nums1) {
s1[x] = 1;
}
for (int& x : nums2) {
s2[x] = 1;
}
vector<int> ans(2);
for (int& x : nums1) {
ans[0] += s2[x];
}
for (int& x : nums2) {
ans[1] += s1[x];
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
func findIntersectionValues(nums1 []int, nums2 []int) []int {
s1 := [101]int{}
s2 := [101]int{}
for _, x := range nums1 {
s1[x] = 1
}
for _, x := range nums2 {
s2[x] = 1
}
ans := make([]int, 2)
for _, x := range nums1 {
ans[0] += s2[x]
}
for _, x := range nums2 {
ans[1] += s1[x]
}
return ans
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int[] findIntersectionValues(int[] nums1, int[] nums2) {
int[] s1 = new int[101];
int[] s2 = new int[101];
for (int x : nums1) {
s1[x] = 1;
}
for (int x : nums2) {
s2[x] = 1;
}
int[] ans = new int[2];
for (int x : nums1) {
ans[0] += s2[x];
}
for (int x : nums2) {
ans[1] += s1[x];
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
s1, s2 = set(nums1), set(nums2)
return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function findIntersectionValues(nums1: number[], nums2: number[]): number[] {
const s1: number[] = Array(101).fill(0);
const s2: number[] = Array(101).fill(0);
for (const x of nums1) {
s1[x] = 1;
}
for (const x of nums2) {
s2[x] = 1;
}
const ans: number[] = Array(2).fill(0);
for (const x of nums1) {
ans[0] += s2[x];
}
for (const x of nums2) {
ans[1] += s1[x];
}
return ans;
}
Loading