【easy】532. K-diff Pairs in an Array】的更多相关文章

这道题给了我们一个含有重复数字的无序数组,还有一个整数k,让我们找出有多少对不重复的数对(i, j)使得i和j的差刚好为k.由于k有可能为0,而只有含有至少两个相同的数字才能形成数对,那么就是说我们需要统计数组中每个数字的个数.我们可以建立每个数字和其出现次数之间的映射,然后遍历哈希表中的数字,如果k为0且该数字出现的次数大于1,则结果res自增1:如果k不为0,且用当前数字加上k后得到的新数字也在数组中存在,则结果res自增1,参见代码如下: /* 思路:因为存在k=0的情况,并且答案需要是完…
class Solution { public: int quicksort(vector<int>& nums, int start, int end, int k){ int i = start; int j = end; int x = nums[i]; while (i<j){ //快排核心… while (nums[j]<x && i<j) j--; if (i<j) nums[i++] = nums[j]; while (nums[i…
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself.…
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 differen…
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Example 1: Inpu…
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. 错误解法: class Solut…
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represented by empty parenthesis pair "()". And you…
[BZOJ3436]小K的农场(差分约束) 题面 由于BZOJ巨慢无比,使用洛谷美滋滋 题解 傻逼差分约束题, 您要是不知道什么是差分约束 您就可以按下\(Ctrl+W\)了 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set> #in…
[BZOJ3110][LG3332][ZJOI2013]K大数查询 题面 洛谷 BZOJ 题解 和普通的整体分治差不多 用线段树维护一下每个查询区间内大于每次二分的值\(mid\)的值即可 然后再按套路做就行了 代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm&…
<通向财务自由之路> 作 者:[美]范·K·萨普曼  译 者:董梅 系 列: 出 版:机械工业出版社 字 数:约40千字 阅读完成:2013年11月26日…