非负数组中找到和为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. xamarin.Android 实现横向滚动导航

    经过一段时间的练习,自己也做了不少的demo和一些小项目,今天就把这些demo分享给大家,也当做笔记记录到自己的博客中. 这次给大家带来的是HorizontalScrollView实现横向滑动,参考博 ...

  2. POJ3579 Median —— 二分

    题目链接:http://poj.org/problem?id=3579 Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  3. 第四届蓝桥杯C++B组省赛

    1.高斯日记 2.马虎的算式 3.第39级台阶 4.黄金连分数 5.前缀判断 6.三部排序 7.错误票据 8.翻硬币 9.带分数 10.连号区间数

  4. 修改fuse库成功

    使用的是fuse-2.9.2 在lib目录下的helper.c的fuse_main_real函数里打印一句话,然后将fuse库编译并install. 对ssfs进行编译,运行后,出现了打印的那句话! ...

  5. 四叉树 bnuoj

    点击打开题目链接 建树+广搜一棵树:最下面有更短代码(很巧妙). #include<iostream> #include<stdio.h> #include<queue& ...

  6. I.MX6 u-boot 2009 lvds hdmi lcd 补丁

    /************************************************************************* * I.MX6 u-boot 2009 lvds ...

  7. Struts2访问Servlet API的几种方式

    struts2提供了三种方式访问servlet API:大致分为两类 1. ActionContext:  public static ActionContext getContext() :获得当前 ...

  8. 【转】cocos2dx 内存管理机制

    原文地址: http://www.zaojiahua.com/memory-management.html cocos2dx采用的是在堆上分配内存空间,想想看你在写程序的时候对于cocos2dx中的类 ...

  9. 单选框 复选框 隐藏之后,绑定的change事件在ie中失效的问题

    有时候需要对单选框和复选框进行美化,就需要在<input type="radio">和<input type="checkbox">元素 ...

  10. hdu 3622 Bomb Game【二分+2-SAT+tarjan】

    用read()会挂 二分半径,显然最优的是所有原都用这个最小半径,然后2-SAT把相交的圆建图,跑tarjan判一下可行性即可 #include<iostream> #include< ...