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 ...
随机推荐
- [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, ...
- 使用Scribefire在博客中插入语法高亮 II
效果如下, 这是我们在Scribefire中添加的code按钮,单击此按钮,则会出现 在codeHere中直接输入代码就可以了. 查看html 可以看到,其中已经添加了<pre>标签. 下 ...
- JavaScript 的注释和快捷键
添加必要的注释,对一个有责任心.有道德模范的前端必须具备的好习惯, 可以大大提高代码的可维护性.可读性. java代码注释快捷键:ctrl+shift+/首先熟悉一下html.css.js的注释的写法 ...
- Linux下mysql远程连接问题
[旧博客转移 - 发布于2015年9月6日 18:42] 在Linux上装好Mysql后 发现远程客户端连接不上 出现这种情况是因为mysql服务器出于安全考虑,默认只允许本地登录数据库服务器. ...
- JavaWeb 后端 <二> 之 Servlet 学习笔记
一.Servlet概述 1.什么是Servlet Servlet是一个运行在服务器端的Java小程序,通过HTTP协议用于接收来自客户端请求,并发出响应. 2.Servlet中的方法 public v ...
- Go学习笔记(一)Let's 干
加 Golang学习 QQ群共同学习进步成家立业 ^-^ 群号:96933959 简介 Go是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言.为了方便搜索和识 ...
- ansible的安装
安装 ansible-server的安装 client需要有python2.5以上 的python server和client都关闭了selinux server端: 网址: http://www.a ...
- 微信小程序选项卡功能
首先看看微信小程序上的选项卡的效果 原理呢,就是先布局好(这就不必说了吧),然后在上面的每一个选项卡上都定义一个同样的点击事件,然后给每一个组件上绑定一个唯一的标识符,然后点击事件触发的时候,获取到绑 ...
- Android源码博文集锦2
Android精选源码 android简单易用的Gallery android漂亮的加载效果 这可能是RxJava 2.x 最好的入门教程示例代码 android图片可拖拽排序 android用几行代 ...
- 虚拟机配置静态IP地址
使用VMware配置虚拟机静态IP地址 一.安装好虚拟后在菜单栏选择编辑→ 虚拟网络编辑器,打开虚拟网络编辑器对话框,选择Vmnet8 Net网络连接方式,随意设置子网IP,点击NAT设置页面,查看子 ...