523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组
非负数组中找到和为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的倍数的连续子数组的更多相关文章
- 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题是存储的当前累加和的 ...
- [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 ...
- 523. Continuous Subarray Sum是否有连续和是某数的几倍
[抄题]: Given a list of non-negative numbers and a target integer k, write a function to check if the ...
- 523. Continuous Subarray Sum
class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...
- 【leetcode】523. Continuous Subarray Sum
题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
随机推荐
- luogu3384 【模板】 树链剖分
题目大意 已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作:操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z操作2: 格式: 2 x ...
- java.lang.IllegalStateException: No instances available for localhost
在SpringCloud的项目中,我们使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如果要调用真 ...
- 学习html5 中的canvas(一)
1.canvas画直线 <!doctype html> <html> <head> <meta charset="UTF-8"> & ...
- C++的学习 (此博客将一直补充更新下去,C++语法方面的内容不开新随笔了, *【语法学习】)
// #include <sstream> // stringstream 是 C++ 提供的另一个字串型的串流(stream)物件,包含在上述头文件中 // 先谈它在字符串处理方面的应用 ...
- 数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索( ...
- 跳转到AppStore 的不同位置办法
程序跳转到appstore中指定的应用 1.进入appstore中指定的应用NSString *str = [NSString stringWithFormat: ...
- codeforces 450C. Jzzhu and Chocolate 解题报告(449A)
题目链接:http://codeforces.com/contest/450/problem/C 题目意思:给出一个 n * m 大小的chocolate bar,你需要在这个bar上切 k 刀,使得 ...
- LA-3716(sort的神用)
题意: 给出两条长度均为n的NDA链A和B,找出一段最长的字串[l,r]使得该区域的突变位置不超过p%; 思路: sum[i]表示[1,i]中不相同的个数,可得表达式(sum[i]-sum[j])/( ...
- maven(一)创建一个maven的web项目
一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写相应的信息,Packaged是默认创建 ...
- 年少和 Smart の日常比赛 R3
在洛谷上参加了个比赛....写写题解 rank3....共5人...(捂脸 没有注明是官方代码的均是我比赛时本人提交的代码 T1 洗牌 题目描述 小明把 n (n 为偶数)张牌按编号顺序 1, 2, ...