@@ -101,10 +101,10 @@ \subsubsection{动规}
101101// 时间复杂度O(n),空间复杂度O(1)
102102class Solution {
103103public:
104- int maxSubArray(int A[], int n ) {
104+ int maxSubArray(vector< int>& nums ) {
105105 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]);
108108 result = max(result, f);
109109 }
110110 return result;
@@ -119,22 +119,24 @@ \subsubsection{思路5}
119119// 时间复杂度O(n),空间复杂度O(n)
120120class Solution {
121121public:
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() );
124124 }
125125private:
126126 // 思路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);
129131 int *sum = new int[n + 1]; // 前n项和
130132
131133 sum[0] = 0;
132134 result = INT_MIN;
133135 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) ;
136138 }
137- for (i = 1; i <= n; i++) {
139+ for (int i = 1; i <= n; i++) {
138140 result = max(result, sum[i] - cur_min);
139141 cur_min = min(cur_min, sum[i]);
140142 }
@@ -191,7 +193,7 @@ \subsubsection{代码}
191193// 时间复杂度O(n^2),空间复杂度O(n^2)
192194class Solution {
193195public:
194- int minCut(string s) {
196+ int minCut(const string& s) {
195197 const int n = s.size();
196198 int f[n+1];
197199 bool p[n][n];
@@ -369,7 +371,7 @@ \subsubsection{递归}
369371// 递归,会超时,仅用来帮助理解
370372class Solution {
371373public:
372- bool isInterleave(string s1, string s2, string s3) {
374+ bool isInterleave(const string& s1, const string& s2, const string& s3) {
373375 if (s3.length() != s1.length() + s2.length())
374376 return false;
375377
@@ -400,7 +402,7 @@ \subsubsection{动规}
400402// 二维动规,时间复杂度O(n^2),空间复杂度O(n^2)
401403class Solution {
402404public:
403- bool isInterleave(string s1, string s2, string s3) {
405+ bool isInterleave(const string& s1, const string& s2, const string& s3) {
404406 if (s3.length() != s1.length() + s2.length())
405407 return false;
406408
@@ -430,7 +432,7 @@ \subsubsection{动规+滚动数组}
430432// 二维动规+滚动数组,时间复杂度O(n^2),空间复杂度O(n)
431433class Solution {
432434public:
433- bool isInterleave(string s1, string s2, string s3) {
435+ bool isInterleave(const string& s1, const string& s2, const string& s3) {
434436 if (s1.length() + s2.length() != s3.length())
435437 return false;
436438
@@ -528,12 +530,12 @@ \subsubsection{分析}
528530\subsubsection {递归 }
529531
530532\begin {Code }
531- // LeetCode, Interleaving String
533+ // LeetCode, Scramble String
532534// 递归,会超时,仅用来帮助理解
533535// 时间复杂度O(n^6),空间复杂度O(1)
534536class Solution {
535537public:
536- bool isScramble(string s1, string s2) {
538+ bool isScramble(const string& s1, const string& s2) {
537539 return isScramble(s1.begin(), s1.end(), s2.begin());
538540 }
539541private:
@@ -559,11 +561,11 @@ \subsubsection{递归}
559561
560562\subsubsection {动规 }
561563\begin {Code }
562- // LeetCode, Interleaving String
564+ // LeetCode, Scramble String
563565// 动规,时间复杂度O(n^3),空间复杂度O(n^3)
564566class Solution {
565567public:
566- bool isScramble(string s1, string s2) {
568+ bool isScramble(const string& s1, const string& s2) {
567569 const int N = s1.size();
568570 if (N != s2.size()) return false;
569571
@@ -597,16 +599,16 @@ \subsubsection{动规}
597599
598600\subsubsection {递归+剪枝 }
599601\begin {Code }
600- // LeetCode, Interleaving String
602+ // LeetCode, Scramble String
601603// 递归+剪枝
602604// 时间复杂度O(n^6),空间复杂度O(1)
603605class Solution {
604606public:
605- bool isScramble(string s1, string s2) {
607+ bool isScramble(const string& s1, const string& s2) {
606608 return isScramble(s1.begin(), s1.end(), s2.begin());
607609 }
608610private:
609- typedef string::iterator Iterator;
611+ typedef string::const_iterator Iterator;
610612 bool isScramble(Iterator first1, Iterator last1, Iterator first2) {
611613 auto length = distance(first1, last1);
612614 auto last2 = next(first2, length);
@@ -634,12 +636,12 @@ \subsubsection{递归+剪枝}
634636
635637\subsubsection {备忘录法 }
636638\begin {Code }
637- // LeetCode, Interleaving String
639+ // LeetCode, Scramble String
638640// 递归+map做cache
639- // 时间复杂度O(n^3),空间复杂度O(n^3)
641+ // 时间复杂度O(n^3),空间复杂度O(n^3), TLE
640642class Solution {
641643public:
642- bool isScramble(string s1, string s2) {
644+ bool isScramble(const string& s1, const string& s2) {
643645 cache.clear();
644646 return isScramble(s1.begin(), s1.end(), s2.begin());
645647 }
@@ -694,14 +696,14 @@ \subsubsection{备忘录法}
694696};
695697}
696698
697- // LeetCode, Interleaving String
699+ // LeetCode, Scramble String
698700// 递归+unordered_map做cache,比map快
699701// 时间复杂度O(n^3),空间复杂度O(n^3)
700702class Solution {
701703public:
702704 unordered_map<Key, bool> cache;
703705
704- bool isScramble(string s1, string s2) {
706+ bool isScramble(const string& s1, const string& s2) {
705707 cache.clear();
706708 return isScramble(s1.begin(), s1.end(), s2.begin());
707709 }
0 commit comments