Skip to content

Commit 6f86bde

Browse files
authored
feat: add solutions to lcof2 problems: No.015, 016 (#646)
* No.015.Find All Anagrams in a String * No.016.Longest Substring Without Repeating Characters
1 parent 45de283 commit 6f86bde

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

lcof2/剑指 Offer II 015. 字符串中的所有变位词/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,40 @@ func check(window []int) bool {
150150
}
151151
```
152152

153+
### **C++**
154+
155+
```cpp
156+
class Solution {
157+
public:
158+
vector<int> findAnagrams(string s, string p) {
159+
vector<int> res;
160+
vector<int> hash(26, 0), zero(26, 0);
161+
162+
if (p.size() > s.size())
163+
return res;
164+
165+
for (int i = 0; i < p.size(); i++) {
166+
hash[p[i] - 'a']++;
167+
hash[s[i] - 'a']--;
168+
}
169+
170+
if (hash == zero)
171+
res.push_back(0);
172+
173+
for (int i = p.size(); i < s.size(); i++) {
174+
hash[s[i] - 'a']--;
175+
hash[s[i - p.size()] - 'a']++;
176+
177+
if (hash == zero)
178+
res.push_back(i - p.size() + 1);
179+
}
180+
181+
182+
return res;
183+
}
184+
};
185+
```
186+
153187
### **...**
154188

155189
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<int> findAnagrams(string s, string p) {
4+
vector<int> res;
5+
vector<int> hash(26, 0), zero(26, 0);
6+
7+
if (p.size() > s.size())
8+
return res;
9+
10+
for (int i = 0; i < p.size(); i++) {
11+
hash[p[i] - 'a']++;
12+
hash[s[i] - 'a']--;
13+
}
14+
15+
if (hash == zero)
16+
res.push_back(0);
17+
18+
for (int i = p.size(); i < s.size(); i++) {
19+
hash[s[i] - 'a']--;
20+
hash[s[i - p.size()] - 'a']++;
21+
22+
if (hash == zero)
23+
res.push_back(i - p.size() + 1);
24+
}
25+
26+
27+
return res;
28+
}
29+
};

lcof2/剑指 Offer II 016. 不含重复字符的最长子字符串/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,34 @@ func max(a, b int) int {
134134
}
135135
```
136136

137+
### **C++**
138+
139+
```cpp
140+
class Solution {
141+
public:
142+
int lengthOfLongestSubstring(string s) {
143+
if (s.size() == 0)
144+
return 0;
145+
146+
int left = 0;
147+
int maxlen = 0;
148+
unordered_set<char> hash;
149+
150+
for (int right = 0; right < s.size(); right++) {
151+
while (hash.find(s[right]) != hash.end()) {
152+
hash.erase(s[left]);
153+
left++;
154+
}
155+
156+
hash.insert(s[right]);
157+
maxlen = max(maxlen, right - left + 1);
158+
}
159+
160+
return maxlen;
161+
}
162+
};
163+
```
164+
137165
### **...**
138166

139167
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string s) {
4+
if (s.size() == 0)
5+
return 0;
6+
7+
int left = 0;
8+
int maxlen = 0;
9+
unordered_set<char> hash;
10+
11+
for (int right = 0; right < s.size(); right++) {
12+
while (hash.find(s[right]) != hash.end()) {
13+
hash.erase(s[left]);
14+
left++;
15+
}
16+
17+
hash.insert(s[right]);
18+
maxlen = max(maxlen, right - left + 1);
19+
}
20+
21+
return maxlen;
22+
}
23+
};

0 commit comments

Comments
 (0)