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. pid 控制

    static std::map<pid_t, TTask *> Tasks; TError TTask::Fork(bool detach) { PORTO_ASSERT(!PostFor ...

  2. MySQL定义char和varchar类型,utf8编码,则最大值为多少?

    有道面试题:若一张表中只有一个字段VARCHAR(N)类型,utf8编码,则N最大值为多少?先明白计算的一些规则限制 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字时,只 ...

  3. Bean property ‘mapperHelper’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

    spring boot 报错: Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property ...

  4. vim 常用命令(记录)

    很好的vim讲解:https://blog.csdn.net/weixin_37657720/article/details/80645991 命令模式:默认模式.输入ctrl+c, 输入:,转换为命 ...

  5. file size php

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. 如何使用h5py读入数据

    在网上找到的方法,如下: import h5py import numpy as np #HDF5的写入: imgData = np.zeros((2,4)) f = h5py.File('HDF5_ ...

  7. python面试题&练习题之函数

    1.写函数,接收两个数字参数,返回最大值例如:传入:10,20返回:20 def res_max(number1,number2): l1 = [] l1.append(number1) l1.app ...

  8. NSString+URLParser NSScanner

    @implementation NSString (URLParser) -(NSDictionary*)parserToDict { NSScanner *scanner = [NSScanner ...

  9. Java Polymorphism

    Ability of an organism to take different shapes is polymorphism in bio world. A simplest definition ...

  10. Hibernate的事务

    1.数据库的封锁(https://www.cnblogs.com/zhai1997/p/11710082.html): 封锁是实现并发控制的重要技术. read uncommitted : 读取尚未提 ...