LeetCode_53. Maximum Subarray
53. Maximum Subarray
Given an integer array nums
, 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: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
package leetcode.easy; public class MaximumSubarray {
@org.junit.Test
public void test() {
int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
System.out.println(maxSubArray(nums));
} public int maxSubArray(int[] nums) {
int max = nums[0];
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
count = nums[i];
for (int j = i + 1; j < nums.length; j++) {
count += nums[j];
if (count > max) {
max = count;
}
}
}
return max;
}
}
LeetCode_53. Maximum Subarray的更多相关文章
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- 3月7日 Maximum Subarray
间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
随机推荐
- mailto标签来调用邮箱客户端
最近项目需要使用mailto标签来调用客户端,并且把邮件模板填到客户端. mailto 的用法: a标签直接调用: <a href="mailto:example@qq.com?cc= ...
- webpack4 打包优化
1 参考文章 彻底解决 webpack 打包文件体积过大 webpack4提升180%编译速度 详解webpack4之splitchunksPlugin代码包分拆 webpack v4 中的断舍离 开 ...
- H5--性能测试(PageSpeed Insights )
中文网站(不需要翻墙): http://www.googlespeed.cn 主要可实现: 1.测试手机与电脑打开的速度对比. 2.详细的测试结果 3.直观的统计数据,查看页面的共xx个请求.总共大小 ...
- C语言入坑指南-缓冲区溢出
前言 缓冲区溢出通常指的是向缓冲区写入了超过缓冲区所能保存的最大数据量的数据.如果说之前所提到的一些问题可能只是影响部分功能的实现,那么缓冲区溢出将可能会造成程序运行终止,被不安全代码攻击等严重问题, ...
- 2019年java技术大盘点
福州SEO:2019年互联网企业在Java开发中有哪些主流.热门的IT技术呢,下面让我们来看一下. 微服务技术 微服务架构主要有:Spring Cloud. Dubbo. Dubbox等,以 Dubb ...
- 基于jQuery制作的手风琴折叠菜单
初始化为全部隐藏 点第一个,显示第一个所隐藏的内容 当点第二个的时候,第一个的内容隐藏,第二个栏目的内容显示,以此类推 下面是代码部分 <!DOCTYPE html><html la ...
- 系统空闲时间 解决 GetLastInputInfo 负数问题
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices ...
- webstorm中不能识别react、vue alias 路径别名符号
https://blog.csdn.net/weixin_37939942/article/details/89388466 因为我平时比较喜欢使用ws做开发,所以在使用vue或react的时候只要使 ...
- sql server 子查询 和exists使用
概述 子查询的概念: 当一个查询是另一个查询的条件时,称之为子查询.子查询可以嵌套在主查询中所有位置,包括SELECT.FROM.WHERE.GROUP BY.HAVING.ORDER BY. 外面的 ...
- 遇到一张jpg的图片打不开,ps打不开,fireworks,打不开,ie8浏览器上显示不了,其他的浏览器没问题
1.在photoshop上报错; 2.在fireworks上报错 3.ie8上 其他的图片都可以,就这张不可以,没发现什么不同的地方,都是jpg格式的呀,而且谷歌浏览器能显示出来; 处理方法: 1.选 ...