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. firefox 之 event兼容写法

    event 在 IE 和 FF(Firefox) 中是不兼容的,IE 中可以直接使用 event 对象,而 FF 中则不可以,解决方法之一如下: var event = window.event || ...

  2. Ubuntu上搭建Hadoop环境(单机模式+伪分布模式) (转载)

    Hadoop在处理海量数据分析方面具有独天优势.今天花了在自己的Linux上搭建了伪分布模式,期间经历很多曲折,现在将经验总结如下. 首先,了解Hadoop的三种安装模式: 1. 单机模式. 单机模式 ...

  3. keras框架的MLP手写数字识别MNIST,梳理?

    keras框架的MLP手写数字识别MNIST 代码: # coding: utf-8 # In[1]: import numpy as np import pandas as pd from kera ...

  4. HTML 内 meta标签

    <!-- 是否删除默认的苹果工具栏和菜单栏 --> <meta name="apple-mobile-web-app-capable" content=" ...

  5. shell 报错 /bin/bash^M: bad interpreter: No such file or directory

    shell 执行报错: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory 原因:window和linux 的文件格式 ...

  6. latex字体

    强调 方式:声明:\em 或者 命令\emph,后者是latex2e的命令 区别:声明与命令的作用范围不同:\em改变当前字体直到被其他相应的声明取消(也可以是\em本身),或者当前的环境结束为止,当 ...

  7. s4-7 生成树协议

    Spanning Tree :为了可靠,采用冗余结构:但是透明网桥 会产生无休止循环的问题 冗余交换拓扑可能带来的问题  广播风暴  多帧传送  MAC地址库不稳定 生成树协议  STP:sp ...

  8. mysql学习之路_外键

    回顾4 连接查询: 连接多张表到一起,不管记录数如何,字段数一定会增加. 分类:内连接,外连接.自然连接,交叉连接, 交叉连接:cross join (笛卡尔积) 内连接:inner join,左右两 ...

  9. ubuntu-server-12.04.2安装配置jdk

    原文链接:http://blog.csdn.net/amymengfan/article/details/9958461 我选择的是离线安装,这需要先下载好jdk安装包(下载地址:http://www ...

  10. P750 内存插槽

    查看p750内存插槽占用情况 lscfg -vp | grep -p DIMM Memory DIMM: Record Name.................VINI Flag Field.... ...