53. Maximum Subarray【leetcode】
53. Maximum Subarray【leetcode】
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
.
挑选子串中最大的值,自己写了一堆代码最后缺少负数情况的考虑,
public class Solution {
public int maxSubArray(int[] nums) {
//第一次找一个数组 记录所有大于0的数字的位置
//结果=第一个非负数
//逐次循环加到下一个非负数,如果比当前结果大,则替换当前结果
int resMax=nums[0];
int res =nums[0] ;
int len = nums.length;
int vaNums[] = new int [len];
int j=0;
// Map<Integer,Integer> map =new HaspMap<Integer,Integer>();
int m=0;
int plusNums[] = new int [len];
for(int i=0;i<len;i++){
if(nums[i]>0){
vaNums[j]=i;
// map.put(i,nums[i]);
j++;
}
else{
plusNums[m]=i;
m++;
}
res+=nums[i];
}
if(j>0){ for(int k=0;k<j;k++){
res =0;
for(int l =vaNums[k];l<=vaNums[j-1];l++){
res+=nums[l];
if(resMax<res){
resMax=res;
}
} }
}//if j >0 end
else {
for(int k=0;k<m;k++){
res =nums[0];
for(int l =vaNums[k];l<plusNums[m-1];l++){
res+=nums[l];
if(resMax<res){
resMax=res;
}
} }
} return resMax;
}
}
最佳办法,感觉是真滴牛逼
public class Solution {
public int maxSubArray(int[] nums) {
//第一次找一个数组 记录所有大于0的数字的位置
//结果=第一个非负数
//逐次循环加到下一个非负数,如果比当前结果大,则替换当前结果
int sum=0,max=Integer.MIN_VALUE;
int len =nums.length;
for(int i=0;i<len;i++){
sum +=nums[i];
max =Math.max(sum,max);
sum = Math.max(0,sum);
}
//方法二 /*
int sum=0,max=Integer.MIN_VALUE,minSum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
max = Math.max(max, sum - minSum);
minSum = Math.min(minSum, sum);
}
*/ return max;
}
}
53. Maximum Subarray【leetcode】的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- [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之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
随机推荐
- 【Android Developers Training】 85. 不要有冗余的下载
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 23. 序言:保存数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Gist - ES6 Promise
The concept of "Promise" Promise is used to asynchronous computations. Introduction " ...
- python实现简单的循环购物车小功能
python实现简单的循环购物车小功能 # -*- coding: utf-8 -*- __author__ = 'hujianli' shopping = [ ("iphone6s&quo ...
- MyEclipse2014web工程项目直接复制不能访问报错处理方案
在学习web servlet项目中,做一个项目时 , 完成到了某一阶段 实现了部分功能,有必要保存这一项目,当复制这个项目之后发现发布会报错或者不能访问. 其实复制项目主要是为了在以后的学习中如果能顺 ...
- JavaWeb 后端 <五> 之 JSP 学习笔记
一.JSP简介 1.也是SUN公司推出的开发动态web资源的技术,属于JavaEE技术之一.由于原理上是Servlet, 所以JSP/Servlet在一起. 二.HTML.Servlet和JSP 1. ...
- 基于Http协议订阅发布系统设计
基于Http协议订阅发布系统设计 --物联网系统架构设计 1,订阅发布(subscriber-publisher) 订阅发布模式最典型的应用场景就是消息系统的设计.在消息系统的架构中 ...
- Kindeditor编辑插件的使用
1.首先kindeditor这个插件需要配合着asp.net的自生带的控件textbox来实现 2.首先前台界面代码 <f:FormRow runat="server"> ...
- js中的数组排序
js数组冒泡排序,快速排序的原理以及实现 冒泡排序: 随便从数组中拿一位数和后一位比较,如果是想从小到大排序,那么就把小的那一位放到前面,大的放在后面,简单来说就是交换它们的位置,如此反复的交换位 ...
- docker~windows版本的安装与使用
回到目录 在面向服务的框架里,docker扮演着十分重要的角色,他使你的部署更轻量,使运维更智能化,事实上微软自己的项目也已经用上了docker了,下面介绍一下在windows环境上使用docker的 ...