【LeetCode】 53. Maximum Subarray 最大子序列和
2017-06-04 16:51
429 查看
原题
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array
the contiguous subarray
思路
1. 最笨的办法
用2个for循环,遍历所有的子序列和,最后得到其中最大的一个
时间复杂度O(n^2)
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.
思路
1. 最笨的办法
用2个for循环,遍历所有的子序列和,最后得到其中最大的一个
public class Solution { public int maxSubArray(int[] nums) { int max = nums[0]; int len = nums.length; int tempSum = 0; for(int i = 0 ; i< len ;i ++){ tempSum = nums[i]; if(tempSum >max){ max = tempSum; } for(int j = i+1; j< len; j++){ tempSum += nums[j]; if(tempSum >max){ max = tempSum; } } } return max; } }
时间复杂度O(n^2)
相关文章推荐
- LeetCode WIith JS || 53. Maximum Subarray[寻求最大连续序列]
- 【leetcode】53. Maximum Subarray 连续子序列的最大和
- [leetcode]53. Maximum Subarray 最大连续子串python实现【medium】
- 53. Maximum Subarray 最大子序列和
- leetcode 53. Maximum Subarray 子数组最大和
- LeetCode[递归]53. Maximum Subarray 数组最大和
- leetcode Maximum Subarray 最大子序列
- leetcode 53. Maximum Subarray 动态规划DP + 最大子串和
- 53. Maximum Subarray 连续子序列的最大和
- [leetcode]53. Maximum Subarray 最大连续子串python实现【medium】
- (Java) LeetCode 152. Maximum Product Subarray —— 乘积最大子序列
- [LeetCode] 53. Maximum Subarray 最大子数组
- leetcode_53. Maximum Subarray-子数组最大和
- leetcode 53. Maximum Subarray-最大子数组|动态规划
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
- [leetcode]Maximum Product Subarray最大子序列乘积
- LeetCode Minimum Size Subarray Sum (最短子序列和)
- 子序列的最大几个值 maximum k elements in a subarray
- [Leetcode] 53. Maximum Subarray
- LeetCode -- 最大连续乘积子序列