File tree 2 files changed +11
-15
lines changed
2 files changed +11
-15
lines changed Original file line number Diff line number Diff line change @@ -179,6 +179,7 @@ \subsubsection{代码}
179
179
class Solution {
180
180
public:
181
181
bool searchMatrix(const vector<vector<int>>& matrix, int target) {
182
+ if (matrix.empty()) return false;
182
183
const size_t m = matrix.size();
183
184
const size_t n = matrix.front().size();
184
185
Original file line number Diff line number Diff line change @@ -104,27 +104,22 @@ \subsubsection{使用栈}
104
104
\subsubsection {Dynamic Programming, One Pass }
105
105
\begin {Code }
106
106
// LeetCode, Longest Valid Parenthese
107
- // 时间复杂度O(n),空间复杂度O(1 )
108
- // @author 一只杰森(http://weibo.com/2197839961 )
107
+ // 时间复杂度O(n),空间复杂度O(n )
108
+ // @author 一只杰森(http://weibo.com/wjson )
109
109
class Solution {
110
110
public:
111
111
int longestValidParentheses(string s) {
112
- if (s.empty()) return 0 ;
112
+ vector<int> f (s.size(), 0) ;
113
113
int ret = 0;
114
- int* d = new int[s.size()];
115
- d[s.size() - 1] = 0;
116
114
for (int i = s.size() - 2; i >= 0; --i) {
117
- if (s[i] == ')' ) d[i] = 0;
118
- else {
119
- int match = i + d[i + 1] + 1; // case: "((...))"
120
- if (match < s.size() && s[match] == ')' ) { // found matching ')'
121
- d[i] = match - i + 1;
122
- if (match + 1 < s.size()) d[i] += d[match + 1]; // more valid sequence after j: "((...))(...)"
123
- } else {
124
- d[i] = 0; // no matching ')'
125
- }
115
+ int match = i + f[i + 1] + 1;
116
+ // case: "((...))"
117
+ if (s[i] == '(' && match < s.size() && s[match] == ')' ) {
118
+ f[i] = f[i + 1] + 2;
119
+ // if a valid sequence exist afterwards "((...))()"
120
+ if (match + 1 < s.size()) f[i] += f[match + 1];
126
121
}
127
- ret = max(ret, d [i]);
122
+ ret = max(ret, f [i]);
128
123
}
129
124
return ret;
130
125
}
You can’t perform that action at this time.
0 commit comments