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.

分析:这道题和152题有点类似。是求连续的子数组的和的最大值。是一道动态规划的题目。

代码如下:

public class Solution {
public int MaxSubArray(int[] nums) {
int max=int.MinValue,sum=int.MinValue; for(int i=;i<nums.Length;i++)
{
if(sum>)
sum+=nums[i];
else
sum=nums[i]; max=sum>max?sum:max;
} return max;
}
}

C#解leetcode 53.Maximum Subarray的更多相关文章

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

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

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

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

  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 (子数组的最大和) 解题思路方法

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

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

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

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

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

随机推荐

  1. Day11 Memcached、Redis和RabbitMQ安装

    修改Ubuntu镜像源: sudo vi /etc/apt/sources.list 全部替换为以下内容: ---------------------------------------------- ...

  2. sys.argv[]用法

    #-*- coding: utf-8 -*- """ sys.argv 用来获取命令行参数 sys.argv[0] 表示当前执行文件 "-k".sta ...

  3. nopcommerce 二次开发

    http://www.cnblogs.com/nopcommerce-b2c/ http://www.nopchina.net/ 数据库结构 http://www.xcode.me/open/docu ...

  4. 转:六百字读懂Git

    原文来自于:http://www.techug.com/git-in-600-words 译注:来自 Hacker School 的 Mary Rose Cook 最近实现了一个纯 JavaScrip ...

  5. python 单元测试

    http://blog.csdn.net/five3/article/details/7104466

  6. 4. Repeater 实例2

    设计管理一个用户程序,对用户的状态进行管理,当用户状态是启用时整行显示红色. 设计思路:用Repeater遍历每行记录,在操作状态的表格中旋转两个按钮,一个为启用功能,另一个为禁用功能,根据Repea ...

  7. 【ZOJ】3430 Detect the Virus

    动态建树MLE.模仿别人的代码模板各种原因wa后,终于AC. #include <iostream> #include <cstdio> #include <cstrin ...

  8. POJ-Common Substrings(后缀数组-长度不小于 k 的公共子串的个数)

    题意: 长度不小于 k 的公共子串的个数 分析: 基本思路是计算 A 的所有后缀和 B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于 k 的部分全部加起来. 先将两个字符串连起来,中间 ...

  9. Mac下Shell快捷键

    ctrl+a //移到行首 ctrl+e //移到行尾 ctrl+y // 插入最近删除的单词或语句 ctrl+k //删除光标处到行尾部分 ctrl+u //删除光标处到行首部分 ctrl+w // ...

  10. kafka log4j配置

    kafka日志文件分为5种类型,依次为:controller,kafka-request,server,state-change,log-cleaner,不同类型log数据,写到不同文件中: 区别于c ...