[抄题]:

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. Raspberry Pi 配置

    资料 教程 <树莓派使用指南> 系统盘制作 插上电源,连上HDML线,计科

  2. session不一定非得要cookie开启才能使用。也可以使用get传递参数

    session不一定非得要cookie开启才能使用.也可以使用get传递参数 可以将session_id();设置为一个常量. define(’s_id‘ ,$_COOKIE['session_nam ...

  3. WC2019 T1 数树

    WC2019 T1 数树 传送门(https://loj.ac/problem/2983) Question 0 对于给定的两棵树,设记两颗树 \(A,B\) 的重边数量为 \(R(A,B)\),那么 ...

  4. python 的日志logging模块

    1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message')logging.info('This is info messag ...

  5. C# 用wps(api v9) 将word转成pdf

    我们不产生代码只是代码的搬运工 我们先来看一段跑不起来的代码  ..各种未将对象应用到实例.. using System; using System.Collections.Generic; usin ...

  6. laravel 中间件学习

    http://blog.csdn.net/kwinh/article/details/56285204 http://blog.csdn.net/fationyyk/article/details/5 ...

  7. SVN1.6服务端和客户端安装配置指导

    本节向大家描述SVN1.6服务端和客户端安装配置步骤,随着SVN的快速发展,版本也进行了升级更新,本节就和大家一起学习一下SVN1.6服务端和客户端安装配置步骤,欢迎大家一起来学习.下面是具体介绍.1 ...

  8. 对于global的介绍

    抄自http://veniceweb.googlecode.com/svn/trunk/public/daily_tech_doc/erlang_global_20091109.txt 1. 介绍:这 ...

  9. PHP5.6 安装Yaf 2.3.5

    Yaf 安装 PHP5.6 安装Yaf 2.3.5 1.下载 https://github.com/laruence/yaf 2.phpize /usr/bin/phpize 3.配置 ./confi ...

  10. 阿里云openapi接口使用,PHP,视频直播

    1.下载sdk放入项目文件夹中 核心就是aliyun-php-sdk-core,它的配置文件会自动加载相应的类 2.引入文件 include_once LIB_PATH . 'ORG/aliyun-o ...