【转载请注明】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 连续和倍数的更多相关文章

  1. [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 ...

  2. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  3. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  4. lintcode :continuous subarray sum 连续子数组之和

    题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...

  5. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  6. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  7. 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题是存储的当前累加和的 ...

  8. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  9. [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 ...

随机推荐

  1. J2EE 课件3 JSP标记

    •JSP标记包括指令标记.动作标记和自定义标记.其中自定义标记主要讲述与Tag文件有关的Tag标记    1.指令标记page page 指令用来定义整个JSP页面的一些属性和这些属性的值,属性值用单 ...

  2. Ubuntu 16.04安装PPA图形化管理工具Y PPA Manager

    安装: sudo add-apt-repository ppa:webupd8team/y-ppa-manager sudo apt-get update sudo apt-get install y ...

  3. Ubuntu中PPA源是什么

    以下内容转自https://imcn.me/ppa: PPA是Personal Package Archives首字母简写.翻译为中文意思是:个人软件包文档 只有Ubuntu用户可以用,而所有的PPA ...

  4. 源码分析-react2-根节点渲染

    //FiberNode{ alternate : '通过该属性和后面的切片进行比较', child : '改切片的子切片', firstEffect : '当前要加入的切片', stateNode : ...

  5. js滚轮换切屏

    因为全项目不是自己写的,仅仅是帮别人写js滚轮代码,并且别人项目也还未上线.所以仅仅贴出自己写的那段部分代码, 效果:鼠标滚轮滚动时.网頁屏幕一屏一屏的上下切换 (下面代码在本地电脑的IE,chrom ...

  6. about service in android

    注意:标红处! A Service is an application component representing either an application's desire to perform ...

  7. 四:Java之字符串操作String、StringBuffer和StringBuilder

    string是我们经经常使用到的一个类型,事实上有时候认为敲代码就是在重复的操作字符串,这是C的特点,在java中.jdk非常好的封装了关于字符串的操作.三个类String .StringBuffer ...

  8. linux 网络编程-基础篇01

    #Socket简介 是一个编程接口是一种特殊的文件描述符(everything in Unix is a file)并不仅限于TCPIP协议面向连接(Transmission Control Prot ...

  9. 2.EF的数据审计日志

    转载:采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2) 数据审计日志: 先说一下这个审计的概念,就是对所有的实体的操作(增,删,改) ...

  10. C# 数据库访问

    C# 数据库访问 分类: C#学习笔记2011-07-05 11:26 515人阅读 评论(0) 收藏 举报 数据库c#datasettextboxcommandexception   目录(?)[+ ...