当前位置:网站首页>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)$
边栏推荐
- How much is a fortress machine? Why do you need a fortress machine?
- Live broadcast Reservation: cloud hosting or cloud function, how can the business do a good job in technology selection?
- Grpc: how to add API Prometheus monitoring interceptors / Middleware?
- Chapter 6: UART echo case of PS bare metal and FreeRTOS case development
- 内存泄漏之KOOM-Shark中的Hprof信息
- Applicationclientprotocol of yarn source code
- An example of SPM manual binding execution plan
- Modstartcms theme introductory development tutorial
- What protocols do fortress computers have and what protocols do fortress computers generally use
- web渗透测试----5、暴力破解漏洞--(3)FTP密码破解
猜你喜欢

Halcon knowledge: contour operator on region (2)

Modstartcms theme introductory development tutorial
Thank you for your recognition! One thank-you note after another

Get to know MySQL database

ModStartCMS 主题入门开发教程

On game safety (I)

在pycharm中pytorch的安装

元气森林推“有矿”,农夫山泉们跟着“卷”?

你了解TLS协议吗?

halcon知识:区域(Region)上的轮廓算子(2)
随机推荐
How do websites use CDN? What are the benefits of using it?
What is load balancing? What are the functions of load balancing?
What does elastic public IP mean? The advantages of elastic public IP
Summary of common problems of real-time audio and video TRTC - quality
How to bypass CDN to get web pages? How many options are available?
Koom of memory leak
LeetCode 1047. Delete all adjacent duplicates in the string
ClickHouse Buffer
Highlights of future cloud native CIF Forum
RI Geng series: write a simple shell script, but it seems to have technical content
Spirit breath development log (17)
Tencent cloud ASR product -php realizes the authentication request of the extremely fast version of recording file identification
Technical dry goods - how to use AI technology to accurately identify mining Trojans
web渗透测试----5、暴力破解漏洞--(2)SNMP密码破解
MySQL stored procedure + function
Clickhouse optimize table comprehensive analysis
Record the creation process of a joke widget (II)
Tencent cloud launched its new 100g+ cloud server product!! Expect more than 400g+ in the future!
ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0
高斯光束及其MATLAB仿真