当前位置:网站首页>Leetcode1497- check whether array pairs can be divided by K - array - hash table - count
Leetcode1497- check whether array pairs can be divided by K - array - hash table - count
2022-07-01 04:53:00 【Li Fan, hurry up】
Note:
1、 All the numbers in the array , All the k Perform a remainder operation , In this way, the question can be changed into whether the sum of two numbers is equal to k
2、 Just take the remainder and open an array to record , After each number is fetched, the corresponding number is
3、 Traverse half the array , Each judgment mod[i] == mod[k - i] that will do
4、 Last , Because it's possible that two people can get the remainder 0 The situation of , Judge 0 Position number is not 2 Multiple
The code is as follows :
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;
}
};
边栏推荐
猜你喜欢

Pytoch (II) -- activation function, loss function and its gradient

Dede collection plug-in does not need to write rules

Research on medical knowledge atlas question answering system (I)

RuntimeError: “max_pool2d“ not implemented for ‘Long‘

Pytoch (IV) -- visual tool visdom

【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点

Distributed - summary list

This sideline workload is small, 10-15k, free unlimited massage

Pytorch convolution operation
![[summer daily question] Luogu p5886 Hello, 2020!](/img/ac/4be05f80aab7fb766674e6e2d16fbc.png)
[summer daily question] Luogu p5886 Hello, 2020!
随机推荐
Thread safety issues
LeetCode_66(加一)
Construction of Meizhou nursing laboratory: equipment configuration
Pytorch(三) —— 函数优化
Pytorch neural network construction template
[FTP] the solution to "227 entering passive mode" during FTP connection
Neural network - nonlinear activation
STM32 expansion board digital tube display
Pico Neo3手柄抓取物体
[hard ten treasures] - 1 [basic knowledge] classification of power supply
对象的序列化与反序列化
先有网络模型的使用及修改
Cmake selecting compilers and setting compiler options
This sideline workload is small, 10-15k, free unlimited massage
【硬十宝典】——1.【基础知识】电源的分类
Codeworks round 449 (Div. 1) C. Kodori tree template
手动实现一个简单的栈
[FTP] common FTP commands, updating continuously
Manually implement a simple stack
Neural networks - use of maximum pooling
