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】的更多相关文章

  1. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  2. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  3. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  4. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  5. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  6. 【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 ...

  7. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  8. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  9. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

随机推荐

  1. 一些爬虫中的snippet

    1.tornado 一个精简的异步爬虫(来自tornado的demo) #!/usr/bin/env python import time from datetime import timedelta ...

  2. C++ inline函数与编译器设置

    1. 经过测试#define与inline的速度几乎没有区别. 2. inline函数更为安全,有效避免了#define二义性问题.inline是真正的函数,而#define只是在字符串意义上的宏替换 ...

  3. Bootstrap下拉菜单

    前面的话 网页交互的时候经常会需要上下文菜单或者隐藏/显示菜单项,Bootstrap默认提供了用于显示链接列表的可切换.有上下文的菜单.而且在各种交互状态下的菜单展示需要和javascript插件配合 ...

  4. JAVAEE——SSH三大框架整合(spring+struts2+hibernate)

    一.整合原理 二.导包(41个) 1.hibernate (1)hibernate/lib/required (2)hibernate/lib/jpa | java persist api java的 ...

  5. App Extensions篇之Share Extension

    转载请注明出处:http://www.cnblogs.com/zhanggui/p/7119572.html 1.前言 这里主要是对App Extension的一些介绍以及详细给大家介绍一下Share ...

  6. Android原生跳转React不同页面(undefined is not an object)

    继续上篇文章的demo,由于现在的项目是原生的,打算用部分页面试下react native,那么问题来了:react貌似只有一个入口 index.android.js,那么在不同的原生页面需要跳转到不 ...

  7. 深入理解Java虚拟机-----------虚拟机类加载机制

    虚拟机类加载机制 类从被加载到虚拟机内存开始,到卸载出内存为止,整个生命周期包括:加载,验证,准备,解析,初始化,使用,卸载等7个阶段.其中,验证,准备,解析3个部分称为连接. 以上7个阶段中,加载, ...

  8. 动态数组ArrayList的使用

    1.定义类 package com.realhope.rmeal.bean; /** * * @author Wucy * 菜谱类 */ public class Menu{ private Inte ...

  9. springBoot基础系列--properties配置

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7183408.html SpringBoot中免除了大部分手动配置,但是对于一些特定的情况, ...

  10. 你应该知道的git高级技巧

    1.cherry-pick,把某个分支已经提交的commit放到另一个分支上 git cherry-pick <commit_id> 该命令可以将某个分支的提交记录合并到当前分支,如在ma ...