当前位置:网站首页>LeetCode 2006. Number of pairs whose absolute value of difference is k
LeetCode 2006. Number of pairs whose absolute value of difference is k
2022-06-24 03:39:00 【freesan44】
Title address (2006. The absolute value of the difference is K Number to number )
https://leetcode-cn.com/problems/count-number-of-pairs-with-absolute-difference-k/
Title Description
Give you an array of integers nums And an integer k , Please return the number pair (i, j) Number of , Satisfy i < j And |nums[i] - nums[j]| == k . |x| The value of is defined as : If x >= 0 , Then the value is x . If x < 0 , Then the value is -x . Example 1: Input :nums = [1,2,2,1], k = 1 Output :4 explain : The absolute value of the difference is 1 The number pair of is : - [1,2,2,1] - [1,2,2,1] - [1,2,2,1] - [1,2,2,1] Example 2: Input :nums = [1,3], k = 3 Output :0 explain : The absolute value of any number pair difference is 3 . Example 3: Input :nums = [3,2,1,5,4], k = 2 Output :3 explain : The absolute value of the difference is 2 The number pair of is : - [3,2,1,5,4] - [3,2,1,5,4] - [3,2,1,5,4] Tips : 1 <= nums.length <= 200 1 <= nums[i] <= 100 1 <= k <= 99
Ideas
Violence solution
Code
- Language support :Python3
Python3 Code:
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
length = len(nums)
res = 0
for i in range(length):
for j in range(i+1,length):
if k == abs(nums[i]-nums[j]):
res += 1
return resComplexity analysis
Make n Is array length .
- Time complexity :$O(nlogn)$
- Spatial complexity :$O(1)$
边栏推荐
- Go program lifecycle
- 3D visualization of Metro makes everything under control
- Live broadcast Reservation: cloud hosting or cloud function, how can the business do a good job in technology selection?
- An example of SPM manual binding execution plan
- Koom of memory leak
- What is the difference between server leasing and hosting?
- Coding Ci of Devops
- Grp: how to add Prometheus monitoring in GRP service?
- A Tencent interview question
- Do you understand TLS protocol?
猜你喜欢
随机推荐
ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0
ClickHouse Buffer
Some basic knowledge of data center server cabinet
618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?
How to use elastic scaling in cloud computing? What are the functions?
Differences between EDI and VMI
Grpc: how to add API Prometheus monitoring interceptors / Middleware?
Does the user need a code signing certificate? What is the use of a code signing certificate
What is edge computing? What are the characteristics of the Internet platform edge calculator?
Community pycharm installation visual database
Several options of F8 are very useful
Building RPM packages - spec Basics
Independent innovation and localization technology: SMT production line monitoring and management visualization of intelligent manufacturing
EIP maximum EIP EIP remote desktop access
Clickhouse optimize table comprehensive analysis
Ar 3D map technology
2021-10-02: word search. Given an M x n two-dimensional character grid boa
Why does the fortress machine use an application publisher? What are the main functions of the fortress machine
Do you understand TLS protocol?
What technology does cloud computing elasticity scale? What are the advantages of elastic scaling in cloud computing?








