首页 > 学院 > 开发设计 > 正文

53. Maximum Subarray

2019-11-06 06:06:17
字体:
来源:转载
供稿:网友

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.

click to show more PRactice.

Subscribe to see which companies asked this question.

public class Solution {    public static int maxSubArray(int[] nums) {		int sum = 0;		int max_sum = Integer.MIN_VALUE;		for (int i = 0; i < nums.length; ++i) {			sum += nums[i];			sum = sum > nums[i] ? sum : nums[i];			max_sum = max_sum > sum ? max_sum : sum;		}		return max_sum;	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表