Skip to content

Commit 9236c10

Browse files
committed
update
1 parent 71f6f24 commit 9236c10

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

ImplementStrStr/ImplementStrStr.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
// Complexity:
88
// brute force, O(n*m) time, O(1) space
9+
// Rabin - Karp(RK), O(n + m) average and O(n*m) worst case time, O(1) space
910
// Knuth-Morris-Pratt Algorithm (KMP), O(n+m) time, O(n) space
1011
//============================================================================
1112

@@ -15,13 +16,15 @@
1516

1617
using namespace std;
1718

19+
#define B 31 // >= size of the alphabet
20+
#define M 29989 // a large enough prime number
21+
1822
class Solution {
1923
public:
2024
char * strStr(char * haystack, char * needle) {
21-
return strStr3(haystack, needle);
25+
return strStr2(haystack, needle);
2226
}
2327

24-
2528
char *strStr1(char *haystack, char *needle) {
2629
if (haystack == NULL || needle == NULL) return NULL;
2730
int n = strlen(haystack), m = strlen(needle);
@@ -33,7 +36,33 @@ class Solution {
3336
return NULL;
3437
}
3538

39+
int mod(int a, int b) {
40+
return (a % b + b) % b;
41+
}
42+
3643
char *strStr2(char *haystack, char *needle) {
44+
if (haystack == NULL || needle == NULL) return NULL;
45+
int n = strlen(haystack), m = strlen(needle);
46+
int hn = 0;
47+
for (int i = 0; i < m; i++) hn = mod(hn*B + needle[i], M);
48+
int hh = 0;
49+
for (int i = 0; i < m; i++) hh = mod(hh*B + haystack[i], M);
50+
if (hh == hn) return haystack;
51+
int E = 1; // E = B^(m-1)
52+
for (int i = 1; i < m; i++) E = mod(E*B, M);
53+
for (int i = m; i < n; i++) {
54+
hh = mod(hh - mod(haystack[i - m] * E, M), M);
55+
hh = mod(hh*B + haystack[i], M);
56+
if (hh == hn) {
57+
int j = 0;
58+
while (j < m && haystack[i + j] == needle[j]) j++;
59+
return haystack + i - m + 1;
60+
}
61+
}
62+
return NULL;
63+
}
64+
65+
char *strStr3(char *haystack, char *needle) {
3766
if (haystack == NULL || needle == NULL) return NULL;
3867
int m = strlen(needle);
3968
if (m == 0) return haystack;

MedianofTwoSortedArrays/MedianofTwoSortedArrays.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Solution {
2626
}
2727

2828
double findMedianSortedArrays(int A[], int m, int B[], int n) {
29-
int total = m + n;
30-
if (total % 2) return findKth(A, m, B, n, total / 2 + 1);
31-
return (findKth(A, m, B, n, total / 2) + findKth(A, m, B, n, total / 2 + 1)) / 2.0;
29+
int total = m + n, half = total / 2;
30+
if (total % 2) return findKth(A, m, B, n, half + 1);
31+
return (findKth(A, m, B, n, half) + findKth(A, m, B, n, half + 1)) / 2.0;
3232
}
3333

3434
int findKth(int A[], int m, int B[], int n, int k) {

WordLadderII/WordLadderII.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,43 @@ using namespace std;
3030

3131
class Solution {
3232
public:
33-
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
34-
unordered_map<string, vector<string> > from;
33+
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
3534
unordered_set<string> cq, nq, visit;
35+
unordered_map<string, vector<string> > froms;
3636
cq.insert(start);
3737
while (!cq.empty() && !cq.count(end)) {
3838
for (auto & cur : cq) visit.insert(cur);
3939
for (auto & cur : cq) {
40-
string next = cur;
40+
auto next = cur;
4141
for (int i = 0; i < next.size(); i++) {
4242
char t = next[i];
4343
for (char c = 'a'; c <= 'z'; c++) {
4444
next[i] = c;
45-
if (next == end || (dict.count(next) && !visit.count(next))) from[next].push_back(cur), nq.insert(next);
45+
if (next == end || (dict.count(next) && !visit.count(next))) nq.insert(next), froms[next].push_back(cur);
4646
}
4747
next[i] = t;
4848
}
4949
}
5050
swap(cq, nq);
5151
nq.clear();
5252
}
53-
vector<vector<string> > res;
5453
vector<string> path;
55-
getPath(end, start, from, path, res);
54+
vector<vector<string> > res;
55+
path.push_back(end);
56+
go(end, start, froms, path, res);
5657
return res;
5758
}
5859

59-
void getPath(string & cur, string & start, unordered_map<string, vector<string> > & from, vector<string> & path, vector<vector<string> > & res) {
60-
path.push_back(cur);
61-
if (cur == start) res.push_back(vector<string>(path.rbegin(), path.rend()));
62-
else for (auto next : from[cur]) getPath(next, start, from, path, res);
63-
path.pop_back();
60+
void go(string & cur, string & end, unordered_map<string, vector<string> > & froms, vector<string> & path, vector<vector<string> > & res) {
61+
if (cur == end) {
62+
res.push_back(vector<string>(path.rbegin(), path.rend()));
63+
return;
64+
}
65+
for (auto & next : froms[cur]) {
66+
path.push_back(next);
67+
go(next, end, froms, path, res);
68+
path.pop_back();
69+
}
6470
}
6571
};
6672

0 commit comments

Comments
 (0)