当前位置:网站首页>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
边栏推荐
- Sword finger offer 58 - ii Rotate string left
- Sword finger offer 05 Replace spaces
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
- Sword finger offer 53 - ii Missing numbers from 0 to n-1
- Add level control and logger level control of Solon logging plug-in
- Csp-j-2020-excellent split multiple solutions
- CF1634 F. Fibonacci Additions
- 剑指 Offer 09. 用两个栈实现队列
- Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
猜你喜欢
Sword finger offer 06 Print linked list from beginning to end
AtCoder Grand Contest 013 E - Placing Squares
Sword finger offer 05 Replace spaces
Solution to game 10 of the personal field
剑指 Offer 09. 用两个栈实现队列
Typical use cases for knapsacks, queues, and stacks
Smart construction site "hydropower energy consumption online monitoring system"
浅谈JVM(面试常考)
Sword finger offer 05 Replace spaces
Dynamic planning solution ideas and summary (30000 words)
随机推荐
A problem and solution of recording QT memory leakage
Sword finger offer 09 Implementing queues with two stacks
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
[practical skills] technical management of managers with non-technical background
Introduction to tools in TF-A
SSH password free login settings and use scripts to SSH login and execute instructions
Fried chicken nuggets and fifa22
剑指 Offer 53 - II. 0~n-1中缺失的数字
In this indifferent world, light crying
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
常见的最优化方法
[jailhouse article] look mum, no VM exits
Maximum number of "balloons"
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
ALU逻辑运算单元
Sword finger offer 06 Print linked list from beginning to end
Alu logic operation unit
Codeforces Round #716 (Div. 2) D. Cut and Stick
Smart construction site "hydropower energy consumption online monitoring system"