[LeetCode]Maximum Subarray题解
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.
这是动态规划的一道较简单的题目。主要思想是,以下标为index的数字作为结尾的所有subarray中,和最大的那个子数列 ,与以下标为index-1的数字作为结尾的和最大的子数列有关系。
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int temp[nums.size()],max;
for(int i = 0; i < nums.size(); i++){
if(i==0){
temp[i] = nums[i];
max = nums[i];
}
else{
if(temp[i-1]>0){
temp[i] = temp[i-1] + nums[i];
}else{
temp[i] = nums[i];
}
}
max = max>temp[i]?max:temp[i];
}
return max;
}
};
[LeetCode]Maximum Subarray题解的更多相关文章
- 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 ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]Maximum Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...
- LeetCode——Maximum Subarray
Description: Find the contiguous subarray within an array (containing at least one number) which has ...
- 53. [LeetCode] Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- LeetCode Maximum Subarray (最大子段和)
题意: 给一个序列,求至少含一个元素的最大子段和? 思路: 跟求普通的最大子段和差不多,只不过需要注意一下顺序.由于至少需要一个元素,所以先将ans=nums[0].接下来可以用sum求和了,如果小于 ...
随机推荐
- python 去除字符串的首末两端的空白字符
my_str = " adsffff adsfsad " my_str.strip() 使用strip()默认将 str 两端的空白字符去除掉 同时还有rstrip() 和 lst ...
- webpack 踩的坑
我是看着这篇博客学习的 http://www.jianshu.com/p/42e11515c10f# 看到loaders的时候,按照博主写法去试 结果报错....找了好久,上网查了好多 也看错误信息了 ...
- java_对象序列化
对象序列化(serializable) 序列化读:ObjectInputStream ois=new ObjectInputStream(new FileInputStream("./gg ...
- 安卓手机移动端Web开发调试之Chrome远程调试(Remote Debugging)
一.让安卓打debug模式的apk包 二.将电脑中的chrome升级到最新版本,在chrome浏览器地址栏中输入chrome://inspect/#devices: 在智能手机还未普及时,移动设备的调 ...
- js 移动端获取当前用户的经纬度
一.HTML5 geolocation的属性 if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(onSuccess ...
- stark - 5 ⇲ 其他常用功能
Ⅰ 排序 当数据量增多,对于数据 我们应该能够指定如何排序的.且此功能应该是可以给用户自定义进行配置的. 这是StarkHandler类的方法1 order_list = [] def get_ord ...
- 解决flex布局下, elementui table组件不能跟随父组件的宽度而变化的bug
bug: 我在flex布局的元素中使用了elementui的table组件,饿了么的table上会被加一个动态的宽度, 当第一次改变flex元素宽度的时候.table的动态宽度会变化,第二次和以后就不 ...
- (转)DB2下载地址总结
原文:https://blog.csdn.net/huozengguang/article/details/58602910 DB2 v8.2,v9.1,v9.5,v9.7下载地址 下列都是完全版包含 ...
- normalize.css源码解析
什么是normalize.css? 它是为了帮助我们统一各个浏览器的样式和消除bug的css库. 为什么需要normalize.css,有什么好处? 不像一些reset.css,normalize. ...
- hibernate的反向生成改懒加载的地方
改变懒加载只需要把生成的文件中的获取类型改为eager fetch = FetchType.EAGER @ManyToOne(fetch = FetchType.EAGER)//把懒加载换成饿加载模式 ...