108th LeetCode Weekly Contest Binary Subarrays With Sum
In an array A of 0s and 1s, how many non-empty subarrays have sum S?
Example 1:
Input: A = [1,0,1,0,1], S = 2
Output: 4
Explanation:
The 4 subarrays are bolded below:
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
Note:
A.length <= 300000 <= S <= A.lengthA[i]is either0or1.
通用解法就是求连续数组的和有多少个,这种题代码都不会变的
把和存起来,给后面的数字-S看有没有这个和,有的话加起来,然后a[ans]++说明又存在符合条件的解了
class Solution {
public:
int numSubarraysWithSum(vector<int>& A, int S) {
long long ans = ;
long long k=;
map<long long,long long>a;
a[]=;
int len = A.size();
for(int i=;i<len;i++){
ans+=A[i];
if(ans>=S){
k+=a[ans-S];
}
a[ans]++;
}
return k;
}
};
108th LeetCode Weekly Contest Binary Subarrays With Sum的更多相关文章
- 108th LeetCode Weekly Contest Minimum Falling Path Sum
Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...
- 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...
- 108th LeetCode Weekly Contest Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode 930. Binary Subarrays With Sum
原题链接在这里:https://leetcode.com/problems/binary-subarrays-with-sum/ 题目: In an array A of 0s and 1s, how ...
- [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和
In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
随机推荐
- MySQL5.7插入中文乱码
参考: https://blog.csdn.net/kelay06/article/details/60870138 https://blog.csdn.net/itmr_liu/article/de ...
- Java生产者消费者问题
1. package interview.thread; import java.util.LinkedList; import java.util.Queue; import org.apache. ...
- p2150 [NOI2015]寿司晚宴
传送门 分析 我们发现对于大于$\sqrt(n)$的数每个数最多只会包含一个 所以我们把每个数按照大质数的大小从小到大排序 我们知道对于一种大质数只能被同一个人取 所以f1表示被A取,f2表示被B取 ...
- Luogu 4755 Beautiful Pair
分治 + 主席树. 设$solve(l, r)$表示当前处理到$[l, r]$区间的情况,我们可以找到$[l, r]$中最大的一个数的位置$mid$,然后扫一半区间计算一下这个区间的答案. 注意,这时 ...
- 防止SQL注入方法总结
一.参数化SQL 是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数 (Parameter) 来给值,用@来表示参数. 在使用参数化查询的情况下,数据库服务器不会将参数的内容视为 ...
- jquery数组拼接
var a=[]; var c=[80,90,70,100] var b={'张三':19,'成绩':c}; a.push(b); console.log("测试案例",a); 同 ...
- 自定义UINavigationBar的背景【转】
from:http://cocoa.venj.me/blog/custom-navbar-background/ 为了让我们的应用程序更加美观,我们往往希望对iPhone自带的控件进行一点自定义.比如 ...
- 【转】Android自定义控件(二)——有弹性的ScrollView
原文地址:http://blog.csdn.net/a105865708/article/details/17784041 实现了当手指滑动到ScrollView的顶部.底部时, 可以继续的向上.向下 ...
- C# ADO.NET+反射读取数据库并转换为List
public List<T> QueryByADO<T>(string connStr, string sql) where T : class, new() { using ...
- Springcloud踩坑记---使用feignclient远程调用服务404
公司项目进行微服务改造,由之前的dubbo改用SpringCloud,微服务之间通过FeignClient进行调用,今天在测试的时候,eureka注册中心有相应的服务,但feignclient就是无法 ...