leetcode523 Continuous Subarray Sum
思路:
令sum[p]表示p位置的前缀和。如果sum[i] % k == sum[j] % k (j - i > 1),则存在子段(i, j]的和能够整除k。
实现:
class Solution
{
public:
bool checkSubarraySum(vector<int>& nums, int k)
{
if (nums.size() < ) return false;
int n = nums.size();
for (int i = ; i < n - ; i++)
if (nums[i] == && nums[i + ] == )
return true;
if (k == ) return false;
int sum = ;
unordered_map<int, int> m;
m[] = -;
for (int i = ; i < n; i++)
{
sum = (sum + nums[i]) % k;
if (m.count(sum))
{
if (i - m[sum] > )
return true;
}
else m[sum] = i;
}
return false;
}
};
leetcode523 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 ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- 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题是存储的当前累加和的 ...
- [Swift]LeetCode523. 连续的子数组和 | Continuous Subarray Sum
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 ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- Continuous Subarray Sum LT523
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
随机推荐
- Linux 网络配置,ifconfig不显示ip地址的解决办法
进入到/etc/sysconfig/network-scripts 然后设置虚拟机的网络配置 这样就配置成功了
- WCF的Binding模型之四:信道工厂(Channel Factory)
由于信道管理器在客户端和服务端所起的不同作用,分为信道监听器和信道工厂.和服务端的信道监听其相比,处于客户端的信道工厂显得简单.从名称就可以看得出来,信道工厂的作用就是单纯的创建用于消息发送的信道.我 ...
- C#中使用 Oracle的事务与存储过程
1 存储过程 1.1 不带参数,没有返回值 创建表 create table test (ID number, NAME varchar2(), SEX varchar2(), AGE number, ...
- How to force immediate stop of threads in Jmeter servers如何在jmeter执行完,立即停止jmeter
https://stackoverflow.com/questions/38900315/how-to-force-immediate-stop-of-threads-in-jmeter-server ...
- dubbo服务的group和version
group 当一个接口有多种实现时,可以用group区分 <!-- dubbo group 使用示例 --> <bean id="demoA" class=&qu ...
- CentOS 7防火墙服务FirewallD指南
CentOS 7防火墙服务FirewallD指南 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 防火墙是一种位于内部网 ...
- struts2学习笔记(二)—— 获取登录信息及计算在线人数
实现目的: 1.点击"Login"的超链接.进入登录页面 2.填写登录信息.提交表单,将用户信息保存进Session 3.显示用户名,并计算在线 ...
- 017 SSH
Router>en Router#config t Enter configuration commands, one per line. End with CNTL/Z. Router(co ...
- AHCI IDE
AHCI模式性能好 IDE模式,提高约20%,使用Windows 7 系统,AHCI 模式是最佳选择,特别是对SSD硬盘IDE是为XP的兼容性,RAID 模式是要有两块以上硬盘才能实现AHCI模式装的 ...
- sum over使用方法,以及与group by的差别
1.sum over使用方法 sum(col1) over(partition by col2 order by col3 ) 以上的函数能够理解为:按col2 进行分组(partition ),每组 ...