Skip to content

Commit 7b61398

Browse files
committed
小重构
1 parent 66ee9fb commit 7b61398

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

C++/LeetCodet题解(C++版).pdf

1.1 KB
Binary file not shown.

C++/chapDynamicProgramming.tex

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,20 @@ \subsubsection{分析}
193193

194194
\subsubsection{代码}
195195
\begin{Code}
196-
//LeetCode, Palindrome Partitioning II
196+
// LeetCode, Palindrome Partitioning II
197+
// 时间复杂度O(n^2),空间复杂度O(n^2)
197198
class Solution {
198199
public:
199200
int minCut(string s) {
200-
const int len = s.size();
201-
int f[len+1];
202-
bool p[len][len];
201+
const int n = s.size();
202+
int f[n+1];
203+
bool p[n][n];
204+
fill_n(&p[0][0], n * n, false);
203205
//the worst case is cutting by each char
204-
for (int i = 0; i <= len; i++)
205-
f[i] = len - 1 - i; // 最后一个f[len]=-1
206-
for (int i = 0; i < len; i++)
207-
for (int j = 0; j < len; j++)
208-
p[i][j] = false;
209-
for (int i = len - 1; i >= 0; i--) {
210-
for (int j = i; j < len; j++) {
206+
for (int i = 0; i <= n; i++)
207+
f[i] = n - 1 - i; // 最后一个f[n]=-1
208+
for (int i = n - 1; i >= 0; i--) {
209+
for (int j = i; j < n; j++) {
211210
if (s[i] == s[j] && (j - i < 2 || p[i + 1][j - 1])) {
212211
p[i][j] = true;
213212
f[i] = min(f[i], f[j + 1] + 1);

C++/chapImplement.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ \subsubsection{代码}
102102
\subsubsection{相关题目}
103103
\begindot
104104
\item Reverse Integer, 见 \S \ref{sec:reverse-integer}
105+
\item Valid Palindrome, 见 \S \ref{sec:valid-palindrome}
105106
\myenddot
106107

107108

C++/chapString.tex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ \subsubsection{代码}
4444

4545
\subsubsection{相关题目}
4646
\begindot
47-
\item
47+
\item Palindrome Number, 见 \S \ref{sec:palindrome-number}
4848
\myenddot
4949

5050

@@ -206,10 +206,10 @@ \subsubsection{代码}
206206
int atoi(const char *str) {
207207
int num = 0;
208208
int sign = 1;
209-
const int len = strlen(str);
209+
const int n = strlen(str);
210210
int i = 0;
211211

212-
while (str[i] == ' ' && i < len) i++;
212+
while (str[i] == ' ' && i < n) i++;
213213

214214
if (str[i] == '+') i++;
215215

@@ -218,7 +218,7 @@ \subsubsection{代码}
218218
i++;
219219
}
220220

221-
for (; i < len; i++) {
221+
for (; i < n; i++) {
222222
if (str[i] < '0' || str[i] > '9')
223223
break;
224224
if (num > INT_MAX / 10 ||
@@ -267,11 +267,11 @@ \subsubsection{代码}
267267
public:
268268
string addBinary(string a, string b) {
269269
string result;
270-
const size_t max_len = a.size() > b.size() ? a.size() : b.size();
270+
const size_t n = a.size() > b.size() ? a.size() : b.size();
271271
reverse(a.begin(), a.end());
272272
reverse(b.begin(), b.end());
273273
int carry = 0;
274-
for (size_t i = 0; i < max_len; i++) {
274+
for (size_t i = 0; i < n; i++) {
275275
const int ai = i < a.size() ? a[i] - '0' : 0;
276276
const int bi = i < b.size() ? b[i] - '0' : 0;
277277
const int val = (ai + bi + carry) % 2;
@@ -386,11 +386,11 @@ \subsubsection{代码}
386386
class Solution {
387387
public:
388388
string longestPalindrome(string s) {
389-
const int len = s.size();
390-
bool f[len][len];
391-
fill_n(&f[0][0], len * len, false);
389+
const int n = s.size();
390+
bool f[n][n];
391+
fill_n(&f[0][0], n * n, false);
392392
// 用 vector 会超时
393-
//vector<vector<bool> > f(len, vector<bool>(len, false));
393+
//vector<vector<bool> > f(n, vector<bool>(n, false));
394394
size_t max_len = 1, start = 0; // 最长回文子串的长度,起点
395395

396396
for (size_t i = 0; i < s.size(); i++) {

0 commit comments

Comments
 (0)