Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fcf836f

Browse files
committed
feat: add solutions to lc/lcci problems
* lcci No.17.11.Find Closest * lc No.2215.Find the Difference of Two Arrays
1 parent 3939f85 commit fcf836f

File tree

12 files changed

+453
-6
lines changed

12 files changed

+453
-6
lines changed

‎lcci/17.11.Find Closest/README.md‎

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,40 @@
2929
<!-- 这里可写当前语言的特殊实现逻辑 -->
3030

3131
```python
32-
32+
class Solution:
33+
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
34+
idx1, idx2, ans = 10**5, -10**5, 10**5
35+
for i, word in enumerate(words):
36+
if word == word1:
37+
idx1 = i
38+
elif word == word2:
39+
idx2 = i
40+
ans = min(ans, abs(idx1 - idx2))
41+
return ans
3342
```
3443

3544
### **Java**
3645

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

3948
```java
40-
49+
class Solution {
50+
public int findClosest(String[] words, String word1, String word2) {
51+
int idx1 = 100000;
52+
int idx2 = -100000;
53+
int ans = 100000;
54+
for (int i = 0; i < words.length; ++i) {
55+
String word = words[i];
56+
if (word.equals(word1)) {
57+
idx1 = i;
58+
} else if (word.equals(word2)) {
59+
idx2 = i;
60+
}
61+
ans = Math.min(ans, Math.abs(idx1 - idx2));
62+
}
63+
return ans;
64+
}
65+
}
4166
```
4267

4368
### **TypeScript**
@@ -61,6 +86,56 @@ function findClosest(words: string[], word1: string, word2: string): number {
6186
}
6287
```
6388

89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int findClosest(vector<string>& words, string word1, string word2) {
95+
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
96+
for (int i = 0; i < words.size(); ++i)
97+
{
98+
string word = words[i];
99+
if (word == word1) idx1 = i;
100+
else if (word == word2) idx2 = i;
101+
ans = min(ans, abs(idx1 - idx2));
102+
}
103+
return ans;
104+
}
105+
};
106+
```
107+
108+
### **Go**
109+
110+
```go
111+
func findClosest(words []string, word1 string, word2 string) int {
112+
idx1, idx2, ans := 100000, -100000, 100000
113+
for i, word := range words {
114+
if word == word1 {
115+
idx1 = i
116+
} else if word == word2 {
117+
idx2 = i
118+
}
119+
ans = min(ans, abs(idx1-idx2))
120+
}
121+
return ans
122+
}
123+
124+
func min(a, b int) int {
125+
if a < b {
126+
return a
127+
}
128+
return b
129+
}
130+
131+
func abs(x int) int {
132+
if x < 0 {
133+
return -x
134+
}
135+
return x
136+
}
137+
```
138+
64139
### **...**
65140

66141
```

‎lcci/17.11.Find Closest/README_EN.md‎

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,38 @@
2727
### **Python3**
2828

2929
```python
30-
30+
class Solution:
31+
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
32+
idx1, idx2, ans = 10**5, -10**5, 10**5
33+
for i, word in enumerate(words):
34+
if word == word1:
35+
idx1 = i
36+
elif word == word2:
37+
idx2 = i
38+
ans = min(ans, abs(idx1 - idx2))
39+
return ans
3140
```
3241

3342
### **Java**
3443

3544
```java
36-
45+
class Solution {
46+
public int findClosest(String[] words, String word1, String word2) {
47+
int idx1 = 100000;
48+
int idx2 = -100000;
49+
int ans = 100000;
50+
for (int i = 0; i < words.length; ++i) {
51+
String word = words[i];
52+
if (word.equals(word1)) {
53+
idx1 = i;
54+
} else if (word.equals(word2)) {
55+
idx2 = i;
56+
}
57+
ans = Math.min(ans, Math.abs(idx1 - idx2));
58+
}
59+
return ans;
60+
}
61+
}
3762
```
3863

3964
### **TypeScript**
@@ -57,6 +82,56 @@ function findClosest(words: string[], word1: string, word2: string): number {
5782
}
5883
```
5984

85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int findClosest(vector<string>& words, string word1, string word2) {
91+
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
92+
for (int i = 0; i < words.size(); ++i)
93+
{
94+
string word = words[i];
95+
if (word == word1) idx1 = i;
96+
else if (word == word2) idx2 = i;
97+
ans = min(ans, abs(idx1 - idx2));
98+
}
99+
return ans;
100+
}
101+
};
102+
```
103+
104+
### **Go**
105+
106+
```go
107+
func findClosest(words []string, word1 string, word2 string) int {
108+
idx1, idx2, ans := 100000, -100000, 100000
109+
for i, word := range words {
110+
if word == word1 {
111+
idx1 = i
112+
} else if word == word2 {
113+
idx2 = i
114+
}
115+
ans = min(ans, abs(idx1-idx2))
116+
}
117+
return ans
118+
}
119+
120+
func min(a, b int) int {
121+
if a < b {
122+
return a
123+
}
124+
return b
125+
}
126+
127+
func abs(x int) int {
128+
if x < 0 {
129+
return -x
130+
}
131+
return x
132+
}
133+
```
134+
60135
### **...**
61136

62137
```

