非负数组中找到和为K的倍数的连续子数组

详见:https://leetcode.com/problems/continuous-subarray-sum/description/

Java实现:

方法一:

class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
for(int i=0;i<nums.length;++i){
int sum=nums[i];
for(int j=i+1;j<nums.length;++j){
sum+=nums[j];
if(sum==k){
return true;
}
if(k!=0&&sum%k==0){
return true;
}
}
}
return false;
}
}

方法二:

class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
if(nums==null){
return false;
}
HashSet<Integer> sums=new HashSet<>();
int sum=0;
for(int i=0;i<nums.length;++i){
sum+=nums[i];
if((k!=0&&sums.contains(sum%k))||(i!=0&&sum==k)){
return true;
}else if(k!=0){
sums.add(sum%k);
}
}
return false;
}
}

方法三:用HashMap保存sum对k取余数,如果前序有余数也为sum%k的位置,那么就存在连续子数组和为k的倍数。

class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer,Integer> map=new HashMap<Integer,Integer>(){{put(0,-1);}};
int sum=0;
for(int i=0;i<nums.length;++i){
sum+=nums[i];
if(k!=0){
sum%=k;
}
Integer prev=map.get(sum);
if(prev!=null){
if(i-prev>1){
return true;
}
}else{
map.put(sum,i);
}
}
return false;
}
}

C++:

方法一:

class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k)
{
for (int i = 0; i < nums.size(); ++i)
{
int sum = nums[i];
for (int j = i + 1; j < nums.size(); ++j)
{
sum += nums[j];
if (sum == k)
{
return true;
}
if (k != 0 && sum % k == 0)
{
return true;
}
}
}
return false;
}
};

方法二:

class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k)
{
int n = nums.size(), sum = 0, pre = 0;
unordered_set<int> st;
for (int i = 0; i < n; ++i)
{
sum += nums[i];
int t = (k == 0) ? sum : (sum % k);
if (st.count(t))
{
return true;
}
st.insert(pre);
pre = t;
}
return false;
}
};

参考:http://www.cnblogs.com/grandyang/p/6504158.html

523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组的更多相关文章

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

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

  3. 523. Continuous Subarray Sum是否有连续和是某数的几倍

    [抄题]: Given a list of non-negative numbers and a target integer k, write a function to check if the ...

  4. 523. Continuous Subarray Sum

    class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...

  5. 【leetcode】523. Continuous Subarray Sum

    题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...

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

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

  7. LintCode 402: Continuous Subarray Sum

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

  8. [LintCode] Continuous Subarray Sum II

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

  9. Continuous Subarray Sum II(LintCode)

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

随机推荐

  1. 20170223 遇到自建表里面相同key值数据不唯一

     我怎么发现这个表里 key值相同数据不唯一, 这两条看起来是完全相同的, 其实排序不能能合并已经说明问题.

  2. 解析SQL中的包含的列和表

    using System; using System.IO; using System.Collections.Generic; namespace SQLProcess { class Progra ...

  3. PrintWrite

    向文本输出流打印对象的格式化表示形式.此类实现在 PrintStream 中的所有 print 方法.它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入. 与 Print ...

  4. spring中PropertyPlaceholderHelper替换占位符的值

    1.Properties中的值替换¥{}或者#{}占位符 String text = "foo=${foo},bar=${bar}"; Properties props = new ...

  5. HDU3533 Escape —— BFS / A*算法 + 预处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 Escape Time Limit: 20000/10000 MS (Java/Others)  ...

  6. html5--6-56 阶段练习5-翻转效果

    html5--6-56 阶段练习5-翻转效果 学习要点 运用所学过的知识完成一个简单的小练习,理解对动画的应用. @charset="UTF-8"; *{ ; ; } img{ w ...

  7. 一步一步学Silverlight 2系列(11):数据绑定

    概念 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  8. Tomcat 系统架构与设计模式之一

    Tomcat 系统架构与设计模式,第 1 部分: 工作原理 来自:http://www.ibm.com/developerworks/cn/java/j-lo-tomcat1/index.html 这 ...

  9. iOS中键盘的收起

    在UIViewController中收起键盘,除了调用相应控件的resignFirstResponder方法之外,还有另外三种方法: 重载UIViewController中的touchesBegin方 ...

  10. Makefile的引入及规则

    ARM裸机1期加强版视频课程配套WiKi第9课第5节_Makefile的引入及规则. 文字不能完全替代视频,所以如果你看了这些文章不太懂,建议购买视频进一步学习. 视频购买地址:100ask.taob ...