【转载请注明】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. java包静态导入,继承

    /** * 静态导入 */package cn.sxt.gao;import cn.sxt.oo.*;//用别的包的类需要导入别的包,表示该包下所有类全部导入import java.util.Date ...

  2. hp 1810-24g switch reset

    Specific steps to execute the factory default reset on the switch are: 1. Using a small, thin tool w ...

  3. 金典 SQL笔记 SQL语句汇总

    SELECT * FROM (SELECT ROW_NUMBER() OVER ( ORDER BY id) AS rowN,FNumber, FName,FSalary ,Fage FROM dbo ...

  4. 数据分析在web交互设计中的作用 页面跳出率 100% 原因分析

    通过分析访问的路径,发现,访问者访问其他页面,直接跳出 页面跳出率  100% 说明: 连作者都发现的导航路径不清晰 对导航进行改版:清晰.明了

  5. MapReduce02

    ====================== MapReduce实现 ====================== Input: 一系列key/value对 用户提供两个函数实现: map(k,v) ...

  6. 通过adb push 从电脑里复制文件到手机里

    在开发中.我们 常常 须要 从 电脑 拷贝 一些 文件 到  自己的 app 目录里. 我之前的 方式 是 将 手机 链接 电脑,然后 从 电脑里 找到 app 文件夹,然后 进行 拷贝. 可是 这种 ...

  7. ios15--综合小例子

    // // XMGViewController.m,控制器类 #import "XMGViewController.h" #import "XMGShop.h" ...

  8. Linux gadget驱动分析2------设备识别过程

    设备连上主机之后,设备驱动做了的事. 设备连上host的port之后,主机端会有一套策略发送请求获取device的一系列描述符.进行枚举过程.找到适合该device的驱动. 这样就可以与device进 ...

  9. 在Docker Hub上你可以很轻松下载到大量已经容器化的应用镜像,即拉即用——daocloud国内镜像加速

    Docker之所以这么吸引人,除了它的新颖的技术外,围绕官方Registry(Docker Hub)的生态圈也是相当吸引人眼球的地方. 在Docker Hub上你可以很轻松下载到大量已经容器化的应用镜 ...

  10. @注解与普通web.xml的关系

    @WebServlet(name = "SimpleServlet" ,urlPatterns = {"/simple"}) public class Simp ...