您的位置:首页 > 其它

子数组之和为零 lintcode

2016-04-14 22:13 204 查看
给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置。

样例

给出
[-3,
1, 2, -3, 4]
,返回
[0, 2]
或者
[1,
3]
.
class Solution {
public:
/**
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number
*          and the index of the last number
*/
vector<int> subarraySum(vector<int> nums){
// write your code here
vector<int> result;
int n = nums.size();
int sum = 0;
for(int i = 0;i<n;i++){
for(int j = i;j<n;j++)
{

sum += nums[j];
if(sum == 0)
{
result.push_back(i);
result.push_back(j);
return result;
}

}
}
//return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: