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 ...
随机推荐
- php中json对象数据的输出转化
php中json对象数据的输出转化 public function get_my_now_citys(){ $datas=$this->_post('datas'); //前台js脚本传递给后端 ...
- 第三章(jQuery中的DOM操作)
3.1 DOM 操作分类 ①DOM Core 包括(getElementById() , getElementsByTagName() , getAttribute() , setAttribute( ...
- app 选项卡代码
<div id="segmented" class="mui-segmented-control"> <a class="mui-c ...
- Ion-affix & Ion-stick 仿IOS悬浮列表插件
Ion-affix & Ion-stick 仿IOS悬浮列表插件 Ion-affix 1.相关网页 Ion-affix 2.环境准备: 执行命令 bower install ion-affix ...
- C#工作笔记
没想到一个Java后端开发还要负责C#桌面程序,我感觉有点方.不过方归方,活还是要干的.简单记录下学到的一些知识点. 1.引用API函数 namespace Demo { class MyUtil { ...
- 第三章:3.3 post 请求
1. 在 from表单中将 属性 methtod="post‘ 改变成post 2. 访问主页地址:http://localhost:8000/index 3. 以上出现的错误. 查资料发现 ...
- java操作DBF的使用
1.引入javadbf.4.1.jar 2. import com.linuxense.javadbf.DBFField; import com.linuxense.javadbf.DBFRead ...
- H5读取本地文件操作
H5读取本地文件操作 本文转自:转:http://hushicai.com/2014/03/29/html5-du-qu-ben-di-wen-jian.html感谢大神分享. 常见的语言比如php. ...
- 编写高质量代码改善C#程序的157个建议:第17个建议之多数情况下使用foreach进行循环遍历
今天是我看<编写高质量代码:改善C#程序的157个建议>第二遍的时候了,看完这本书的确是受益匪浅,学到了很多东西,也明白了很多道理. 里面的代码我每个都调试了一遍,有时候是有些出入的,可能 ...
- PC-lint集成于SourceInsight 范例以及简单分析;提高代码的健壮性;
写代码之际突然想起了pc-lint这个"古董级"的代码静态分析工具; 下午机房的服务器歇菜了,没法调试游戏,刚好抽出时间来研究一下pc-lint集成在SourceInsight ...