LeetCode Maximum Subarray
来源:互联网 发布:php foreach用法 编辑:程序博客网 时间:2024/06/14 18:18
description:
Given an array of integers, find a contiguous subarray which has the largest sum.
Notice
The subarray should contain at least one number.
Have you met this question in a real interview? Yes
Example
Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.
解题思路:使用滑窗法,进行处理。当算计算的值比原有的值小的时候,要进行最大值回滚。同时还要有一个全局的变量来进行最大值的记录。
public class Solution { /** * @param nums: A list of integers * @return: A integer indicate the sum of max subarray */ public int maxSubArray(int[] nums) { // write your code if (nums == null || nums.length == 0) { return 0; } int max = Integer.MIN_VALUE; int sum = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; max = Math.max(sum, max); sum = Math.max(0, sum); } return max; }}
0 0
- 【LeetCode】Maximum Subarray 和 Maximum Product Subarray
- LeetCode: Maximum Subarray
- LeetCode Maximum Subarray
- [Leetcode] Maximum Subarray
- LeetCode: Maximum Subarray
- leetcode 25: Maximum Subarray
- [LeetCode] Maximum Subarray
- [Leetcode] Maximum Subarray
- [LeetCode]Maximum Subarray
- [leetcode]Maximum Subarray
- LeetCode-Maximum Subarray
- [leetcode] Maximum Subarray
- LeetCode 45: Maximum Subarray
- LeetCode - Maximum Subarray
- LeetCode:Maximum Subarray
- LeetCode 53: Maximum Subarray
- 【leetcode】Maximum Subarray
- Leetcode Maximum Subarray
- ajax同步和异步的区别
- android 多渠道打包
- php中数组的操作函数
- JAVA并发之BlockingQueue阻塞队列
- 集训4.10-数页码
- LeetCode Maximum Subarray
- vuejs项目运行的探索之路
- muduo源码分析2:整体框架
- IE缓存问题导致生成新的证书之后打开还是旧的证书
- eclipse对freemarker模板文件ftl文件安装可视化插件JBoss Application Development
- 1007. 素数对猜想 (20)
- 专业的格式转换工具pdf2cad发布v11,支持当前所有的Windows和Mac操作系统
- sqlplus登录oracle时ORA-01017: invalid username/password; logon denied的错误
- 洛谷 P1162 填涂颜色