当前位置:网站首页>Topic36——53. 最大子数组和
Topic36——53. 最大子数组和
2022-06-25 22:10:00 【_卷心菜_】
题目:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组 是数组中的一个连续部分。
示例 1:
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
示例 2:
输入:nums = [1]
输出:1
示例 3:
输入:nums = [5,4,-1,7,8]
输出:23
提示:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
动态规划:
class Solution {
public int maxSubArray(int[] nums) {
int max = nums[0];
int pre = 0;
for(int j = 0; j < nums.length; j++) {
pre = Math.max(nums[j], nums[j] + pre);
max = Math.max(max, pre);
}
return max;
}
}
边栏推荐
- STL教程5-STL基本概念及String和vector使用
- 对伪类的理解
- mongodb
- Let's talk about string today
- 关于Swoole协程容器
- 51 single chip microcomputer, some registers, some knowledge points
- 对象数组去重
- 谈一谈生产环境中swoole协程创建数量控制机制
- Two ways to center block level elements
- Alipay payment interface sandbox environment test and integration into an SSM e-commerce project
猜你喜欢
随机推荐
js实现输入开始时间和结束时间,输出其中包含多少个季,并且把对应年月打印出来
数组常用的一些操作方法
SSL/TLS、对称加密和非对称加密和TLSv1.3
The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
平衡二叉树AVL
mysql版本升级+数据迁移
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
Can I upload pictures without deploying the server?
php中使用Makefile编译protobuf协议文件
Using Google protobuf protocol environment configuration in PHP
Understanding of pseudo classes
支付宝支付接口沙箱环境测试以及整合到一个ssm电商项目中
InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
proxy
如何配置SQL Server 2008管理器_过路老熊_新浪博客
我的博客今天2岁167天了,我领取了先锋博主徽章_过路老熊_新浪博客
在win10下使用visual studio2015链接mysql数据库
Today's 61 Fu
Doris 运维中遇到的问题
今天说说String相关知识点









