当前位置:网站首页>LeetCode1497-检查数组对是否可以被 k 整除-数组-哈希表-计数
LeetCode1497-检查数组对是否可以被 k 整除-数组-哈希表-计数
2022-07-01 04:48:00 【李烦烦搞快点】
Note:
1、数组中所有的数,都对k进行一个取余运算,这样之后就可以把题目变成是否有两个数的和等于k
2、刚才取余的同时就开个数组记录下来,每个数取完之后对应的是几
3、遍历一半的数组,判断每个mod[i] == mod[k - i]即可
4、最后,因为有可能俩人取完余数是0的情况,判断一下0号位置是不是2的倍数
代码如下:
class Solution {
public:
bool canArrange(vector<int>& arr, int k) {
vector<int> mod(k);
for(auto num: arr)
mod[(num % k + k) % k] ++;
for(int i = 1; i <= k / 2; i ++)
if(mod[i] != mod[k - i])
return false;
return mod[0] % 2 == 0;
}
};
边栏推荐
- All in all, the low code still needs to solve these four problems
- Pytorch(四) —— 可视化工具 Visdom
- LeetCode_35(搜索插入位置)
- Why is Internet thinking not suitable for AI products?
- 打印流与System.setout();
- Codeforces Round #771 (Div. 2) ABCD|E
- Neural networks - use sequential to build neural networks
- [pat (basic level) practice] - [simple simulation] 1064 friends
- 分布式锁的实现
- Thoughts on the construction of Meizhou cell room
猜你喜欢

The index is invalid

Dataloader的使用
![[hard ten treasures] - 1 [basic knowledge] classification of power supply](/img/a8/f129c9d15ca6ed99db1dacfc750ead.png)
[hard ten treasures] - 1 [basic knowledge] classification of power supply

Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling

Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition

Neural network - nonlinear activation

分布式锁的实现

先有网络模型的使用及修改

LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target

C -- array
随机推荐
Matters behind the construction of paint testing laboratory
【暑期每日一题】洛谷 P5740【深基7.例9】最厉害的学生
Pytoch (IV) -- visual tool visdom
Thoughts on the construction of Meizhou cell room
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
Dede collection plug-in does not need to write rules
[pat (basic level) practice] - [simple simulation] 1064 friends
Common interview questions ①
LeetCode_28(实现 strStr())
分布式事务-解决方案
Basic usage, principle and details of session
2022 hoisting machinery command registration examination and hoisting machinery command examination registration
[godot] unity's animator is different from Godot's animplayer
LeetCode_ 28 (implement strstr())
How to use common datasets in pytorch
Shell analysis server log command collection
FileInputStream
Dataloader的使用
[FTP] the solution to "227 entering passive mode" during FTP connection
STM32扩展板 温度传感器和温湿度传感器的使用
