//题目:
//给定一个整数数组与一个整数k,当且存在两个不同的下标i和j满足nums[i] = nums[j]而且| i - j | <= k时返回true。否则返回false。
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
//注意: 当K >= numsSize的时候
//愤慨的解法1300ms 哭晕在厕所
bool containsNearbyDuplicate(int* nums, int numsSize, int k)
{
//nums[i] = num[j] && |i-j| <= k
int i=0,j=0;
if(numsSize > k)
{
for(i=0;i<numsSize-k;i++)
{
for(j=i+1;j<=i+k;j++)
{
if(nums[i] == nums[j])
return true;
}
}
for(i=numsSize-k;i<numsSize;i++)
{
for(j=i+1;j<numsSize;j++)
if(nums[i] == nums[j])
return true;
}
}
else
{
for(i=0;i<numsSize;i++)
{
for(j=i+1;j<numsSize;j++)
if(nums[i] == nums[j])
return true;
}
} return false;
} int main()
{
int nums[2] = {1,1};
bool r = containsNearbyDuplicate(nums,2,2);
printf("containsNearbyDuplicate is : %d \n",r);
}

[Leetcode]-containsNearbyDuplicate的更多相关文章

  1. [LeetCode] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

  2. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  3. python leetcode 日记 --Contains Duplicate II --219

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  4. leetcode面试准备:Contains Duplicate I && II

    1 题目 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. You ...

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

  6. 【一天一道LeetCode】#219. Contains Duplicate II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. LeetCode数组解题模板

    一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...

  8. LeetCode哈希表

    1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...

  9. LeetCode算法题-Contains Duplicate II(Java实现)

    这是悦乐书的第193次更新,第197篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第53题(顺位题号是219).给定整数数组和整数k,找出数组中是否存在两个不同的索引i和 ...

随机推荐

  1. Redis中各种方法的使用

    ①set ; i< ; i++) { // 不可以重复添加数据 client.AddItemToSet(KKey, "dong升-" + i); } client.Remov ...

  2. Android 开发之Matrix图片处理类的使用

    在Android中,对图片的处理需要使用到Matrix类,Matrix是一个3 x 3的矩阵,他对图片的处理分为四个基本类型: 1.Translate————平移变换 2.Scale————缩放变换 ...

  3. Ubuntu14下LAMP环境的安装以及yaf扩展的安装

    前段时间在ubuntu下安装了lamp环境,记录一下安装过程方便以后查阅. 安装lamp环境 ① 安装apache sudo apt-get install apache2 系统会弹出如图所示的提示, ...

  4. php数组使用技巧及操作总结

    数组,可以说是PHP的数据应用中较重要的一种方式.PHP的数组函数众多,下面是一些小结,借此记之,便于以后鉴之. 1. 数组定义 数组的定义使用 array()方式定义,可以定义空数组:<?ph ...

  5. 对discuz的代码分析学习(二)首页文件

    如果当前地址栏存在查询字符,并且是一个数字,条件成立. 查询字符:www.baidu.com/index.php?aaa=bbb              aaa=bbb就是查询字符如果条件成立,则把 ...

  6. 基于KVM建立虚拟机的步骤及总结说明

    1.前言 目前正在涉足云计算IaaS工作,虚拟化是IaaS的重要部分,因此这段时间对各个虚拟机化技术和工具进行研究,研究的目的不仅仅是为了会使用这个工具,而是通过研究了解技术的实现机制和原理,即知其然 ...

  7. AIDE支持实时错误检查、代码重构、代码智能导航、生成APK

    AIDE是一个Android Java集成开发环境,可以在Android系统内进行Android软件和游戏的开发.它不仅仅是一个编辑器,而是支持编写-编译-调试运行整个周期,开发人员可以在Androi ...

  8. Codeforces 427D Match &amp; Catch(后缀自动机)

    [题目链接] http://codeforces.com/problemset/problem/427/D [题目大意] 给出一个两个字符串,求出最短且在两个字符串中唯一的公共子串. [题解] 以原字 ...

  9. Thread’s start method and run method

    工作中用到了Thread,一开始用错了,仔细研究了一下,稍作整理. 前言,今天写代码居然这样写的 new Thread() { @Override public void run() { System ...

  10. 【c语言】推断一个数是不是2的n次方

    // 推断一个数是不是2的n次方 #include <stdio.h> void judge_n(int a) { int b = a - 1; if ((a & b) == 0) ...