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.

 class Solution {
public:
int maxSubArray(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (>=n) return ; int b = ; int sum =A[]; for(int i=;i<n;i++){
if(b<)
b=; b = b+A[i]; if(b>sum)
sum =b;
}
return sum;
}
};

我的答案

思路:最大连续子数组的和,这是个非常经典的题目,答案也很简练,重点是代码中b这个变量的使用。

[leetcode.com]算法题目 - Maximum Subarray的更多相关文章

  1. LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

    Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...

  2. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

  3. LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

    原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...

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

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

  5. [LeetCode]题53:Maximum Subarray

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

  6. LeetCode(53) Maximum Subarray

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

  7. LeetCode之“动态规划”:Maximum Subarray

    题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...

  8. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. [LeetCode&Python] Problem 53. Maximum Subarray

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

随机推荐

  1. mybatis3.2初学感悟

    新手,学了mybatis框架一周,写点感悟下来. mybatis,是操作数据库,持久层的一个框架,它是对JDBC的封装.看到了这个框架,我突然感受到封装与抽象的力量.也明白了些为什么要分层的原因. 记 ...

  2. web.xml 详细介绍(zz)

    web.xml 详细介绍 博客分类: CoreJava WebXMLServletJSPTomcat  http://mianhuaman.iteye.com/blog/1105522 1.启动一个W ...

  3. Python 递归函数 详解

    Python 递归函数 详解   在函数内调用当前函数本身的函数就是递归函数   下面是一个递归函数的实例: 第一次接触递归函数的人,都会被它调用本身而搞得晕头转向,而且看上面的函数调用,得到的结果会 ...

  4. python学习 day09 (3月14日)----函数

    一.函数的进阶 1.1 动态参数 1.2* ** 1.3*args , **kwargs 1.4 函数的注释 1.5名称空间 1.6函数的嵌套全局变量 : 贴边写的局部变量 : 不是贴边写的. ''' ...

  5. python学习 day4 (3月5日)---列表

    列表: 容器性数据 有序  可更改 大量数据 一.增 1.追加    append(objcet) 2.索引增加   Insert(index,元素) 3.迭代追加   extend(object) ...

  6. 再读c++primer plus 001

    1. OOP强调的是在运行阶段(而不是编译阶段)进行决策,运行阶段指的是程序正在运行时,编译阶段指的是编译器将程序组合起来时. 2.变量的值都存储在栈中,而new从被称为堆或自由存储区的内存区域分配内 ...

  7. 使用Hadoop API 解压缩 HDFS文件

    接上篇:使用Hadoop API 压缩HDFS文件 压缩完了,当然需要解压缩了. 直接上代码: private static void getFile(String filePath) throws ...

  8. GHOST完成后出现GRUB解决方法

    1.试一下这个命令: grub> rootnoverify (hd0,0)(注意空格!!!) 或者 grub>makeacrive (hd0,0)grub> chainloader ...

  9. winform 可拖动无边框窗体解决办法

    方法一:通过重载消息处理实现. 鼠标的拖动只对窗体本身有效,不能在窗体上的控件区域点击拖动 /// <summary> /// 通过重载消息处理实现.重写窗口过程(WndProc),处理一 ...

  10. 用Kotlin写一个基于Spring Boot的RESTful服务

    Spring太复杂了,配置这个东西简直就是浪费生命.尤其在没有什么并发压力,随便搞一个RESTful服务 让整个业务跑起来先的情况下,更是么有必要纠结在一堆的XML配置上.显然这么想的人是很多的,于是 ...