Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-,,-,,-,,,-,],
Output:
Explanation: [,-,,] has the largest sum = .

翻译:

给定一个整数组,找到相加之和最大的子数组,返回最大的和

思路1:

暴力循环不解释

1.循环遍历一个值的和,找出最大的那个

2.循环遍历两个相邻数的和,找出最大的那个

3.循环遍历三个相邻数的和,找出最大的那个

...

最后比较所有的最大和,得到最最大的那个

代码

class Solution {
public int maxSubArray(int[] nums) {
int sum = nums[0];
int length = nums.length;
for(int i = 1;i<=length;i++){
int once = getOnce(nums,i);
if(once>sum){
sum = once;
}
}
return sum;
} public int getOnce(int[] nums,int n){
int max = nums[0];
for(int i = 0;i<=(nums.length-n);i++){
int add = 0;
int nn = n;
while(--nn >= 0){
add+=nums[i+nn];
}
if(add > max){
max = add;
}
}
return max;
}
}

跑起来没问题,但是提交后报错了!Time Limit Exceeded,就是运行超时了,因为循环了3遍,时间复杂度是N的3次方啊,所以抛弃

百度得到解决办法

思路2:

原数组  [-2,1,-3,4,-1,2,1,-5,4]

设置初始最大和为第一个数-2,从第二个数开始遍历

这里有一个思维技巧,就是只循环一遍,得到每个以当前值结尾的数组的最大值,一开始我没想明白这一块,后来多想几遍也就明白了

代码:

class Solution {
public int maxSubArray(int[] nums) {
int sum = nums[0];
int max = nums[0]; for (int i = 1; i < nums.length; i++) {
System.out.print("当前值:"+nums[i]+"--当前值+前面的最大和:"+(sum + nums[i]));
sum = Math.max(nums[i], sum + nums[i]);
System.out.println("当前的最大和:"+sum);
max = Math.max(max, sum);
}
return max;
}
}

为何要这样比较呢,因为对于当前值来说,有两种可能

1.和前面的数组抱团

2.自己单干

如果自己单干比抱团还厉害,当然自己单干咯

欢迎关注我的微信公众号:安卓圈

【LeetCode算法-53】Maximum Subarray的更多相关文章

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

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

  2. Leetcode之53. Maximum Subarray Easy

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

  3. 【算法】LeetCode算法题-Maximum Subarray

    这是悦乐书的第154次更新,第156篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第13题(顺位题号是53).给定一个整数数组nums,找出一个最大和,此和是由数组中索引 ...

  4. 【LeetCode】53. Maximum Subarray (2 solutions)

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  5. LeetCode OJ 53. Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. 【一天一道LeetCode】#53. Maximum Subarray

    一天一道LeetCode系列 (一)题目 Find the contiguous subarray within an array (containing at least one number) w ...

  7. Leetcode No.53 Maximum Subarray(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums, find the contiguous subarray (containing at least one nu ...

  8. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...

  9. [leetcode DP]53. Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. 【Leetcode】53. Maximum Subarray

    题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...

随机推荐

  1. 在ubuntu系统中,遇到 “由于/bin 不在PATH 环境变量中,故无法找到该命令”问题

    2018年01月07日 11:27:34 YangJianShuai 阅读数 3024更多 分类专栏: linux 好多命令的位置在/usr/bin.恢复办法如下:1. /usr/bin/sudo v ...

  2. Nuxt 知识点

    脚手架工具 create-nuxt-app 创建项目: $ npx create-nuxt-app <项目名> 启动项目: To get started: cd nuxt_demo npm ...

  3. 推荐系统(recommender systems):预测电影评分--构造推荐系统的一种方法:低秩矩阵分解(low rank matrix factorization)

    如上图中的predicted ratings矩阵可以分解成X与ΘT的乘积,这个叫做低秩矩阵分解. 我们先学习出product的特征参数向量,在实际应用中这些学习出来的参数向量可能比较难以理解,也很难可 ...

  4. 开源项目(7)Opencv日常之Homography

    参考教程 https://blog.csdn.net/liuphahaha/article/details/50719275 什么是Homography 在图1中有两张书的平面图,两张图分别有四个相对 ...

  5. UFUN 函数 UF_UI UF_DISP函数( UF_UI_select_with_class_dialog 、UF_DISP_set_highlight)

    //设置class_dialog选择过滤 static int init_proc(UF_UI_selection_p_t select,void* user_data) { //过滤类别的个数 ; ...

  6. 用于C# 的异步,持久的键值存储 Akavache 使用

    Akavache是​​一个异步的,持久的(即写入磁盘)键值存储,用于在C#中编写桌面和移动应用程序,基于SQLite3.Akavache非常适合存储重要数据(即用户设置)以及过期的缓存本地数据. 开源 ...

  7. CSPS_114

    考前自闭赛 综合我100场血的教训,我的考试策略应该是: 1.不要期望能AC某道题,想都不要想,否则很容易直接崩 2.哪怕想到正解,先打暴力,把暴力码出来!没用也码! 稳扎稳打地得到代码难度最小的下一 ...

  8. 【学习笔记】fwt&&fmt&&子集卷积

    前言:yyb神仙的博客 FWT 基本思路:将多项式变成点值表达,点值相乘之后再逆变换回来得到特定形式的卷积: 多项式的次数界都为\(2^n\)的形式,\(A_0\)定义为前一半多项式(下标二进制第一位 ...

  9. request登录案例

    一.案例需求 1.编写login.html登录页面 username & password 两个输入框 2.使用Druid数据库连接池技术,操作mysql,day14数据库中user表 3.使 ...

  10. 洛谷P3258 [JLOI]2014松鼠的新家

    题目 树上差分 树上点差分,注意会出现路径端点多记录的情况,这时需要在最后输出的时候输出子树的差分数组的和-1,而不是在处理原数据的时候减1.并且a[n]不需要糖果,最后也减去就行. #include ...