‎lcci/17.11.Find Closest/Solution.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int findClosest(vector<string>& words, string word1, string word2) {
4+
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
5+
for (int i = 0; i < words.size(); ++i)
6+
{
7+
string word = words[i];
8+
if (word == word1) idx1 = i;
9+
else if (word == word2) idx2 = i;
10+
ans = min(ans, abs(idx1 - idx2));
11+
}
12+
return ans;
13+
}
14+
};

‎lcci/17.11.Find Closest/Solution.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func findClosest(words []string, word1 string, word2 string) int {
2+
idx1, idx2, ans := 100000, -100000, 100000
3+
for i, word := range words {
4+
if word == word1 {
5+
idx1 = i
6+
} else if word == word2 {
7+
idx2 = i
8+
}
9+
ans = min(ans, abs(idx1-idx2))
10+
}
11+
return ans
12+
}
13+
14+
func min(a, b int) int {
15+
if a < b {
16+
return a
17+
}
18+
return b
19+
}
20+
21+
func abs(x int) int {
22+
if x < 0 {
23+
return -x
24+
}
25+
return x
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int findClosest(String[] words, String word1, String word2) {
3+
int idx1 = 100000;
4+
int idx2 = -100000;
5+
int ans = 100000;
6+
for (int i = 0; i < words.length; ++i) {
7+
String word = words[i];
8+
if (word.equals(word1)) {
9+
idx1 = i;
10+
} else if (word.equals(word2)) {
11+
idx2 = i;
12+
}
13+
ans = Math.min(ans, Math.abs(idx1 - idx2));
14+
}
15+
return ans;
16+
}
17+
}

‎lcci/17.11.Find Closest/Solution.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
3+
idx1, idx2, ans = 10**5, -10**5, 10**5
4+
for i, word in enumerate(words):
5+
if word == word1:
6+
idx1 = i
7+
elif word == word2:
8+
idx2 = i
9+
ans = min(ans, abs(idx1 - idx2))
10+
return ans

‎solution/2200-2299/2215.Find the Difference of Two Arrays/README.md‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,48 @@ nums2 中的每个整数都在 nums1 中出现,因此,answer[1] = [] 。
5656
<!-- 这里可写当前语言的特殊实现逻辑 -->
5757

5858
```python
59-
59+
class Solution:
60+
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
61+
s1, s2 = set(nums1), set(nums2)
62+
return [list(s1 - s2), list(s2 - s1)]
6063
```
6164

6265
### **Java**
6366

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

6669
```java
70+
class Solution {
71+
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
72+
Set<Integer> s1 = convert(nums1);
73+
Set<Integer> s2 = convert(nums2);
74+
75+
List<List<Integer>> ans = new ArrayList<>();
76+
List<Integer> l1 = new ArrayList<>();
77+
List<Integer> l2 = new ArrayList<>();
78+
for (int v : s1) {
79+
if (!s2.contains(v)) {
80+
l1.add(v);
81+
}
82+
}
83+
for (int v : s2) {
84+
if (!s1.contains(v)) {
85+
l2.add(v);
86+
}
87+
}
88+
ans.add(l1);
89+
ans.add(l2);
90+
return ans;
91+
}
6792

93+
private Set<Integer> convert(int[] nums) {
94+
Set<Integer> s = new HashSet<>();
95+
for (int v : nums) {
96+
s.add(v);
97+
}
98+
return s;
99+
}
100+
}
68101
```
69102

70103
### **TypeScript**
@@ -87,6 +120,53 @@ var findDifference = function(nums1, nums2) {
87120
};
88121
```
89122

123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
130+
unordered_set<int> s1(nums1.begin(), nums1.end());
131+
unordered_set<int> s2(nums2.begin(), nums2.end());
132+
vector<vector<int>> ans(2);
133+
for (int v : s1)
134+
if (!s2.count(v))
135+
ans[0].push_back(v);
136+
for (int v : s2)
137+
if (!s1.count(v))
138+
ans[1].push_back(v);
139+
return ans;
140+
}
141+
};
142+
```
143+
144+
### **Go**
145+
146+
```go
147+
func findDifference(nums1 []int, nums2 []int) [][]int {
148+
s1, s2 := make(map[int]bool), make(map[int]bool)
149+
for _, v := range nums1 {
150+
s1[v] = true
151+
}
152+
for _, v := range nums2 {
153+
s2[v] = true
154+
}
155+
ans := make([][]int, 2)
156+
for v := range s1 {
157+
if !s2[v] {
158+
ans[0] = append(ans[0], v)
159+
}
160+
}
161+
for v := range s2 {
162+
if !s1[v] {
163+
ans[1] = append(ans[1], v)
164+
}
165+
}
166+
return ans
167+
}
168+
```
169+
90170
### **...**
91171

92172
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /