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】 35. 序言:分享文件
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 打开vmvare出现The VMware Authorization Service is not running。
win+r再输入cmd打开doc,输入services.msc打开服务,吧Vmware Authorization Service 更改为自动或者将其启动即可.
- java excel导出
下面是jsp代码: <li class="btns"><input id="btnExport" class="btn btn-pr ...
- MySQL(二)--事务与视图
一.事务 1.提交 2.回滚 3.ACID特性 二.视图 1.创建视图 2.删除视图 3.更新视图 4.使用视图 三.子查询 1. 使用子查询 2. 标量子查询 3. 关联子查询 一.事务 在 RDB ...
- Your password does not satisfy the current policy requirements
创建用户,做测试想设置一个简单的密码.报错: 大概是MySQL5.7搞事情,默认安装了validate_password插件. mysql> SHOW VARIABLES LIKE 'valid ...
- Linux 下挂在ntfs 硬盘
CentOS 7 下想要挂载NTFS的文件系统该怎么办呢? 我们需要一个NTFS-3G工具,并编译它之后在mount就可以了,就这么简单. 首先要进入官网下载NTFS-3G工具 http://www. ...
- Unity3D-Shader-人物残影效果
[旧博客转移 - 2016年1月7日 00:24 ] 前面的话 上一篇讲了一下人物边缘发光效果,链接: Unity-ShaderLab-实现X光效果,这次我们利用这个Shader来实现人物残影效果 先 ...
- 原生js数组
forEach()遍历:在原来数组上进行操作 var arrF = [2,3,4]; var arrS = arrF.forEach(function (value,index,a) { //val ...
- bootstrap栅栏系统css中的col-xs-*、col-sm-*、col-md-* 的意义以及 bootstrap一个标签中,同时有 col-xs , col-sm , col-md , col-lg的理解
摘要: bootstrap栅栏系统css中的col-xs-*.col-sm-*.col-md-* 的意义: .col-xs- 超小屏幕 手机 (<768px) .col-sm- 小屏幕 平板 ( ...
- 入职15天,Angular2 小记!
ng 配置@ngModule({ imports: [ BrowserModule ], //导入模块 declarations: [ AppComponent ], //导入组件 providers ...