[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求和了,如果小于 ...
随机推荐
- Java多线程(汇聚页)
Java多线程(汇聚页) Java多线程总结
- Codeforces Global Round 2部分题解
传送门 好难受啊掉\(rating\)了-- \(A\ Ilya\ and\ a\ Colorful\ Walk\) 找到最后一个与第一个颜色不同的,比一下距离,然后再找到最左边和最右边与第一个颜色不 ...
- leetcode-479-Largest Palindrome Product(找到两个乘数相乘得到的最大的回文数)
题目描述: Find the largest palindrome made from the product of two n-digit numbers. Since the result cou ...
- 南昌 Max answer
https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a interva ...
- poj3250 Bad Hair Day 单调栈(递减)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24420 Accepted: 8292 Des ...
- Linux快速查看某条命令的版本和存放的位置(ls -l `which mvn`)
输入: ls -l `which mvn` 如图:
- WebStorm 快键键
ctrl+/ 单行注释ctrl+shift+/ 块注释ctrl+shift+ +/- 展开/折叠ctrl+alt+L 格式化代码ctrl+shift+ up/down 上下移动句子 Alt+回车 导入 ...
- MySQL 重命名数据库
首先创建目标库 create database trgdb; 获取所有源库的表名 use information_schema; select table_name from TABLES where ...
- 手机端全局样式表整理(mobile)
@charset "utf-8";/* * filename: global.css * description: 全局样式(包含样式重置,公共常用 ...
- Android多媒体技术之视频播放
1.Android中视频播放方式 surfaceView+MediaPlayer,通过MediaPlayer来控制视频的播放.暂停.进度等: 使用VideoView 来播放,这个类其实也是继承了Sur ...