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 ...
随机推荐
- BlockingQueue(阻塞队列)详解
一. 前言 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全"传输"数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量 ...
- RDO Stack:VMs cannot access external network.
Issue: There are many root causes to make your openstack vm instances cannot be reached from externa ...
- TCP发送源码学习(3)--tcp_transmit_skb
一.tcp_transmit_skb static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, g ...
- Git 处理tag和branch的命令
最近想给GitHub 上的项目设置tag,可是使用GitHub Desktop,找了一圈都没找到快速设置Tag 的地方,最后只能通过终端命令来添加了. 想要查看Git 的命令,可以使用 git --h ...
- 用Codility测试你的编码能力
没有宏观的架构设计,没有特定的框架语言.在Codility提出的一些小问题上,用最纯粹的方式测试你最基本的编码能力. Codility第一课:算法复杂度 各种算法书的开篇大多是算法分析,而复杂度(co ...
- Java编程思想阅读收获
15年8月份买了一本Java编程思想第四版中文版.之所以买中文版是因为我试读了同事的英文版发现自己英语水平还是有限,单词虽然认识,但对很多句子把握不准,这样看书太慢了,要理解英文还要理解技术有些hol ...
- Dynamics CRM2016 Web API之通过实体的primary key查询记录
CRM2016启用了webapi 而弃用了odata,作为码农的我们又开始学习新东西了. 下面是一段简单的查询代码,通过systemuser的primary key来查询一条记录 Web API查询方 ...
- Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)
demo源码,需要可以下载 1.这是一个github开源项目,先去github上面下载,github下载地址. 2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应 ...
- Android 字体设置-Typeface讲解
控件的字体设置的两种方式 常用的字体类型名称还有: Typeface.DEFAULT //常规字体类型 Typeface.DEFAULT_BOLD //黑体字体类型 Typeface.MONOSPAC ...
- Android使用局和数据实现天气项目-android学习之旅(十二)
1.首先注册聚合数据账号,下载相应的sdk 2.导入jar包和 so文件 配置Application,初始化sdk <application //自己新建的application类 androi ...