Leetcode0523--Continuous Subarray Sum 连续和倍数
【转载请注明】https://www.cnblogs.com/igoslly/p/9341666.html
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int size = nums.size();
for(int i=0;i<size-1;i++){
int sum=nums[i];
for(int j=i+1;j<size;j++){
sum +=nums[j];
// 解决k=0 问题,判断是否整除
if(k==0 && sum==0) return true;
if(k!=0 && sum%k==0) return true;
}
}
return false;
}
};
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int sum=;
map<int,int> sumhash;
for(int i=;i<nums.size();i++){
sum+=nums[i];
// judge if k=0
if(k== && i<nums.size()-){
if((nums[i]+nums[i+])==) return true;
}
// have previous sum%k
if(k!= && sumhash.find(sum%k)!=sumhash.end()) return true;
if(i!= && k!= && sum%k==) return true;
if(k!=) sumhash[sum%k]++;
}
return false;
}
};
int t = (k == ) ? sum : (sum % k);
if (m.count(t)) {
if (i - m[t] > ) return true;
} else m[t] = i;
Leetcode0523--Continuous Subarray Sum 连续和倍数的更多相关文章
- [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- [Swift]LeetCode523. 连续的子数组和 | Continuous Subarray Sum
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
随机推荐
- 常州模拟赛d8t1 友好数对
分析:其实就是问你有多少对a,b有且仅有两位不相同.我们可以先枚举这两位,对应ai枚举一位,对应bi枚举一位,如果ai^(1<<x) == bi^(1<<y),证明恰好有两位不 ...
- poj 3074
题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...
- 我不喜欢的 Rust 特性 (之一) eager drop
struct Foo; impl Drop for Foo { fn drop(&mut self) { println!("drop"); } } fn main() { ...
- 安全简单解决MVC 提示 检测到有潜在危险的 Request.Form 值.
一般使用富文本编辑器的时候.提交的表单中包含HTML字符,就会出现此错误提示. 使用 ValidateInput(false) 特性标签并不能解决此问题. 网上前篇一律的回答是修改Web.Config ...
- nginx: 添加文件下载目录
修改nginx.conf,添加如下行: location /file/ { alias /usr/share/nginx/html/file/; add_header Content-di ...
- 【翻译自mos文章】rman 标准版和企业版的兼容性
rman 标准版和企业版的兼容性 来源于: RMAN Standard and Enterprise Edition Compatibility (文档 ID 730193.1) 适用于: Oracl ...
- 开发汉澳sinox64位,对接汉澳矩阵电脑
汉澳矩阵电脑刚刚不久前提出,即使全然设计出了汉澳矩阵电脑线路图,要在上面执行操作系统必须有矩阵操作系统. 汉澳sinox64位操作系统并非矩阵操作系统,仅仅是更为接近. 汉澳矩阵电脑假设插有16个矩阵 ...
- leetcode 217 Contains Duplicate 数组中是否有反复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- 20170623_oracle_SQL
============SQL分类 数据定义语言(DDL):CREATE ALERT DROP TRUNCATE 数据操纵语言(DML):INSERT UPDATE DELETE SELECT 事务控 ...
- Ubuntu 16.04 安装CodeBlocks
首先将软件源添加进来,就是运行以下命令 sudo add-apt-repository ppa:damien-moore/codeblocks-stable sudo apt-get update 完 ...