Commit b7972593 by zhayao

### keep going

parent 5b21604f
 ... ... @@ -1559,7 +1559,8 @@ public: Longest Palindromic Substring // DP 还有bug // it's working now // DP problem, iterative way to fill the table and looking for the needed answer for the sub-problems class Solution { public: ... ... @@ -1578,31 +1579,65 @@ public: if(s[i] == s[i+1]) {table[i][i+1] = true; longest_pali = s.substr(i, 2);} } for(int size=2; size longest) longest_pali = s.substr(start,end-start+1); } for(int length=3; length<=s.size(); length++){ for(int start=0; start<=s.size()-length; start++){ int end = start + length -1; if(s[start] == s[end] && table[start+1][end-1]){ table[start][end] = true; if(end-start+1 > longest) longest_pali = s.substr(start, end-start+1); } } } return longest_pali; return longest_pali; } }; // recursion version??? Maximum Subarray Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. class Solution { public: int maxSubArray(vector& nums) { if (nums.empty()) return 0; if (nums.size() == 1) return nums[0]; vector> table(nums.size(), vector(nums.size(), INT_MIN)); int largest = nums[0]; for(int i=0; ilargest) largest = nums[i]; } for(int length=2; length<=nums.size(); length++){ for(int start=0; start<=nums.size()-length; start++){ int end = start+length-1; int current = nums[end] + table[start][end-1]; if (current>largest) largest = current; table[start][end] = current; } } return largest; } }; ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!