LeetCode: Maximum Subarray
自己想得太复杂了,网上查了别人的代码。。太简单了。。
1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int sum = 0; 7 int ret = INT_MIN; 8 for(int i = 0 ; i < n; ++i) { 9 sum += A[i]; 10 ret = max(ret, sum); 11 sum = max(sum, 0); 12 } 13 return ret; 14 } 15 };
C#
1 public class Solution { 2 public int MaxSubArray(int[] nums) { 3 int sum = 0; 4 int ans = Int32.MinValue; 5 for (int i = 0; i < nums.Length; i++) { 6 sum += nums[i]; 7 ans = Math.Max(ans, sum); 8 sum = Math.Max(sum, 0); 9 } 10 return ans; 11 } 12 }
View Code
转载于:https://www.cnblogs.com/yingzhongwen/archive/2013/04/08/3007134.html
LeetCode: Maximum Subarray相关推荐
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode] Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode - Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【DP】LeetCode 53. Maximum Subarray
LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...
- 【动态规划】LeetCode 53. Maximum Subarray
LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...
- [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray
41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...
- LeetCode(53):Maximum Subarray
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
最新文章
- CVPR 2019收录论文ID公开,你上榜了吗?
- 求出歌手的得分python_哪位大侠帮我做做python的题目啊,做其中5个就好。跪求啊...
- 脚手架工程:介绍安装
- Blazor WebAssembly 应用程序中进行 HTTP 请求
- 嵌入式Linux系统编程学习之六make工程管理和Makefile
- Q85:对比“直接光照”和“间接光照”的反射模型
- 外包征集令:一个Android TV酒店项目
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_22-CMS前端页面查询开发-Api调用-跨域解决...
- IIS发布web网站
- word宏加密的破解
- python 创建nc文件,包含经纬度
- VMware虚拟机磁盘扩容
- typecho插件,typechoSEO插件,typecho程序插件
- stata陈强:计量经济学及stata应用_陈强 第五章 多元线性回归
- NGINX反向代理缓存
- 二进制和文本文件的比较
- Redis源码精炼版
- ThreadLocal.withInitial
- SAP概念之利润中心(Profit Center)
- rm: cannot remove `/usr/local/tmp/‘: Directory not empty