Leetcode_53_Maximum Subarray
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43989997
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)这是一道比较经典的笔试面试题。主要考查对数组的运用。由于数组中的元素可能为正,也可能为负,所以,要得到连续元素的最大值,需对数组遍历过程中出现负值时进行判断。这样,只需遍历数组一次(初始化当前连续序列之和sum=0,最大值max=x[0]),在遍历的过程中,如果当前sum>=0,说明连续序列之和为正,将当前遍历元素的数值加到sum中;如果sum<0,说明在之前遍历过程中遇到了负数,将当前遍历元素的数值赋给sum;如果sum比当前最大值max要大,则将sum的值赋给max;遍历完数组后,max即为所求。
(3)该题主要需考虑正负数交替的情况以及全是负数的情况,详情参见下方代码。希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
*/
public class Maximum_Subarray{
public int maxSubArray(int[] x) {
if(x==null || x.length==0) return 0;
int sum = 0;
int max = x[0];
for (int i = 0; i < x.length; i++) {
if(sum>=0){
sum = sum+x[i];
}else{
sum=x[i];
}
if(sum>max){
max = sum; }
}
// for (int i = 0; i < x.length; i++) {
// for (int j = i; j < x.length; j++) {
// for (int k = i; k <= j; k++) {
// sum = sum + x[k];
// }
// if(MaxSum<sum){
// MaxSum = sum;
// }
// sum=0;
// }
// }
return max;
}
}
Leetcode_53_Maximum Subarray的更多相关文章
- [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- LeetCode-53-Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
随机推荐
- 查看4k对齐,激活.net framework 3.5
查看是否4k对齐 Win+R,打开运行窗口,在窗口中输入“msinfo32",组件”--“存储”--“磁盘”.然后可以在右边栏看到“分区起始偏移”,我们图例中有2个数值,分别是:32256字 ...
- 27 自定义View 和案例
有时安卓提供的View不满足我们的需求时可以创建一个类继承View或其子类重写方法 如 package com.qf.sxy.day28_customview.view; import android ...
- 详解EBS接口开发之采购订单导入
采购订单常用标准表简介 1.1 常用标准表 如下表中列出了与采购订单导入相关的表和说明: 表名 说明 其他信息 po.po_headers_all 采购订单头 采购订单号,采购类型,供应商,地点, ...
- 带你深入理解STL之Set和Map
在上一篇博客带你深入理解STL之RBTree中,讲到了STL中关于红黑树的实现,理解起来比较复杂,正所谓前人种树,后人乘凉,RBTree把树都种好了,接下来就该set和map这类关联式容器来" ...
- Impala中的代码生成技术
Cloudera Impala是一种为Hadoop生态系统打造的开源MPP(massive parallel processing)数据库,它主要为分析型查询负载而设计,而非OLTP.Impala能最 ...
- How to speed up Remote Desktop Connection in Win7
run following command in DOS window: netsh interface tcp set global autotuninglevel=disabled or nets ...
- Ubuntu 安装 texlive2013 及中文支持
分享一下安装和配置经验. 1.材料准备 texlive的安装包:可以百度下,这里也提供一个下载地址: http://mirror.hust.edu.cn/CTAN/systems/texlive/Im ...
- 开源负载均衡通讯分发器(LB dispatcher) - G5
from:http://bbs.csdn.net/topics/390753043 1.开发背景今天和系统运维的老大聊天,谈到一直在用的F5,行里对其评价为价格过高.功能复杂难懂,反正印象不是很好,使 ...
- TCP连接建立系列 — 客户端接收SYNACK和发送ACK
主要内容:客户端接收SYNACK.发送ACK,完成连接的建立. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 接收入口 tcp_v4_rcv |--&g ...
- Android之Notification-android学习之旅(二)
notification常用于下拉式的消息推送. Notification的构成 Nitification的实例 1.新建一个Builder,要选Notification.compat包. 2.然后用 ...