Leetcode 53. Maximum SubarrayJAVA语言
1
2
3
|
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.
|
题意:求连续子数组的最大和
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class Solution {
public int maxSubArray( int [] nums) {
int len=nums.length;
if (nums== null || len== 0 ) return 0 ;
int MAX=nums[ 0 ];
int curSum=nums[ 0 ];
for ( int i= 1 ;i<len;i++){
if (curSum> 0 ){
curSum+=nums[i];
} else {
curSum=nums[i];
}
MAX=Math.max(curSum,MAX);
}
return MAX;
}
}
|
PS:第一种思路,逐渐累加循环。
还有一种思路,可以用动态规划做。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class Solution {
public int maxSubArray( int [] nums) {
int len=nums.length;
if (nums== null || len== 0 ) return 0 ;
//dp[i]代表以nums[i]为结尾的最大和。
int [] dp= new int [len];
dp[ 0 ]=nums[ 0 ];
int MAX=dp[ 0 ];
for ( int i= 1 ;i<len;i++){
if (dp[i- 1 ]< 0 ){
dp[i]=nums[i];
} else {
dp[i]=dp[i- 1 ]+nums[i];
}
MAX=Math.max(dp[i],MAX);
}
return MAX;
}
}
|
Leetcode 53. Maximum SubarrayJAVA语言相关推荐
- 【DP】LeetCode 53. Maximum Subarray
LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...
- 【动态规划】LeetCode 53. Maximum Subarray
LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...
- [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray
41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...
- LeetCode 53. Maximum Subarray--动态规划--C++,Python解法
题目地址:Maximum Subarray - LeetCode Given an integer array nums, find the contiguous subarray (containi ...
- LeetCode(53):Maximum Subarray
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode]: 53: Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- leetCode 53. maximum subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode#53 Maximum Subarray
Problem Difinition: Find the contiguous subarray within an array (containing at least one number) wh ...
- C#解leetcode 53.Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
最新文章
- document.all
- optaplanner_OptaPlanner –具有真实道路距离的车辆路线
- qdir 类似工具_qdir 类似工具_支持 Win8.1,全能资源管理器 Q-Dir 5.74 发布
- 千兆网线8根线定义图_网线水晶头如何制作及怎么测试?
- [脚本收集]提取Tripntale图片
- 【渝粤教育】电大中专成本会计作业 题库
- 树莓派使用usb摄像头
- 使用腾讯云服务器搭建自己网站应该怎么做
- android组合按键截屏,如果你的安卓手机支持〔电源〕键+〔音量减〕键截屏,你可以代码...
- 天才小毒妃 第943章 小东西很努力
- 对比工具winMerge
- 一位博士在华为的 22 年
- 利用身份证号码算年龄 并排序
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- sourceTree使用教程
- DotNetTextBox V3.0 所见即所得编辑器控件Ver3.2.4 Free(免费版)
- netty-Android-RN等博文收藏
- Unity学习笔记(实现传送带)
- HTB walkthrough -- Admirer
- 迅雷下载的资源,文件夹有大小,但是打开没有文件
热门文章
- 苹果6发布时间_iPhone12promax11月6日几点预售 11.6苹果12mini预售时间
- 剑指offer面试题[14]-调整数组顺序使奇数位于偶数前面
- 理解这几个安全漏洞,你也能做安全测试【干货建议收藏】
- Python操作数据库完成接口测试
- linux mysql 实战_Linux平台MySQL多实例项目实施_MySQL数据库基础与项目实战06
- 你的手机浏览器不支持webgle_不支持n79频段5G手机不能买?OPPO Reno3全频覆盖消除后顾之忧...
- undo the local changes
- papers to read
- Hierarchical deformation of Locally Rigid Meshes
- Lesson 2 Gradient Desent