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.

挑选子串中最大的值,自己写了一堆代码最后缺少负数情况的考虑,

  1. public class Solution {
  2. public int maxSubArray(int[] nums) {
  3. //第一次找一个数组 记录所有大于0的数字的位置
  4. //结果=第一个非负数
  5. //逐次循环加到下一个非负数,如果比当前结果大,则替换当前结果
  6. int resMax=nums[0];
  7. int res =nums[0] ;
  8. int len = nums.length;
  9. int vaNums[] = new int [len];
  10. int j=0;
  11. // Map<Integer,Integer> map =new HaspMap<Integer,Integer>();
  12. int m=0;
  13. int plusNums[] = new int [len];
  14. for(int i=0;i<len;i++){
  15. if(nums[i]>0){
  16. vaNums[j]=i;
  17. // map.put(i,nums[i]);
  18. j++;
  19. }
  20. else{
  21. plusNums[m]=i;
  22. m++;
  23. }
  24. res+=nums[i];
  25. }
  26. if(j>0){
  27.  
  28. for(int k=0;k<j;k++){
  29. res =0;
  30. for(int l =vaNums[k];l<=vaNums[j-1];l++){
  31. res+=nums[l];
  32. if(resMax<res){
  33. resMax=res;
  34. }
  35. }
  36.  
  37. }
  38. }//if j >0 end
  39. else {
  40. for(int k=0;k<m;k++){
  41. res =nums[0];
  42. for(int l =vaNums[k];l<plusNums[m-1];l++){
  43. res+=nums[l];
  44. if(resMax<res){
  45. resMax=res;
  46. }
  47. }
  48.  
  49. }
  50. }
  51.  
  52. return resMax;
  53. }
  54. }

最佳办法,感觉是真滴牛逼

  1. public class Solution {
  2. public int maxSubArray(int[] nums) {
  3. //第一次找一个数组 记录所有大于0的数字的位置
  4. //结果=第一个非负数
  5. //逐次循环加到下一个非负数,如果比当前结果大,则替换当前结果
  6. int sum=0,max=Integer.MIN_VALUE;
  7. int len =nums.length;
  8. for(int i=0;i<len;i++){
  9. sum +=nums[i];
  10. max =Math.max(sum,max);
  11. sum = Math.max(0,sum);
  12. }
  13. //方法二
  14.  
  15. /*
  16. int sum=0,max=Integer.MIN_VALUE,minSum = 0;
  17. for (int i = 0; i < nums.length; i++) {
  18. sum += nums[i];
  19. max = Math.max(max, sum - minSum);
  20. minSum = Math.min(minSum, sum);
  21. }
  22. */
  23.  
  24. return max;
  25. }
  26. }

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. [leetcode-561-Array Partition I]

    Given an array of 2n integers, your task is to group these integers into n pairs of integer,say (a1, ...

  2. 使用Scribefire在博客中插入语法高亮 II

    效果如下, 这是我们在Scribefire中添加的code按钮,单击此按钮,则会出现 在codeHere中直接输入代码就可以了. 查看html 可以看到,其中已经添加了<pre>标签. 下 ...

  3. JavaScript 的注释和快捷键

    添加必要的注释,对一个有责任心.有道德模范的前端必须具备的好习惯, 可以大大提高代码的可维护性.可读性. java代码注释快捷键:ctrl+shift+/首先熟悉一下html.css.js的注释的写法 ...

  4. Linux下mysql远程连接问题

    [旧博客转移 - 发布于2015年9月6日 18:42]   在Linux上装好Mysql后 发现远程客户端连接不上 出现这种情况是因为mysql服务器出于安全考虑,默认只允许本地登录数据库服务器. ...

  5. JavaWeb 后端 <二> 之 Servlet 学习笔记

    一.Servlet概述 1.什么是Servlet Servlet是一个运行在服务器端的Java小程序,通过HTTP协议用于接收来自客户端请求,并发出响应. 2.Servlet中的方法 public v ...

  6. Go学习笔记(一)Let's 干

    加 Golang学习 QQ群共同学习进步成家立业 ^-^ 群号:96933959 简介     Go是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言.为了方便搜索和识 ...

  7. ansible的安装

    安装 ansible-server的安装 client需要有python2.5以上 的python server和client都关闭了selinux server端: 网址: http://www.a ...

  8. 微信小程序选项卡功能

    首先看看微信小程序上的选项卡的效果 原理呢,就是先布局好(这就不必说了吧),然后在上面的每一个选项卡上都定义一个同样的点击事件,然后给每一个组件上绑定一个唯一的标识符,然后点击事件触发的时候,获取到绑 ...

  9. Android源码博文集锦2

    Android精选源码 android简单易用的Gallery android漂亮的加载效果 这可能是RxJava 2.x 最好的入门教程示例代码 android图片可拖拽排序 android用几行代 ...

  10. 虚拟机配置静态IP地址

    使用VMware配置虚拟机静态IP地址 一.安装好虚拟后在菜单栏选择编辑→ 虚拟网络编辑器,打开虚拟网络编辑器对话框,选择Vmnet8 Net网络连接方式,随意设置子网IP,点击NAT设置页面,查看子 ...