当前位置:网站首页>Daily question 2006 Number of pairs whose absolute value of difference is k
Daily question 2006 Number of pairs whose absolute value of difference is k
2022-07-05 05:42:00 【A big pigeon】
topic : Given array nums And integer k, In the array Satisfy |nums[i]-nums[j]| = k(i<j) Number to number .
Explain :1. Direct double cycle
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
cnt = 0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if abs(nums[j]-nums[i]) == k:
cnt += 1
return cnt2. Hashtable
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
cnt, ans = Counter(nums), 0
for key in cnt:
if (key + k) in cnt:
ans += cnt[key] * cnt[key + k]
return ans边栏推荐
- Personal developed penetration testing tool Satania v1.2 update
- How can the Solon framework easily obtain the response time of each request?
- kubeadm系列-00-overview
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- Common optimization methods
- Acwing 4300. Two operations
- Introduction to convolutional neural network
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- Annotation and reflection
- SAP method of modifying system table data
猜你喜欢

On-off and on-off of quality system construction

sync.Mutex源码解读

第六章 数据流建模—课后习题

Sword finger offer 58 - ii Rotate string left

Codeforces round 712 (Div. 2) d. 3-coloring (construction)
![[jailhouse article] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[jailhouse article] jailhouse hypervisor

EOJ 2021.10 E. XOR tree

Hang wait lock vs spin lock (where both are used)

The connection and solution between the shortest Hamilton path and the traveling salesman problem

Sword finger offer 05 Replace spaces
随机推荐
How to adjust bugs in general projects ----- take you through the whole process by hand
sync. Interpretation of mutex source code
Control unit
[cloud native] record of feign custom configuration of microservices
EOJ 2021.10 E. XOR tree
Software test -- 0 sequence
Simply sort out the types of sockets
Little known skills of Task Manager
Implement a fixed capacity stack
Acwing 4300. Two operations
shared_ Repeated release heap object of PTR hidden danger
The number of enclaves
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
ALU逻辑运算单元
F - Two Exam(AtCoder Beginner Contest 238)
Dynamic planning solution ideas and summary (30000 words)
Solution to the palindrome string (Luogu p5041 haoi2009)
Configuration and startup of kubedm series-02-kubelet
Over fitting and regularization
软件测试 -- 0 序