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. django项目中使用KindEditor富文本编辑器。

    先从官网下载插件,放在static文件下 前端引入 <script type="text/javascript" src="/static/back/kindedi ...

  2. Jmeter连接MYSQL数据库,并进行数据库的操作;

    1.在操作jmeter连接mysql数据库之前,我们需要到网上下载一个jar包.并且添加到测试计划里:mysql-connector-java-5.1.13-bin.jar;把这个jar放到jmete ...

  3. 异常sql处理

    下面是在awr报告里面看到的有问题的sql,是9个变量的,在应用前台属于关联查询,在sqlplus里面手工执行检查实际执行情况如下: SELECT /*+ GATHER_PLAN_STATISTICS ...

  4. python Tkinter的Text组件中创建x轴和y轴滚动条

    #!/usr/bin/python #coding: utf-8 from Tkinter import * root = Tk() root.title("记事本") root. ...

  5. Atcoder Beginner Contest 138 简要题解

    D - Ki 题意:给一棵有根树,节点1为根,有$Q$次操作,每次操作将一个节点及其子树的所有节点的权值加上一个值,问最后每个节点的权值. 思路:dfs序再差分一下就行了. #include < ...

  6. 51nod1814 Clarke and string

    [传送门] 直接想最暴力的做法就是正解了.每次询问都把两个串的回文树建出来,然后再两棵树上同时dfs,经过相同的节点答案就加一.遇到一个不存在的就退出.再把询问记忆化一下就OK了.复杂度是 $O(n ...

  7. JS实现继承的几种方法

    父类: // 定义一个动物的类 function Animal (name) { // 属性 this.name = name || 'Animal'; // 实例方法 this.sleep = fu ...

  8. Linux后台开发工具箱-葵花宝典

    Linux后台开发工具箱-葵花宝典 一见 2016/11/4 目录 目录 1 1. 前言 4 2. 脚本类工具 4 2.1. 双引号和单引号 4 2.2. 取脚本完整文件路径 5 2.3. 环境变量和 ...

  9. MySQL8.0忘记密码后重置密码(亲测有效)

    实测,在mysql8系统下,用mysqld --console --skip-grant-tables --shared-memory可以无密码启动服务 服务启动后,以空密码登入系统 mysql.ex ...

  10. Shared Virtual Memory (SVM) Functions

    Description Shared Virtual Memory (SVM) (Glossary): An address space exposed to both the host and th ...