LeetCode(219) Contains Duplicate II
题目
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 difference between i and j is at most k.
分析
题目描述:给定一个整数序列,查找是否存在两个下标分别为i和j的元素值nums[i]=nums[j]且满足i于j的距离最大为k;
定义一个长度最大为k的滑动窗口,用一个unordered_set维护窗口内的数字判断是否出现重复,使用两个指针start和end标记滑动窗口的两端,初始都是0,然后end不断进行扩展,扫描元素判断是否出现重复元素,直到发现end−start>k, 就开始移动start,并且在set中移除对应的元素。如果以为扫描到数组末尾还没有发现重复元素,那就可以返回false。时间复杂度和空间复杂度都是O(N)。
AC代码
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (nums.empty())
return false;
int sz = nums.size();
//使用容器unordered_set 其查找性能为常量
unordered_set<int> us;
int start = 0, end = 0;
for (int i = 0; i < sz; ++i)
{
if (us.count(nums[i]) == 0)
{
us.insert(nums[i]);
++end;
}
else{
return true;
}
if (end - start > k)
{
us.erase(nums[start]);
++start;
}
}//for
return false;
}
};
LeetCode(219) Contains Duplicate II的更多相关文章
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- LeetCode(47):全排列 II
Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...
- LeetCode(217)Contains Duplicate
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- LeetCode(63)Unique Paths II
题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...
随机推荐
- NET Core 与 Vue.js 服务端渲染
NET Core 与 Vue.js 服务端渲染 http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/原作者: ...
- 使用CRA开发的基于React的UI组件发布到内网NPM上去
前言:构建的ES组件使用CNPM发布内网上过程 1. 使用Create-React-APP开的组件 如果直接上传到NPM,你引用的时候会报: You may need an appropriate l ...
- AngularJS(六):表单-复选框
本文也同步发表在我的公众号“我的天空” 复选框 复选框只有两个值:true或者false,因此在AngularJS中,一般都是将复选框的ng-model绑定为一个布尔值属性,通过这两个布尔值来决定其勾 ...
- android :fragmentation使用中遇到的 NullPointerException
背景:fragmentation(单ACTIVITY+多个fragments)+brvah( recyclerView多级自定义菜单功能) 目的:实现 菜单栏的点击,fragment 显示相应的内 ...
- 一个mybatis错误导致无法启动项目的问题
今天遇到Mybatis一个问题,导致项目一直起不来,查了很久发现是MapperXML的错,问题表现为: 系统始终起不来,但也不报错,始终卡到如下信息位置: 信息: Initializing Sprin ...
- SQL Server 填充因子
在创建聚集索引时,表中的数据按照索引列中的值的顺序存储在数据库的数据页中.在表中插入新的数据行或更改索引列中的值时,Microsoft® SQL Server™ 2000 可能必须重新 ...
- jsp之初识UserBean
package com.java.model; public class Student { private String name; private int age; public String g ...
- 如何处理SAP云平台错误消息 there is no compute unit quota for subaccount
当我试图部署一个应用到SAP云平台的neo环境时: 指定Compute Unit Size为Lite: 点击Deploy按钮,遇到如下错误消息:there is no compute unit quo ...
- UVA 211 The Domino Effect 多米诺效应 (回溯)
骨牌无非两种放法,横着或竖着放,每次检查最r,c最小的没访问过的点即可.如果不能放就回溯. 最外面加一层认为已经访问过的位置,方便判断. #include<bits/stdc++.h> ; ...
- fork新建进程
#include <sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<stdio.h> ...