【转载请注明】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. nyoj 19 擅长排列的小明(深搜,next_permutation)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  2. Ubuntu16.04再次装机记

    个人的吐槽 整个国庆假期(9天),基本上都在搭建PHP环境中耗费了. 是这样的——来大学报到前,想着大学里用个Dev-C++应该够了,于是把Ubuntu删了,腾出了100G的空间.到了CSU,加了团委 ...

  3. [K/3Cloud]关于"选单"操作

    之前有些人对这块有些疑问,比如: 1.选单操作是否和下推基本一样,都是公用同一套单据转换规则,只不过下推是源单推目标单,选单是目标单去选择源单,最终操作结果一样? 2,我想实现选单的时候,选单列表先通 ...

  4. jquery ajax获取json并解析,获取的json是object对象格式

    首先我们使用的是ajax方式,推荐一个学习网址: http://blog.csdn.net/shiyaru1314/article/details/51065410 这个博主写的特别好.现在来看我们的 ...

  5. 布局(codevs 1242)

    题目描述 Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们 ...

  6. Ubuntu安装vnc 解决乱码

    https://blog.csdn.net/dddxxxx/article/details/53580789 https://www.centos.bz/2017/12/%E8%A7%A3%E5%86 ...

  7. S - Arc of Dream 矩阵快速幂

    An Arc of Dream is a curve defined by following function: where a 0 = A0 a i = a i-1*AX+AY b 0 = B0  ...

  8. 高数(A)下 第十章

    10.1 10.2 10.3 10.4 10.5 10.7 自测题

  9. 18、Java并发性和多线程-饥饿与公平

    以下内容转自http://ifeve.com/starvation-and-fairness/: 如果一个线程因为CPU时间全部被其他线程抢走而得不到CPU运行时间,这种状态被称之为“饥饿”.而该线程 ...

  10. 1. MaxCounters 计数器 Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.

    package com.code; import java.util.Arrays; public class Test04_4 { public static int[] solution(int ...