考察:最大连续字段和问题。

解决问题时间复杂度:O(n)

问题隐含条件:如果给出的数集都是负数,那么最大连续字段和就是,最大的那个负数。

eg:{-2,-1}  结果应该输出 -1 而不是 0

int maxSubArray(int* nums, int numsSize) {
int maxSum = 0; //维护最大连续字段和
int currentMaxSum = 0;//当前最大和
int nextNum = 0;
int singleSum = nums[0]; //存在全是负数,则singleSum 代表最大的那个
int j = 0;
for (int i = 0; i < numsSize; i ++) {
nextNum = nums[i];
currentMaxSum += nextNum;
if (currentMaxSum > 0) {
maxSum = maxSum <= currentMaxSum ? currentMaxSum: maxSum;
} else {
currentMaxSum = 0;
j ++;
singleSum = singleSum < nextNum ? nextNum : singleSum;
}
}
maxSum = (j == numsSize) ? singleSum : maxSum;
return maxSum;
}

LeetCode 53. Maximum Subarray 最大连续字段和问题的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  3. 41. leetcode 53. Maximum Subarray

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

  4. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  5. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  6. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  7. LeetCode 53. Maximum Subarray(最大的子数组)

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

  8. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

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

  9. [LeetCode] 53. Maximum Subarray 最大子数组

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

随机推荐

  1. android 开发积累

    1.ListView滚动黑屏问题 ListView滚动时,数据项变成黑色 问题解决办法:通过添加 android:cacheColorHint = "#00000000" 将背景设 ...

  2. Windwos下连远程linux Hbase小问题

    前几天,兴起想仔细玩玩hbase,细细去研究一下,写了个小demo,从win7去连接另一台T510的ubuntu上的hbase.很简单的crud的操作程序,没有看出来什么问题,但是跑起来,硬是好像bl ...

  3. 第一百六十三节,jQuery,基础核心

    jQuery,基础核心 一.代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号“$”来起 始的.而这个“$”就是jQuery当中最重要且独有的对象:jQuery对象,所 ...

  4. 转:Python的这几个技巧,简直屌爆了

    经使用Python编程有多年了,即使今天我仍然惊奇于这种语言所能让代码表现出的整洁和对DRY编程原则的适用.这些年来的经历让我学到了很多的小技巧和知识,大多数是通过阅读很流行的开源软件,如Django ...

  5. 几个比较经典的算法问题的java实现

    1.八皇后问题 public class EightQueen { private static final int ROW = 16; private static final int COL = ...

  6. (转)ThreadLocal

    转自:http://blog.csdn.net/lufeng20/article/details/24314381 Thread同步机制的比较 ThreadLocal和线程同步机制相比有什么优势呢?T ...

  7. JDK动态代理具体解释

    首先说一下动态代理和静态代理的差别: 静态代理:是预先写好或由特定工具自己主动生成的代码.再对其编译.在程序执行前.代理类的.class文件就已经存在了. 动态代理:代理是在程序执行时,运用反射机制动 ...

  8. 桥梁模式(Bridge)

    桥梁模式属于结构类的设计模式,示意结构图如下:

  9. JZOJ.5264【NOIP2017模拟8.12】化学

    Description

  10. 8782:乘积最大(划分dp)

    8782:乘积最大   同洛谷 P1018 乘积最大 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我 ...