当前位置:网站首页>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 cnt
2. 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
边栏推荐
- lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- High precision subtraction
- Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
- Use of room database
- 2017 USP Try-outs C. Coprimes
- Bit mask of bit operation
- SAP-修改系统表数据的方法
- R语言【数据集的导入导出】
- ALU逻辑运算单元
- [jailhouse article] look mum, no VM exits
猜你喜欢
Sword finger offer 53 - I. find the number I in the sorted array
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
R语言【数据集的导入导出】
CF1634E Fair Share
剑指 Offer 05. 替换空格
[practical skills] how to do a good job in technical training?
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
从Dijkstra的图灵奖演讲论科技创业者特点
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
【实战技能】非技术背景经理的技术管理
随机推荐
剑指 Offer 06.从头到尾打印链表
浅谈JVM(面试常考)
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
Time of process
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
Sword finger offer 05 Replace spaces
剑指 Offer 58 - II. 左旋转字符串
游戏商城毕业设计
[practical skills] how to do a good job in technical training?
【Jailhouse 文章】Jailhouse Hypervisor
卷积神经网络简介
Brief introduction to tcp/ip protocol stack
Hang wait lock vs spin lock (where both are used)
Palindrome (csp-s-2021-palin) solution
AtCoder Grand Contest 013 E - Placing Squares
kubeadm系列-02-kubelet的配置和启动
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
How can the Solon framework easily obtain the response time of each request?
Add level control and logger level control of Solon logging plug-in
shared_ Repeated release heap object of PTR hidden danger