原题

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

Example 1:

Input: [23, 2, 4, 6, 7],  k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.

Example 2:

Input: [23, 2, 6, 4, 7],  k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.

Note:
The length of the array won’t exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer.

代码分析

DP问题应用连续区域,判断是否等于指定值的倍数。

代码实现

public class Solution {private int[] _nums;private int _len;public bool CheckSubarraySum(int[] nums, int k) {_nums = nums;_len = nums.Length;calculator();for(int i=0; i<_len; i++)for( int j=i+1; j<_len;j++){if(k==0) //取余时一定注意,k不能为0!{if(rangeSum(i,j)==0)return true;}else {if(rangeSum(i,j)%k==0)return true;}}return false;}private void calculator(){for(int i=1;i<_len;i++)_nums[i]+= _nums[i-1];}private int rangeSum(int i, int j){if(i==0)return _nums[j];return _nums[j] - _nums[i-1];}
}

leetcode-solution库

leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

LeetCode-Easy部分中标签为Dynamic Programming 523. Continuous Subarray Sum相关推荐

  1. LeetCode 523. Continuous Subarray Sum

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

  2. 【leetcode】523. Continuous Subarray Sum

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

  3. leetcode 523. Continuous Subarray Sum | 523. 连续的子数组和(同余定理)

    题目 https://leetcode.com/problems/continuous-subarray-sum/ 题解 没有想到 O(n) 的方法,于是直奔答案: 参考1:[宫水三叶]拓展到求方案数 ...

  4. LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)

    581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...

  5. leetcode 5465. 子树中标签相同的节点数

    代码能力越来越菜了...... 题目链接 这题本来就是一个普普通通的DFS,但我却掉进了坑了,一直在超时,后面看了题解以后才恍然大悟. 我一开始的想法是,dp[i][k]表示第i个节点为根的情况下,含 ...

  6. LeetCode Subarray Sum Equals K

    原题链接在这里:https://leetcode.com/problems/subarray-sum-equals-k/description/ 题目: Given an array of integ ...

  7. LeetCode 动态规划(Dynamic programming)系列题目--C++,Python解法

    LeetCode上有许多态规划(Dynamic programming)的题目,我在这里整合一下 本文章不再更新,请看LeetCode 所有题目总结 LeetCode 所有题目总结:LeetCode ...

  8. leetcode周赛5465. 子树中标签相同的节点数

    leetcode周赛5465. 子树中标签相同的节点数 给你一棵树(即,一个连通的无环无向图),这棵树由编号从 0 到 n - 1 的 n 个节点组成,且恰好有 n - 1 条 edges .树的根节 ...

  9. 【leetcode-Python】-Dynamic Programming -309. Best Time to Buy and Sell Stock with Cooldown

    题目链接 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目描述 给定价格数组prices, ...

最新文章

  1. Ubuntu安装Nginx+PHP+Memcache+Mysql指导手册
  2. 深渊水妖 模拟,贪心 牛客白月赛44
  3. 足球 Floyd算法
  4. POJ——字符串插入
  5. python 为什么只执行一次_python只执行一次
  6. Linux:yum配置和使用
  7. scala属性/成员变量
  8. tp cli模式产生日志导致web环境写入不了
  9. 工作日志模板_生产运行部操作日志“电子化”
  10. GridView中设置DataFormatString无效的解决方法.
  11. 软硬磁盘和磁盘驱动器是微型计算机的外存储设备,可实现对信息的,21. 软硬磁盘和磁盘驱动器是微型计算机的外存储设备,它是实现对信息的...
  12. Java开发的发展前景和未来规划方向
  13. 回溯算法求解桥本分数式
  14. 【推理加速】博客翻译:利用融合conv和bn的方法加速模型
  15. 计算机色彩知识调研(一):色域
  16. 2022年最新谷歌商店上架_Google Play 上架全流程附图
  17. java根据指定字符开头_Java如何检查以特定单词开头的字符串?
  18. [设计模式] 模板方法模式与多线程结合实现数据的批处理
  19. java word中插入图片_在Word文档中插入图片
  20. 网络打印服务器存打印文件,网络打印机文件打印顺序问题

热门文章

  1. 福大计算机在职研究生,福州大学在职研究生上课时间介绍
  2. Vue3官网-高级指南(十七)响应式计算`computed`和侦听`watchEffect`(onTrack、onTrigger、onInvalidate、副作用的刷新时机、`watch` 、pre)
  3. 游戏音乐制作中要注意的五大问题
  4. pandas使用心得(一)pandas初体验之处理excel(上)
  5. 编译原理|递归下降分析子程序
  6. 【TouchDesigner】声音可视化呈现
  7. int最大值表示方法
  8. 未来计算机dl攻城略地进程
  9. 一个非常正确且强大的编程学习方法
  10. 看狂神视频学习JVM笔记