Skip to content

Commit 87bdda1

Browse files
committed
feat: add solutions to lc problems: No.0760,1198
1 parent 40f5a8a commit 87bdda1

File tree

8 files changed

+133
-28
lines changed

8 files changed

+133
-28
lines changed

solution/0700-0799/0760.Find Anagram Mappings/README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,36 @@ B = [50, 12, 32, 46, 28]
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```python
53-
53+
class Solution:
54+
def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:
55+
mapper = collections.defaultdict(set)
56+
for i, num in enumerate(nums2):
57+
mapper[num].add(i)
58+
return [mapper[num].pop() for num in nums1]
5459
```
5560

5661
### **Java**
5762

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

6065
```java
61-
66+
class Solution {
67+
public int[] anagramMappings(int[] nums1, int[] nums2) {
68+
Map<Integer, Set<Integer>> map = new HashMap<>();
69+
for (int i = 0; i < nums2.length; ++i) {
70+
Set<Integer> s = map.getOrDefault(nums2[i], new HashSet<>());
71+
s.add(i);
72+
map.put(nums2[i], s);
73+
}
74+
int[] res = new int[nums1.length];
75+
for (int i = 0; i < nums1.length; ++i) {
76+
int idx = map.get(nums1[i]).iterator().next();
77+
res[i] = idx;
78+
map.get(nums1[i]).remove(idx);
79+
}
80+
return res;
81+
}
82+
}
6283
```
6384

6485
### **...**

solution/0700-0799/0760.Find Anagram Mappings/README_EN.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,34 @@ and so on.
6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:
72+
mapper = collections.defaultdict(set)
73+
for i, num in enumerate(nums2):
74+
mapper[num].add(i)
75+
return [mapper[num].pop() for num in nums1]
7176
```
7277

7378
### **Java**
7479

7580
```java
76-
81+
class Solution {
82+
public int[] anagramMappings(int[] nums1, int[] nums2) {
83+
Map<Integer, Set<Integer>> map = new HashMap<>();
84+
for (int i = 0; i < nums2.length; ++i) {
85+
Set<Integer> s = map.getOrDefault(nums2[i], new HashSet<>());
86+
s.add(i);
87+
map.put(nums2[i], s);
88+
}
89+
int[] res = new int[nums1.length];
90+
for (int i = 0; i < nums1.length; ++i) {
91+
int idx = map.get(nums1[i]).iterator().next();
92+
res[i] = idx;
93+
map.get(nums1[i]).remove(idx);
94+
}
95+
return res;
96+
}
97+
}
7798
```
7899

79100
### **...**

solution/0700-0799/0760.Find Anagram Mappings/Solution.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import java.util.*;
2-
31
class Solution {
4-
public int[] anagramMappings(int[] A, int[] B) {
5-
Map<Integer, Integer> map = new HashMap<>();
6-
for (int i = 0; i < B.length; i++) {
7-
map.put(B[i], i);
2+
public int[] anagramMappings(int[] nums1, int[] nums2) {
3+
Map<Integer, Set<Integer>> map = new HashMap<>();
4+
for (int i = 0; i < nums2.length; ++i) {
5+
Set<Integer> s = map.getOrDefault(nums2[i], new HashSet<>());
6+
s.add(i);
7+
map.put(nums2[i], s);
88
}
9-
int[] res = new int[B.length];
10-
int j = 0;
11-
for (int k : A) {
12-
res[j++] = map.get(k);
9+
int[] res = new int[nums1.length];
10+
for (int i = 0; i < nums1.length; ++i) {
11+
int idx = map.get(nums1[i]).iterator().next();
12+
res[i] = idx;
13+
map.get(nums1[i]).remove(idx);
1314
}
1415
return res;
1516
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
class Solution:
2-
def anagramMappings(self, A, B):
3-
"""
4-
:type A: List[int]
5-
:type B: List[int]
6-
:rtype: List[int]
7-
"""
8-
record = {val: i for i, val in enumerate(B)}
9-
return [record[val] for val in A]
2+
def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:
3+
mapper = collections.defaultdict(set)
4+
for i, num in enumerate(nums2):
5+
mapper[num].add(i)
6+
return [mapper[num].pop() for num in nums1]

solution/1100-1199/1198.Find Smallest Common Element in All Rows/README.md

+25-3
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,49 @@
2929
<li><code>mat[i]</code> 已按严格递增顺序排列。</li>
3030
</ul>
3131

32-
3332
## 解法
3433

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

36+
“计数器”实现。
37+
3738
<!-- tabs:start -->
3839

3940
### **Python3**
4041

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

4344
```python
44-
45+
class Solution:
46+
def smallestCommonElement(self, mat: List[List[int]]) -> int:
47+
counter = collections.Counter()
48+
for row in mat:
49+
for num in row:
50+
counter[num] += 1
51+
if counter[num] == len(mat):
52+
return num
53+
return -1
4554
```
4655

4756
### **Java**
4857

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

5160
```java
52-
61+
class Solution {
62+
public int smallestCommonElement(int[][] mat) {
63+
int[] counter = new int[10001];
64+
for (int[] row : mat) {
65+
for (int num : row) {
66+
++counter[num];
67+
if (counter[num] == mat.length) {
68+
return num;
69+
}
70+
}
71+
}
72+
return -1;
73+
}
74+
}
5375
```
5476

5577
### **...**

solution/1100-1199/1198.Find Smallest Common Element in All Rows/README_EN.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,41 @@
3434
<li><code>mat[i]</code> is sorted in strictly increasing order.</li>
3535
</ul>
3636

37-
3837
## Solutions
3938

4039
<!-- tabs:start -->
4140

4241
### **Python3**
4342

4443
```python
45-
44+
class Solution:
45+
def smallestCommonElement(self, mat: List[List[int]]) -> int:
46+
counter = collections.Counter()
47+
for row in mat:
48+
for num in row:
49+
counter[num] += 1
50+
if counter[num] == len(mat):
51+
return num
52+
return -1
4653
```
4754

4855
### **Java**
4956

5057
```java
51-
58+
class Solution {
59+
public int smallestCommonElement(int[][] mat) {
60+
int[] counter = new int[10001];
61+
for (int[] row : mat) {
62+
for (int num : row) {
63+
++counter[num];
64+
if (counter[num] == mat.length) {
65+
return num;
66+
}
67+
}
68+
}
69+
return -1;
70+
}
71+
}
5272
```
5373

5474
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int smallestCommonElement(int[][] mat) {
3+
int[] counter = new int[10001];
4+
for (int[] row : mat) {
5+
for (int num : row) {
6+
++counter[num];
7+
if (counter[num] == mat.length) {
8+
return num;
9+
}
10+
}
11+
}
12+
return -1;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def smallestCommonElement(self, mat: List[List[int]]) -> int:
3+
counter = collections.Counter()
4+
for row in mat:
5+
for num in row:
6+
counter[num] += 1
7+
if counter[num] == len(mat):
8+
return num
9+
return -1

0 commit comments

Comments
 (0)