LeetCode OJ: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
.
典型的DP问题,递推条件还是想了有点长时间,代码如下所示:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
vector<int> ans;
int sz = nums.size();
if(!sz) return ;
ans.resize(sz);
ans[] = nums[];
int maxSum = nums[];
for(int i = ; i < sz; ++i){
ans[i] = max(nums[i], ans[i - ] + nums[i]); //这里的条件应该注意
maxSum = max(ans[i], maxSum);
}
return maxSum;
}
};
LeetCode OJ:Maximum Subarray(子数组最大值)的更多相关文章
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 【js】Leetcode每日一题-子数组异或查询
[js]Leetcode每日一题-子数组异或查询 [题目描述] 有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]. 对于每个查询 i ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- C#解leetcode 53.Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 解题思路
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- Ionic 3 项目的工程目录结构(转载)
工程目录结构说明如下图
- Tkprof工具详解一(转载)
在数据库生成的oracle trace文件中,可读性是比较差的,此时可使用tkprof工具来格式化trace文件,tkprof是一个命令行工具,作用就是把原始的跟踪trace文件作为输入,然后格式化一 ...
- [笔记]Go语言写文件几种方式性能对比
Go语言中写文件有多种方式,这里进行如下几种方式的速度对比: 打开文件,写入内容,关闭文件.如此重复多次 打开文件,写入内容,defer 关闭文件.如此重复多次 打开文件,重复多次写入内容,defer ...
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- vue-cli的utils.js文件详解
转载自:http://www.cnblogs.com/ye-hcj/p/7078047.html utils.js文件 // 引入nodejs路径模块var path = require('path' ...
- Spring框架学习之IOC(一)
Spring框架学习之IOC(一) 先前粗浅地学过Spring框架,但当时忙于考试及后期实习未将其记录,于是趁着最近还有几天的空闲时间,将其稍微整理一下,以备后期查看. Spring相关知识 spri ...
- LigerUI LigerGird IE7/8 下不显示 “in”的操作数无效
LigerUI IE7/8 下显示 in的操作数无效 修改脚本生成LigerGrid的地方,将最后一列后面的,去掉
- .NET计时器的使用-Stopwatch类
作用: 微软提供的常用于统计时间消耗的类,作为一个固定的API接口供大家使用. 先看代码: using System; using System.Collections.Generic; using ...
- jQuery二级下拉菜单
在线演示 本地下载
- [算法]Plus One
Question: Given a non-negative number represented as an array of digits, plus one to the number. The ...