1 class Solution 2 { 3 public: 4 int maxSubArray(vector<int>& nums) 5 { 6 const int n = nums.size(); 7 if (n == 0) 8 return 0; 9 int ans, i, sum; 10 for (i = sum = 0, ans = nums[0]; i < n; i++) 11 { 12 sum += n
the contiguous subarray[4,−1,2,1]has the largest sum =6. 分析:这道题是到很经典的题,关键点是要明白sum为负数的连续subarray对可能的最大值没有帮助(除该连续subarray的sum为largest sum外)。所以很简单的,如果sum小于0,我们便置sum为0。代码如下: classSolution {public:intmaxSubArray(intA[],intn...
Given an integer arraynums, 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: 6Explanation: [4,-1,2,1] has the largest sum = 6. Follow up: If you have figured out t...
How to create patch file in Visual Studio How to create "DeleteFileDialog" similar to "OpenFileDialog"? How To Create A 25-Character Product Key How to create a access database from VB code How to create a datagridview per tab sheet in a TabControl How to create a function to call a ...