053. Maximum Subarray

 1 class Solution {
 2 public:
 3     int maxSubArray(vector<int>& nums) {
 4         if (nums.size() == 0) return 0;
 5             else {
 6                 int result = nums[0]; int sum = nums[0];
 7                 for (int i = 1; i < static_cast<int>(nums.size()); ++i) {
 8                     if (sum < 0) sum = nums[i];
 9                     else sum += nums[i];
10                     result = max(result, sum);
11                 }
12                 return result;
13             }
14         }
15 };

方法二:动态规划

 1 class Solution {
 2 public:
 3     int maxSubArray(vector<int>& nums) {
 4         if (nums.size() == 0) return 0;
 5         else {
 6             int result = INT_MIN, f = 0;
 7             for (int i = 0; i < static_cast<int>(nums.size()); ++ i) {
 8                 f = max(f + nums[i], nums[i]);
 9                 result = max(result, f);
10             }
11             return result;
12         }
13     }
14 };
时间: 2024-08-25 19:16:22

053. Maximum Subarray的相关文章

【LeetCode】053. Maximum Subarray

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 题解: 由于是连续子序列这个限制,所以如果k+1这个元素

Java for LeetCode 053 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. 解题思路: 看题目,一次遍历肯定能搞定问题,只需使用一个sum

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6.

41. leetcode 53. Maximum Subarray

53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum = 6. 思路:这个题还挺经典

LeetCode——Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. 原题链接: https://oj.leetcode.com/p

Lintcode42 Maximum Subarray II solution 题解

[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The number in each subarray should be contiguous.Return the largest sum. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,