[LintCode] Subarray Sum & Subarray Sum II
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.
Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].
用hashmap来存从nums[0]到nums[i]的和以及当前下标i,遍历数组求前缀和acc[i],如果发现acc[i]的值已经在hashmap中的,那么说明已经找到一段子数组和为0了。
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
map<int, int> has;
int sum = ;
has[] = -;
vector<int> res;
for (int i = ; i < nums.size(); ++i) {
sum += nums[i];
if (has.find(sum) != has.end()) {
res.push_back(has[sum] + );
res.push_back(i);
return res;
} else {
has[sum] = i;
}
}
return res;
}
};
Given an integer array, find a subarray where the sum of numbers is between two given interval. Your code should return the number of possible answer.
Given [1,2,3,4] and interval = [1,3], return 4. The possible answers are:
[0, 0]
[0, 1]
[1, 1]
[3, 3]
先求出前缀和,然后枚举求两个前缀和的差。如果差在start与end之间,就给res+1。注意前缀和数组前要插入一个0。
class Solution {
public:
/**
* @param A an integer array
* @param start an integer
* @param end an integer
* @return the number of possible answer
*/
int subarraySumII(vector<int>& A, int start, int end) {
// Write your code here
vector<int> acc(A);
acc.insert(acc.begin(), );
for (int i = ; i < acc.size(); ++i) {
acc[i] += acc[i-];
}
int tmp, res = ;
for (int i = ; i < acc.size() - ; ++i) {
for (int j = i + 1; j < acc.size(); ++j) {
tmp = acc[j] - acc[i];
if (tmp>= start && tmp <= end) ++res;
}
}
return res;
}
};
[LintCode] Subarray Sum & Subarray Sum II的更多相关文章
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- Zero Sum Subarray
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return th ...
- Longest subarray of target sum
2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- Path Sum,Path Sum II
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...
- LeetCode之“树”:Path Sum && Path Sum II
Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...
- LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解
文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...
- 关于数论分块里r=sum/(sum/l)的证明!
今天的模拟赛里T2要使用到数论分块,里面有一个重要的坎就是关于r=sum/(sum/l)的证明,网上关于这道题的题解里都没有关于这个的证明,那么我就来填补一下: 在以下的文章里,我都会使用lo(x)表 ...
随机推荐
- Oracle官方文档
Oracle DBA 10g 两日速成课程 http://www.oracle.com/webfolder/technetwork/cn/tutorials/obe/db/10g/r2/2day_db ...
- CDB和PDB的创建、连接、启动、关闭
一.CDB和PDB基本管理 基本概念: Multitenant Environment:多租户环境 CDB(Container Database):数据库容器 PD(Pluggable Databas ...
- KMS11激活Window系列
运行状态图 download: kms11
- C++中public、protected及private使用方法
1.类的一个特征就是封装,public和private作用就是实现这一目的.所以: 用户代码(类外)能够訪问public成员而不能訪问private成员:private成员仅仅能由类成员(类内)和友元 ...
- 生命周期方法调用,以及在onStop()方法中处理草稿信息
生命周期方法调用顺序 1. 从会话列表界面跳转到信息列表界面. 07-17 17:29:18.718: I/txrjsms(19370): MessageListActivity.onCreate 0 ...
- TxQueryRunner
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.SQLException; import org.apache. ...
- 【转】centos(原生yum系通用)安装xfce便捷方法
一个鸟人突然来了句他要在centos 5.2上装xfce,yum install xfce和yum install xfce4均没有合适的包(服务器没装x环境)于是我ssh登录到服务器上看了下,的确没 ...
- Swift3 - compare方法之ComparisonResult说明
Swift3在实现两个对象比较时,引入了compare方法,其中,方法返回值ComparisonResult解释如下: ComparisonResult是一个枚举类型,包含了以下3个成员: 其中: q ...
- wordpress搭建博客上传begin主题The themes is locked to another domain
如题, 在使用wordpress搭建个人博客过程中, 上传begin主题, 出现如下弹框的错误, 而且样式有误. 环境: Los Angeles CentOS 7 x64 nginx+mysql 解决 ...
- SaltStack入门到精通第一篇:安装SaltStack
SaltStack入门到精通第一篇:安装SaltStack 作者:纳米龙 发布日期:2014-06-09 17:50:36 实际环境的设定: 系统环境: centos6 或centos5 实验机 ...