leetcode 53 最大自序列和
简介
暴力只要变量两遍, 挺好的, 不过更好的应该是动态规划. 应该是最简单的动态规划了吧
code
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max_num = nums[0];
for(int i=0; i<nums.size(); i++){
int sum = nums[i];
max_num = max(max_num, sum);
for(int j=i+1; j<nums.size(); j++){
sum += nums[j];
max_num = max(max_num, sum);
}
}
return max_num;
}
};
class Solution {
public int maxSubArray(int[] nums) {
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(nums[0]);
for(int i=1; i<nums.length; i++){
a.add(Math.max((Integer)a.get(i-1) + nums[i], nums[i]));
}
int max = -100000000;
for(Integer i: a){
max = Math.max(i, max);
}
return max;
}
}
leetcode 53 最大自序列和的更多相关文章
- [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 ...
- leetcode 最长连续序列 longest consecutive sequence
转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- leetcode 53 最大子序列之和(动态规划)
思路:nums为给定的数组,动态规划: 设 一维数组:dp[i] 表示 以第i个元素为结尾的一段最大子序和. 1)若dp[i-1]小于0,则dp[i]加上前面的任意长度的序列和都会小于nums[i], ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode 946. 验证栈序列(Validate Stack Sequences) 26
946. 验证栈序列 946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct va ...
- LeetCode 53. 最大子序和(Maximum Subarray)
53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...
- [LeetCode]364. 加权嵌套序列和 II (DFS)
题目 给一个嵌套整数序列,请你返回每个数字在序列中的加权和,它们的权重由它们的深度决定. 序列中的每一个元素要么是一个整数,要么是一个序列(这个序列中的每个元素也同样是整数或序列). 与 前一个问题 ...
- leetcode || 53、Maximum Subarray
problem: Find the contiguous subarray within an array (containing at least one number) which has the ...
随机推荐
- eolinker校验规则之正则匹配:返回结果校验的方法和案例(正则校验)
如上图红色箭头,需要校验返回值内是否包含"创建满足条件的优惠券"这一内容 如果需要满足以上校验,最好的方法就是使用正则进行匹配 切换到正则匹配,输入需要校验的内容,即可实现正则匹配 ...
- Java 里的对象在虚拟机里面是怎么存储的?
Java 中的对象在虚拟机里的存储 在 Java 中,对象在虚拟机中的存储方式取决于 JVM 内存模型,主要存储在 堆(Heap) 中.对象的内存布局和管理方式会影响对象的创建.访问和销毁.下面详细解 ...
- ubuntu安装clamav
直接apt安装 sudo apt-get update sudo apt-get install clamav clamav-daemon clamscan --version 停止服务更新病毒库 为 ...
- <HarmonyOS第一课13>给应用添加通知和提醒 #鸿蒙课程##鸿蒙生态#
课程介绍 <HarmonyOS第一课:给应用添加通知和提醒>将引导开发者如何在HarmonyOS应用中实现通知功能.课程首先介绍如何为您的应用添加基础类型通知,包括普通文本.多行文本和图片 ...
- 【工具】Zotero|使用Zotero向Word/WPS中插入引用文献(2023年)
版本:Word 2021,Zotero 6.0.30 版本:WPS 教育版,HUST 前言:两年前我找网上插入文献的方式,网上的博客提示让我去官网下个插件然后才能装,非常麻烦,导致我对Zotero都产 ...
- 为什么重写equals一定也要重写hashCode方法?
简要回答 这个是针对set和map这类使用hash值的对象来说的 只重写equals方法,不重写hashCode方法: 有这样一个场景有两个Person对象,可是如果没有重写hashCode方法只重写 ...
- 操作系统综合题之“银行家算法,计算各资源总数和Need还需要数量和解释什么是安全状态以及银行家进阶题(额外提出资源请求计算是否满足)”
一.问题:某系统在某时刻的进程和资源状态如下表所示: 进程 Allocation(已分配资源数) (A B C D) Max(最大需要资源数) (A B C D) Avaliable(可用资源数) ( ...
- Json工具类之"Json格式转换总结"
一.HashMap转Json HashMap<String, String> oldContentMap = new HashMap<>(); oldContentMap.pu ...
- python 读写、创建文件
python中对文件.文件夹(文件操作函数)的操作设计到os模块以及shutil模块 os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作,比如:绝对路径,父路径等 ...
- codeup之分数序列求和
Description 有如下分数序列 求出次数列的前20项之和. 请将结果的数据类型定义为double类型. Input 无 Output 小数点后保留6位小数,末尾输出换行. Sample Inp ...