@@ -35,7 +35,7 @@ \subsubsection{代码}
35
35
if (!::isalnum(*left)) ++left;
36
36
else if (!::isalnum(*right)) --right;
37
37
else if (*left != *right) return false;
38
- else{ left++, right--; }
38
+ else { left++, right--; }
39
39
}
40
40
return true;
41
41
}
@@ -69,29 +69,20 @@ \subsubsection{暴力匹配}
69
69
// 暴力解法,时间复杂度O(N*M),空间复杂度O(1)
70
70
class Solution {
71
71
public:
72
- char *strStr(const char *haystack, const char *needle) {
73
- // if needle is empty return the full string
74
- if (!*needle) return (char*) haystack;
75
-
76
- const char *p1;
77
- const char *p2;
78
- const char *p1_advance = haystack;
79
- for (p2 = &needle[1]; *p2; ++p2) {
80
- p1_advance++; // advance p1_advance M-1 times
81
- }
82
-
83
- for (p1 = haystack; *p1_advance; p1_advance++) {
84
- char *p1_old = (char*) p1;
85
- p2 = needle;
86
- while (*p1 && *p2 && *p1 == *p2) {
87
- p1++;
88
- p2++;
72
+ int strStr(const string& haystack, const string& needle) {
73
+ if (needle.empty()) return 0;
74
+
75
+ const int N = haystack.size() - needle.size() + 1;
76
+ for (int i = 0; i < N; i++) {
77
+ int j = i;
78
+ int k = 0;
79
+ while (j < haystack.size() && k < needle.size() && haystack[j] == needle[k]) {
80
+ j++;
81
+ k++;
89
82
}
90
- if (!*p2) return p1_old;
91
-
92
- p1 = p1_old + 1;
83
+ if (k == needle.size()) return i;
93
84
}
94
- return nullptr ;
85
+ return -1 ;
95
86
}
96
87
};
97
88
\end {Code }
@@ -103,10 +94,8 @@ \subsubsection{KMP}
103
94
// KMP,时间复杂度O(N+M),空间复杂度O(M)
104
95
class Solution {
105
96
public:
106
- char *strStr(const char *haystack, const char *needle) {
107
- int pos = kmp(haystack, needle);
108
- if (pos == -1) return nullptr;
109
- else return (char*)haystack + pos;
97
+ int strStr(const string& haystack, const string& needle) {
98
+ return kmp(haystack.c_str(), needle.c_str());
110
99
}
111
100
private:
112
101
/*
@@ -389,7 +378,7 @@ \subsubsection{动规}
389
378
// 动规,时间复杂度O(n^2),空间复杂度O(n^2)
390
379
class Solution {
391
380
public:
392
- string longestPalindrome(string s) {
381
+ string longestPalindrome(const string& s) {
393
382
const int n = s.size();
394
383
bool f[n][n];
395
384
fill_n(&f[0][0], n * n, false);
@@ -423,7 +412,7 @@ \subsubsection{Manacher’s Algorithm}
423
412
// Transform S into T.
424
413
// For example, S = "abba" , T = "^#a#b#b#a#$" .
425
414
// ^ and $ signs are sentinels appended to each end to avoid bounds checking
426
- string preProcess(string s) {
415
+ string preProcess(const string& s) {
427
416
int n = s.length();
428
417
if (n == 0) return "^$" ;
429
418
@@ -520,6 +509,10 @@ \subsubsection{递归版}
520
509
// 递归版,时间复杂度O(n),空间复杂度O(1 )
521
510
class Solution {
522
511
public:
512
+ bool isMatch(const string& s, const string& p) {
513
+ return isMatch(s.c_str(), p.c_str());
514
+ }
515
+ private:
523
516
bool isMatch(const char *s, const char *p) {
524
517
if (*p == '\0' ) return *s == '\0' ;
525
518
@@ -596,6 +589,10 @@ \subsubsection{递归版}
596
589
// 时间复杂度O(n!*m!),空间复杂度O(n)
597
590
class Solution {
598
591
public:
592
+ bool isMatch(const string& s, const string& p) {
593
+ return isMatch(s.c_str(), p.c_str());
594
+ }
595
+ private:
599
596
bool isMatch(const char *s, const char *p) {
600
597
if (*p == '*' ) {
601
598
while (*p == '*' ) ++p; //skip continuous '*'
@@ -618,6 +615,10 @@ \subsubsection{迭代版}
618
615
// 迭代版,时间复杂度O(n*m),空间复杂度O(1 )
619
616
class Solution {
620
617
public:
618
+ bool isMatch(const string& s, const string& p) {
619
+ return isMatch(s.c_str(), p.c_str());
620
+ }
621
+ private:
621
622
bool isMatch(const char *s, const char *p) {
622
623
bool star = false;
623
624
const char *str, *ptr;
@@ -751,7 +752,7 @@ \subsubsection{有限自动机}
751
752
// finite automata,时间复杂度O(n),空间复杂度O(n)
752
753
class Solution {
753
754
public:
754
- bool isNumber(const char * s) {
755
+ bool isNumber(const string& s) {
755
756
enum InputType {
756
757
INVALID, // 0
757
758
SPACE, // 1
@@ -774,17 +775,17 @@ \subsubsection{有限自动机}
774
775
};
775
776
776
777
int state = 0;
777
- for (; *s != '\0' ; ++ s) {
778
+ for (auto ch : s) {
778
779
InputType inputType = INVALID;
779
- if (isspace(*s ))
780
+ if (isspace(ch ))
780
781
inputType = SPACE;
781
- else if (*s == '+' || *s == '-' )
782
+ else if (ch == '+' || ch == '-' )
782
783
inputType = SIGN;
783
- else if (isdigit(*s ))
784
+ else if (isdigit(ch ))
784
785
inputType = DIGIT;
785
- else if (*s == '.' )
786
+ else if (ch == '.' )
786
787
inputType = DOT;
787
- else if (*s == 'e' || *s == 'E' )
788
+ else if (ch == 'e' || ch == 'E' )
788
789
inputType = EXPONENT;
789
790
790
791
// Get next state from current state and input symbol
@@ -809,6 +810,10 @@ \subsubsection{使用strtod()}
809
810
// 偷懒,直接用 strtod(),时间复杂度O(n)
810
811
class Solution {
811
812
public:
813
+ bool isNumber (const string& s) {
814
+ return isNumber(s.c_str());
815
+ }
816
+ private:
812
817
bool isNumber (char const* s) {
813
818
char* endptr;
814
819
strtod (s, &endptr);
@@ -909,7 +914,7 @@ \subsubsection{代码}
909
914
}
910
915
}
911
916
912
- int romanToInt(string s) {
917
+ int romanToInt(const string& s) {
913
918
int result = 0;
914
919
for (size_t i = 0; i < s.size(); i++) {
915
920
if (i > 0 && map(s[i]) > map(s[i - 1])) {
@@ -1070,7 +1075,7 @@ \subsubsection{代码}
1070
1075
// 时间复杂度O(n),空间复杂度O(n)
1071
1076
class Solution {
1072
1077
public:
1073
- string simplifyPath(string const& path) {
1078
+ string simplifyPath(const string & path) {
1074
1079
vector<string> dirs; // 当做栈
1075
1080
1076
1081
for (auto i = path.begin(); i != path.end();) {
@@ -1137,10 +1142,9 @@ \subsubsection{用 STL}
1137
1142
// 时间复杂度O(n),空间复杂度O(1 )
1138
1143
class Solution {
1139
1144
public:
1140
- int lengthOfLastWord(const char *s) {
1141
- const string str(s);
1142
- auto first = find_if(str.rbegin(), str.rend(), ::isalpha);
1143
- auto last = find_if_not(first, str.rend(), ::isalpha);
1145
+ int lengthOfLastWord(const string& s) {
1146
+ auto first = find_if(s.rbegin(), s.rend(), ::isalpha);
1147
+ auto last = find_if_not(first, s.rend(), ::isalpha);
1144
1148
return distance(first, last);
1145
1149
}
1146
1150
};
@@ -1154,6 +1158,10 @@ \subsubsection{顺序扫描}
1154
1158
// 时间复杂度O(n),空间复杂度O(1 )
1155
1159
class Solution {
1156
1160
public:
1161
+ int lengthOfLastWord(const string& s) {
1162
+ return lengthOfLastWord(s.c_str());
1163
+ }
1164
+ private:
1157
1165
int lengthOfLastWord(const char *s) {
1158
1166
int len = 0;
1159
1167
while (*s) {
0 commit comments