子数组之和
来源:互联网 发布:全国书法艺术网络大赛 编辑:程序博客网 时间:2024/05/17 10:03
问题描述:
样例:
给出
解题思路:
用循环控制只要和为零就记录始终位置,并且跳出循环.
代码:
感想:
给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置
样例:
给出
[-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; }};
感想:
阅读全文
0 0
- 子数组之和
- 最大子数组之和
- 最大子数组之和
- 最大子数组之和
- 子数组之和
- 子数组之和
- 题目:子数组之和
- 题目:子数组之和
- 子数组之和
- (138)子数组之和
- LintCode_子数组之和
- 子数组之和_LintCode
- 子数组之和
- 子数组之和
- LintCode:子数组之和
- lintcode ----子数组之和
- 子数组之和
- 子数组之和
- 快乐树
- Linux——Linux下的tar压缩解压缩命令详解
- bzoj 3142: [Hnoi2013]数列 数学
- 每天一个小算法之 选择排序算法
- 51nod 1185 威佐夫游戏 V2 (博弈+大数乘法模拟)
- 子数组之和
- Android开发学习——显式intent和隐式intent(1)
- sql中limit和count的含义
- 生成网站和发布网站有什么差别
- 计算机的 二 八 十 十六 进制
- ActivityMQ集成到springmvc中
- OSPF动态路由协议的配置命令
- 树莓派开启I2C功能
- sql语句中count的用法