File tree 4 files changed +114
-0
lines changed
剑指 Offer II 015. 字符串中的所有变位词
剑指 Offer II 016. 不含重复字符的最长子字符串
4 files changed +114
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,40 @@ func check(window []int) bool {
150
150
}
151
151
```
152
152
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
+
153
187
### ** ...**
154
188
155
189
```
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -134,6 +134,34 @@ func max(a, b int) int {
134
134
}
135
135
```
136
136
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
+
137
165
### ** ...**
138
166
139
167
```
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments