给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置
样例: 给出 [-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 int a; vector<int> b; for ( int i=0;i<nums.size();i++) { a=nums[i]; if(a==0){b.push_back(i);b.push_back(i);break; } for(int j=i+1;j<nums.size();j++) { a=a+nums[j]; if(a==0){b.push_back(i); b.push_back(j); break; } } if(a==0)break; } return b; } }; 感想: