532. K-diff Pairs in an Array绝对值差为k的数组对
[抄题]:
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为找k只能取两边:带数据进去看一下就知道了
[一句话思路]:
右边界的循环在左边界的循环以内控制
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 0< i < nums.length,说最后一次
- 用指针的时候要提前确认一下指针范围,否则会莫名报错。忘了
- 左窗口可能一直重复,eg 11111,用while去重。头一次见
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
右边界的循环在左边界的循环以内控制
[复杂度]:Time complexity: O(每个左边界都对应新一半中的右边界nlgn) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
右边界在左边界之内:
for (int i = 0, j = 0; i < nums.length; i++) {
for (j = Math.max(j, i + 1); j < nums.length && nums[j] - nums[i] < k; j++);
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int findPairs(int[] nums, int k) {
//cc
if (nums == null || nums.length == 0) {
return 0;
}
//ini
int count = 0;
//sort
Arrays.sort(nums);
//for
for (int i = 0, j = 0; i < nums.length; i++) {
for (j = Math.max(j, i + 1); j < nums.length && nums[j] - nums[i] < k; j++);
if (j < nums.length && nums[j] - nums[i] == k) count++;
while (i + 1 < nums.length && nums[i + 1] == nums[i]) i++;
}
//return
return count;
}
}
532. K-diff Pairs in an Array绝对值差为k的数组对的更多相关文章
- LeetCode 532. K-diff Pairs in an Array (在数组中相差k的配对)
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- [LeetCode] K-diff Pairs in an Array 数组中差为K的数对
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- 【leetcode_easy】532. K-diff Pairs in an Array
problem 532. K-diff Pairs in an Array 题意:统计有重复无序数组中差值为K的数对个数. solution1: 使用映射关系: 统计数组中每个数字的个数.我们可以建立 ...
- 532. K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- [LeetCode] K Inverse Pairs Array K个翻转对数组
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- 629. K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- 【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- [Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
随机推荐
- MyBatis典型的错误org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
XXXmapper.java(接口) XXXmapper.xml(结果集映射) //此两个文件要在统一包下,且xml中的namespace是唯一的,为了区分须写成 该xml的全路径
- Linux下shell命令 1
1 [root@hadoop-namenode-1 iebd] cd /filename/filename 跳转至filename文件夹 2 [root@hadoop-namenode-1 ...
- EL表达式可以直接放在url的“ ”里面
<div class="hc-prm-search search flr"> <form action="/userCenter/projectInfo ...
- CodeForces - 484BMaximum Value(hash优化)
个人心得:周测题目,一题没出,难受得一批.这个题目做了一个半小时还是无限WR,虽然考虑到了二分答案这个点上面了, 奈何二分比较差就想用自己的优化,虽然卡在了a=k*b+c,这里但是后面结束了这样解决还 ...
- openfaas 私有镜像配置
备注: 此项目是使用nodejs 生成唯一id 的\ 预备环境 docker harbor faas-cli openfaas k8s 1. 项目初始化 faas-cli new node --la ...
- FPGA前世今生(四)
前几期我们一直介绍FPGA内部资源,今天我们将用最后的篇幅来介绍剩下的内部资源部分,结束我们FPGA的前世今生.之所以起名字为FPGA前世今生,其实就是介绍一下FPGA内部资源,前世的内部结构资源就不 ...
- windows下通过.bat运行java程序
在windows下运行Java项目,单独的jar可以使用,java -jar xxx.jar 运行,如果是一个zip包,里面包含了class文件和所依赖的jar的时候,可以使用 (也可以以看看这里): ...
- 【转】JMeter远程测试
详解JMeter远程测试(1) 如果运行JMeter客户端的机器性能不能满足测试需要,那么测试人员可以通过单个JMeter GUI客户端来控制多个远程JMeter服务器,以便对服务器进行压力测试,模拟 ...
- Java-Maven-Runoob:Maven 自动化部署
ylbtech-Java-Maven-Runoob:Maven 自动化部署 1.返回顶部 1. Maven 自动化部署 项目开发过程中,部署的过程包含需如下步骤: 将所的项目代码提交到 SVN 或者代 ...
- Xdebug日志文件不显示
Xdebug是一个很强大的调试php的软件,安装也很简单. 1.php_xdebug.dll 放入php目录下的ext文件中 2.php.ini中开启 [Xdebug] extension = &qu ...