当前位置:网站首页>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
边栏推荐
- Personal developed penetration testing tool Satania v1.2 update
- Cluster script of data warehouse project
- Sword finger offer 04 Search in two-dimensional array
- Daily question - Search two-dimensional matrix PS two-dimensional array search
- A misunderstanding about the console window
- 读者写者模型
- 剑指 Offer 05. 替换空格
- After setting up the database and website When you open the app for testing, it shows that the server is being maintained
- Daily question - longest substring without repeated characters
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
猜你喜欢
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
SAP-修改系统表数据的方法
Some common problems in the assessment of network engineers: WLAN, BGP, switch
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
剑指 Offer 53 - II. 0~n-1中缺失的数字
CF1637E Best Pair
Scope of inline symbol
Fried chicken nuggets and fifa22
2017 USP Try-outs C. Coprimes
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
随机推荐
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
Detailed explanation of expression (csp-j 2021 expr) topic
How many checks does kubedm series-01-preflight have
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
Gbase database helps the development of digital finance in the Bay Area
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
Reflection summary of Haut OJ freshmen on Wednesday
EOJ 2021.10 E. XOR tree
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
剑指 Offer 06.从头到尾打印链表
Cluster script of data warehouse project
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
Introduction to convolutional neural network
The connection and solution between the shortest Hamilton path and the traveling salesman problem
Acwing 4301. Truncated sequence
Annotation and reflection
SAP method of modifying system table data
AtCoder Grand Contest 013 E - Placing Squares
kubeadm系列-01-preflight究竟有多少check
Software test -- 0 sequence