[LeetCode] Continuous Subarray Sum 连续的子数组之和
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.
这道题给了我们一个数组和一个数字k,让我们求是否存在这样的一个连续的子数组,该子数组的数组之和可以整除k。遇到除法问题,我们肯定不能忘了除数为0的情况等处理。还有就是我们如何能快速的遍历所有的子数组,并且求和,我们肯定不能完全的暴力破解,这样OJ肯定不答应。我们需要适当的优化,如果是刷题老司机的话,遇到这种求子数组或者子矩阵之和的题,应该不难想到要建立累加和数组或者累加和矩阵来做。没错,这道题也得这么做,我们要遍历所有的子数组,然后利用累加和来快速求和。在得到每个子数组之和时,我们先和k比较,如果相同直接返回true,否则再判断,若k不为0,且sum能整除k,同样返回true,最后遍历结束返回false,参见代码如下:
解法一:
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
for (int i = ; i < nums.size(); ++i) {
int sum = nums[i];
for (int j = i + ; j < nums.size(); ++j) {
sum += nums[j];
if (sum == k) return true;
if (k != && sum % k == ) return true;
}
}
return false;
}
};
下面这种方法用了些技巧,那就是,若数字a和b分别除以数字c,若得到的余数相同,那么(a-b)必定能够整除c。这里就不证明了,博主也不会证明。明白了这条定理,那么我们用一个集合set来保存所有出现过的余数,如果当前的累加和除以k得到的余数在set中已经存在了,那么说明之前必定有一段子数组和可以整除k。需要注意的是k为0的情况,由于无法取余,我们就把当前累加和放入set中。还有就是题目要求子数组至少需要两个数字,那么我们需要一个变量pre来记录之前的和,我们每次存入set中的是pre,而不是当前的累积和,参见代码如下:
解法二:
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size(), sum = , pre = ;
unordered_set<int> st;
for (int i = ; i < n; ++i) {
sum += nums[i];
int t = (k == ) ? sum : (sum % k);
if (st.count(t)) return true;
st.insert(pre);
pre = t;
}
return false;
}
};
既然set可以做,一般来说用哈希表也可以做,这里我们建立余数和当前位置之间的映射,由于有了位置信息,我们就不需要pre变量了,之前用保存的坐标和当前位置i比较判断就可以了,参见代码如下:
解法三:
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size(), sum = ;
unordered_map<int, int> m{{,-}};
for (int i = ; i < n; ++i) {
sum += nums[i];
int t = (k == ) ? sum : (sum % k);
if (m.count(t)) {
if (i - m[t] > ) return true;
} else m[t] = i;
}
return false;
}
};
参考资料:
https://discuss.leetcode.com/topic/80975/java-solution
https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space/2
https://discuss.leetcode.com/topic/80892/concise-c-solution-use-set-instead-of-map
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Continuous Subarray Sum 连续的子数组之和的更多相关文章
- [LeetCode] 560. Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- [LeetCode] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [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 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表
文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...
- LeetCode Continuous Subarray Sum
原题链接在这里:https://leetcode.com/problems/continuous-subarray-sum/description/ 题目: Given a list of non-n ...
- 523. Continuous Subarray Sum
class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...
随机推荐
- 基于jquery的插件开发
最近在公司做一个项目,由于后台数据太多需要分页显示,在网上找了很多插件都没有找到合适的分页插件,所有的分页插件始终达不到自己想要的效果.由于这个项目也不是很赶,就在网上查找各种资料,自己写一个基于jq ...
- GitHub趋势:Vue.js大有超过TensorFlow之势!
2月,Github上第二受欢迎的项目是Flutter.Flutter的第一个测试版本是作为2018年世界移动通信大会的一部分而开始的. Flutter是一款移动UI框架,旨在帮助开发人员在iOS和An ...
- 利用拷贝data目录文件的方式迁移mysql数据库
其实迁移数据库,一般用sql文件就行,把A服务器数据库的表结构和数据等等导出,然后导入到B服务器数据库, 但是这次数据文件过大,大约有40个G,使用命令行导入,效果不是很好,经常在执行过程中报错.卡死 ...
- Rails 定时任务——whenever实现周期性任务
根据项目的进展,我们需要实现后台进行定时读取信息的功能,而最关键的实现部分是周期性功能,根据调研,决定使用whenever来实现这一功能. github:https://github.com/java ...
- c语言函数作业
一.PTA实验作业 6-3 使用函数判断完全平方数 1. 本题PTA提交列表 2. 设计思路 1.利用由题目给定的int IsSquare( int n )进行操作 2.首先判断n是否大于0. 3.若 ...
- 冲刺NO.5
Alpha冲刺第五天 站立式会议 项目进展 今日项目完成内容主要包括了JS的学习,事务管理员模块与学生模块的完善与补充,并且开始编写信用信息管理模块和奖惩事务管理模块. 问题困难 前端部分的技术掌握的 ...
- 数字是否可以被3和5同时整除,use if and % (21.9.2017)
#include <stdio.h> int main(){ int a; //a是所输入的数字 printf("输入数字: "); scanf("%d&qu ...
- C++数据结构中的基本算法排序
冒泡排序 基本思想:两两比较待排序的数,发现反序时交换,直到没有反序为止. public static void BubbleSort(int[] R) { for (int i = 0; i < ...
- verilog学习笔记(2)_一个小module及其tb
module-ex_cnt module ex_cnt( input wire sclk, input wire rst_n, output wire[9:0] cnt ); reg [9:0] cn ...
- python简单路由系统
# 输入模块名/函数 url = input('请输入网址:') module,func = url.split('/') m = __import__('lib.'+module,fromlist= ...