@@ -101,10 +101,10 @@ \subsubsection{动规}
101
101
// 时间复杂度O(n),空间复杂度O(1)
102
102
class Solution {
103
103
public:
104
- int maxSubArray(int A[], int n ) {
104
+ int maxSubArray(vector< int>& nums ) {
105
105
int result = INT_MIN, f = 0;
106
- for (int i = 0; i < n ; ++i) {
107
- f = max(f + A [i], A [i]);
106
+ for (int i = 0; i < nums.size() ; ++i) {
107
+ f = max(f + nums [i], nums [i]);
108
108
result = max(result, f);
109
109
}
110
110
return result;
@@ -119,22 +119,24 @@ \subsubsection{思路5}
119
119
// 时间复杂度O(n),空间复杂度O(n)
120
120
class Solution {
121
121
public:
122
- int maxSubArray(int A[], int n ) {
123
- return mcss(A, n );
122
+ int maxSubArray(vector< int>& A ) {
123
+ return mcss(A.begin(), A.end() );
124
124
}
125
125
private:
126
126
// 思路5,求最大连续子序列和
127
- static int mcss(int A[], int n) {
128
- int i, result, cur_min;
127
+ template <typename Iter>
128
+ static int mcss(Iter begin, Iter end) {
129
+ int result, cur_min;
130
+ const int n = distance(begin, end);
129
131
int *sum = new int[n + 1]; // 前n项和
130
132
131
133
sum[0] = 0;
132
134
result = INT_MIN;
133
135
cur_min = sum[0];
134
- for (i = 1; i <= n; i++) {
135
- sum[i] = sum[i - 1] + A[ i - 1] ;
136
+ for (int i = 1; i <= n; i++) {
137
+ sum[i] = sum[i - 1] + *(begin + i - 1) ;
136
138
}
137
- for (i = 1; i <= n; i++) {
139
+ for (int i = 1; i <= n; i++) {
138
140
result = max(result, sum[i] - cur_min);
139
141
cur_min = min(cur_min, sum[i]);
140
142
}
@@ -191,7 +193,7 @@ \subsubsection{代码}
191
193
// 时间复杂度O(n^2),空间复杂度O(n^2)
192
194
class Solution {
193
195
public:
194
- int minCut(string s) {
196
+ int minCut(const string& s) {
195
197
const int n = s.size();
196
198
int f[n+1];
197
199
bool p[n][n];
@@ -369,7 +371,7 @@ \subsubsection{递归}
369
371
// 递归,会超时,仅用来帮助理解
370
372
class Solution {
371
373
public:
372
- bool isInterleave(string s1, string s2, string s3) {
374
+ bool isInterleave(const string& s1, const string& s2, const string& s3) {
373
375
if (s3.length() != s1.length() + s2.length())
374
376
return false;
375
377
@@ -400,7 +402,7 @@ \subsubsection{动规}
400
402
// 二维动规,时间复杂度O(n^2),空间复杂度O(n^2)
401
403
class Solution {
402
404
public:
403
- bool isInterleave(string s1, string s2, string s3) {
405
+ bool isInterleave(const string& s1, const string& s2, const string& s3) {
404
406
if (s3.length() != s1.length() + s2.length())
405
407
return false;
406
408
@@ -430,7 +432,7 @@ \subsubsection{动规+滚动数组}
430
432
// 二维动规+滚动数组,时间复杂度O(n^2),空间复杂度O(n)
431
433
class Solution {
432
434
public:
433
- bool isInterleave(string s1, string s2, string s3) {
435
+ bool isInterleave(const string& s1, const string& s2, const string& s3) {
434
436
if (s1.length() + s2.length() != s3.length())
435
437
return false;
436
438
@@ -528,12 +530,12 @@ \subsubsection{分析}
528
530
\subsubsection {递归 }
529
531
530
532
\begin {Code }
531
- // LeetCode, Interleaving String
533
+ // LeetCode, Scramble String
532
534
// 递归,会超时,仅用来帮助理解
533
535
// 时间复杂度O(n^6),空间复杂度O(1)
534
536
class Solution {
535
537
public:
536
- bool isScramble(string s1, string s2) {
538
+ bool isScramble(const string& s1, const string& s2) {
537
539
return isScramble(s1.begin(), s1.end(), s2.begin());
538
540
}
539
541
private:
@@ -559,11 +561,11 @@ \subsubsection{递归}
559
561
560
562
\subsubsection {动规 }
561
563
\begin {Code }
562
- // LeetCode, Interleaving String
564
+ // LeetCode, Scramble String
563
565
// 动规,时间复杂度O(n^3),空间复杂度O(n^3)
564
566
class Solution {
565
567
public:
566
- bool isScramble(string s1, string s2) {
568
+ bool isScramble(const string& s1, const string& s2) {
567
569
const int N = s1.size();
568
570
if (N != s2.size()) return false;
569
571
@@ -597,16 +599,16 @@ \subsubsection{动规}
597
599
598
600
\subsubsection {递归+剪枝 }
599
601
\begin {Code }
600
- // LeetCode, Interleaving String
602
+ // LeetCode, Scramble String
601
603
// 递归+剪枝
602
604
// 时间复杂度O(n^6),空间复杂度O(1)
603
605
class Solution {
604
606
public:
605
- bool isScramble(string s1, string s2) {
607
+ bool isScramble(const string& s1, const string& s2) {
606
608
return isScramble(s1.begin(), s1.end(), s2.begin());
607
609
}
608
610
private:
609
- typedef string::iterator Iterator;
611
+ typedef string::const_iterator Iterator;
610
612
bool isScramble(Iterator first1, Iterator last1, Iterator first2) {
611
613
auto length = distance(first1, last1);
612
614
auto last2 = next(first2, length);
@@ -634,12 +636,12 @@ \subsubsection{递归+剪枝}
634
636
635
637
\subsubsection {备忘录法 }
636
638
\begin {Code }
637
- // LeetCode, Interleaving String
639
+ // LeetCode, Scramble String
638
640
// 递归+map做cache
639
- // 时间复杂度O(n^3),空间复杂度O(n^3)
641
+ // 时间复杂度O(n^3),空间复杂度O(n^3), TLE
640
642
class Solution {
641
643
public:
642
- bool isScramble(string s1, string s2) {
644
+ bool isScramble(const string& s1, const string& s2) {
643
645
cache.clear();
644
646
return isScramble(s1.begin(), s1.end(), s2.begin());
645
647
}
@@ -694,14 +696,14 @@ \subsubsection{备忘录法}
694
696
};
695
697
}
696
698
697
- // LeetCode, Interleaving String
699
+ // LeetCode, Scramble String
698
700
// 递归+unordered_map做cache,比map快
699
701
// 时间复杂度O(n^3),空间复杂度O(n^3)
700
702
class Solution {
701
703
public:
702
704
unordered_map<Key, bool> cache;
703
705
704
- bool isScramble(string s1, string s2) {
706
+ bool isScramble(const string& s1, const string& s2) {
705
707
cache.clear();
706
708
return isScramble(s1.begin(), s1.end(), s2.begin());
707
709
}
0 commit comments