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 ...
随机推荐
- spark streaming(2) DAG静态定义及DStream,DStreamGraph
DAG 中文名有向无环图.它不是spark独有技术.它是一种编程思想 ,甚至于hadoop阵营里也有运用DAG的技术,比如Tez,Oozie.有意思的是,Tez是从MapReduce的基础上深化而来的 ...
- Ionic/Angularjs 知识点解析
Ionic/Angularjs 知识点解析 angular-ui-router(状态跳转) state的定义:(在app.js的config下配置) $stateProvider .state('ap ...
- nodejs模块学习: connect解析
nodejs模块学习: connect解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的轮子来解决 ...
- webpack以及loader 加载命令
module.exports={ entry:'./main/main.js', output:{ path:'./build', filename:'bundle.js' }, module:{ l ...
- Hibernate使用注解进行ORM映射实例
在上一篇博客中,我们通过xml配置文件进行实体类和表的映射,但是近两年来有更多的项目对一些比较稳定的实体类使用了注解进行ORM映射,这样使得编程更加简洁.简单.其实使用注解进行ORM映射和使用xml进 ...
- spring +springmvc+mybatis组合mybatis-config.xml文件配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC &q ...
- c++ 命名空间 以及 作用域 函数参数 面向对象实验报告
面向对象的分析与设计 实验报告一 一.变量的储存类别 auto static register extern auto变量 函数中的局部变量,如不专门声明为static存储类别,都是动态地分配存 ...
- Java xml 解析
1. XML框架结构 Java SE 6 平台提供的 XML 处理主要包括两个功能:XML 处理(JAXP,Java Architecture XML Processing)和 XML 绑定(JAXB ...
- HelloGitHub.com 网站开源了
简介 随着 HelloGitHub 月刊持续更新了一年多,内容变的越来越多.因为内容数据没有结构化,如果还是使用之前的编辑文本的方式编辑月刊内容,会对后面的继续发刊和维护带来了很多问题和多余的工作,例 ...
- 腾讯云centos7服务器环境搭建,tomcat+jdk+mysql+nginx
软件:jdk 1.8.0_45 tomcat 8.5.8 mysql 5.6.36 nginx 1.10.x或以上 其中tomcat在centos6.8中没问题,centos7中会出现卡在启动那里 I ...