Skip to content

Commit a3b1f11

Browse files
committed
string substring 2 pointer problems
1 parent be64de0 commit a3b1f11

4 files changed

+100
-0
lines changed

172. Factorial Trailing Zeroes.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int trailingZeroes(int n) {
4+
long five = 5;
5+
long ans = 0;
6+
while(five <= n) {
7+
ans += n/five;
8+
five = five * 5;
9+
}
10+
return ans;
11+
12+
}
13+
};

24. Swap Nodes in Pairs.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* swapPairs(ListNode* head) {
12+
13+
if(!head)
14+
return nullptr;
15+
16+
if(!(head->next))
17+
return head;
18+
19+
ListNode *newhead = head->next;
20+
ListNode *cur = head;
21+
ListNode *next;
22+
ListNode *prev = nullptr;
23+
24+
while(cur && cur->next) {
25+
next = cur->next;
26+
cur->next = next->next;
27+
next->next = cur;
28+
29+
if (prev) {
30+
prev->next = next;
31+
}
32+
prev = cur;
33+
cur = cur->next;
34+
}
35+
return newhead;
36+
}
37+
};
File renamed without changes.

76. Minimum Window Substring.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
public:
3+
string minWindow(string s, string t) {
4+
5+
if(s.empty() || t.empty())
6+
return "";
7+
8+
unordered_map<char, int> m;
9+
10+
for(auto c:t) {
11+
m[c]++;
12+
}
13+
14+
int n = s.length();
15+
int end = 0;
16+
int start = 0;
17+
int minlen = INT_MAX;
18+
int counter = t.length();
19+
string result = "";
20+
21+
while(end < n) {
22+
23+
if(m[s[end]] > 0)
24+
counter--;
25+
26+
m[s[end]]--;
27+
28+
end++;
29+
30+
while (counter == 0) {
31+
32+
if (end - start < minlen) {
33+
minlen = end-start;
34+
result = s.substr(start, minlen);
35+
}
36+
m[s[start]]++;
37+
38+
if(m[s[start]] > 0)
39+
counter++;
40+
41+
start++;
42+
}
43+
44+
}
45+
46+
return result;
47+
48+
49+
}
50+
};

0 commit comments

Comments
 (0)