LeetCode 最大子序和 以你之姓@ 2023-06-04 14:56 1阅读 0赞 题目链接:[https://leetcode-cn.com/problems/maximum-subarray/][https_leetcode-cn.com_problems_maximum-subarray] ### 题目大意: ### 略。 ### 分析: ### 略 ### 代码如下: ### ![ContractedBlock.gif][] ![ExpandedBlockStart.gif][] 1 class Solution { 2 public: 3 int maxSubArray(vector<int>& nums) { 4 int ans = nums[0]; 5 int ret = 0; 6 7 for(int i = 0; i < nums.size(); ++i) { 8 ret += nums[i]; 9 ans = max(ans, ret); 10 if(ret <= 0) ret = 0; 11 } 12 13 return ans; 14 } 15 }; 转载于:https://www.cnblogs.com/zaq19970105/p/11528332.html [https_leetcode-cn.com_problems_maximum-subarray]: https://leetcode-cn.com/problems/maximum-subarray/ [ContractedBlock.gif]: https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif [ExpandedBlockStart.gif]: /images/20230601/0e688762d30b4470b6d9859c99ff75c6.png
还没有评论,来说两句吧...