[抄题]:

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只能取两边:带数据进去看一下就知道了

[一句话思路]:

右边界的循环在左边界的循环以内控制

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 0< i < nums.length,说最后一次
  2. 用指针的时候要提前确认一下指针范围,否则会莫名报错。忘了
  3. 左窗口可能一直重复,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的数组对的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【leetcode_easy】532. K-diff Pairs in an Array

    problem 532. K-diff Pairs in an Array 题意:统计有重复无序数组中差值为K的数对个数. solution1: 使用映射关系: 统计数组中每个数字的个数.我们可以建立 ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  9. [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 ...

随机推荐

  1. 第一章 Oracle10g数据库新特性

    1.1 Oracle10g数据库概述 1.1.1 网格数据库 Oracle10g数据库是一种为网格计算而设计的数据库,是第一个用完整集成的软件基础架构来实现网络计算的数据库系统,其中10g的g表示gr ...

  2. 在zxing开源项目里,camera.setDisplayOrientation(90)出现错误

    [错误提示]  setDisplayOrientation(int)未定义 [错误原因]  sdk版本过低,这个方法在Android2.2之后才有 [解决方法]  直接在project.propert ...

  3. iOS设置translucent 引发的坐标问题

    iOS  NavigationBar  + 导航栏 tablevie时候的布局情况,之前迷惑了我很久,怎么也没法理解透明度会影响布局. 接下来看一下以下三种情况的运行结果 1.全部系统默认情况下利用m ...

  4. [Client] looks like we got no XML document in....

    无wsdl方式应用webservice时,服务端包含了 include_once'inc/utility_all.php '一直报[Client] looks like we got no XML d ...

  5. Oracle视图编译错误解决办法

    因为新搭的环境,数据库是从另一个现成的环境导过来的,直接后台用exp和imp命令操作.但是新环境的Oracle数据库有问题,一些视图创建不了,导致用到这些视图的视图和存储过程也编译不了.后来手工重新编 ...

  6. Java基础数据类型二进制转换

    前言: 本文主要介绍java基础类型的二进制转换和二进制的基本概念. 二进制: 1,二进制是以0和1为码,逢2进1,比如3=11=1*2+1. 2,在计算机当中其它进制的算法基本基于2进制,因为计算机 ...

  7. Php header()函数及其常见使用

    语法: Void header(string $string[,bool $replace=true [, int $http_response_code) 向客户端发送原始的HTTP报头 需注意: ...

  8. springboot redis简单结合

    参考: https://www.cnblogs.com/ityouknow/p/5748830.htmlhttp://blog.csdn.net/i_vic/article/details/53081 ...

  9. 【转】JMeter脚本的参数化

    JMeter脚本的参数化 当你利用Badboy将你的测试脚本录制完毕后,接下来就是脚本的调试工作了.在我看来,调试应该包括有以下几个方面:1.根据测试场景对脚本进行必要的修改:2.脚本参数化:3.添加 ...

  10. Java-Maven-Runoob:Maven 构建 & 项目测试

    ylbtech-Java-Maven-Runoob:Maven 构建 & 项目测试 1.返回顶部 1. Maven 构建 & 项目测试 在上一章节中我们学会了如何使用 Maven 创建 ...