python_lintcode_简单题 _44最小子数组_41最大子数组
2017-08-15 21:58
375 查看
44最小子数组
题目
给定一个整数数组,找到一个具有最小和的子数组。返回其最小和。注意事项
子数组最少包含一个数字
样例
给出数组[1, -1, -2, 1],返回 -3
思路
这道题是要在原来的整数数组中按数组顺序找到满最小和的数组。例如[1, -2,1,-1, -2, 1]–>[-2,1,-1, -2]=-4代码
class Solution: """ @param nums: a list of integers @return: A integer denote the sum of minimum subarray """ def minSubArray(self, nums): # write your code here x1=nums[0] x2=nums[0] for i in nums[1:]: x1=min(i,x1+i) x2=min(x1,x2) return x2
41最大子数组
题目
给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。注意事项
子数组最少包含一个数
样例
给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6
代码
class Solution: """ @param nums: A list of integers @return: An integer denote the sum of maximum subarray """ def maxSubArray(self, nums): # write your code here x1=nums[0] x2=nums[0] for i in nums[1:]: x1=max(i,i+x1) x2=max(x1,x2) return x2
相关文章推荐
- LintCode【简单】41. 最大子数组 。代码及思路
- LintCode-41. 最大子数组
- lintcode 41.最大子数组
- 【LintCode 简单】44. 最小子数组
- LintCode之44 最小子数组
- python_lintcode_698数组中最大的差值_3. 统计数字_12. 带最小值操作的栈
- Lintcode 41.最大子数组
- 【LintCode 简单】41. 最大子数组
- LintCode 44 最小子数组
- LintCode. 44 最小子数组
- LintCode【简单】44. 最小子数组 。代码及思路
- 宏的简单应用(得出数组中的最大值)
- lintcode 刷题-两数组的交 II Python
- LintCode【简单】50. 数组剔除元素后的乘积 。代码及思路
- lintcode---43. 最大子数组 III
- 『Python CoolBook』C扩展库_其三_简单数组操作
- LintCode【简单】100. 删除排序数组中的重复数字。代码及思路
- Python数组遍历的简单实现方法小结
- python如何将数组分成几个区间,取每个区间的最大值存到另一个数组里
- 41. 最大子数组