[Leetcode]-containsNearbyDuplicate
//题目:
//给定一个整数数组与一个整数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的更多相关文章
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- 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 ...
- leetcode面试准备:Contains Duplicate I && II
1 题目 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. You ...
- 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 ...
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode数组解题模板
一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...
- LeetCode哈希表
1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...
- LeetCode算法题-Contains Duplicate II(Java实现)
这是悦乐书的第193次更新,第197篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第53题(顺位题号是219).给定整数数组和整数k,找出数组中是否存在两个不同的索引i和 ...
随机推荐
- SSH框架——Sprign声明式事务
Spring事务管理 Spring是SSH中的管理员,负责管理其它框架,协调各个部分的工作.今天一起学习一下Spring的事务管理.Spring的事务管理分为声明式跟编程式.声明式就是在Spring的 ...
- JavaSE复习日记 : 抽象类
/* * 抽象类 * 抽象: * 面向对象的三大核心思想; * 封装: 封装,ppp是封装的一部分 * 继承; * 多态: 父类的引用指向子类的对象 * 引用: 是指一个引用型变量 * 有哪些变量? ...
- 多关键字排序(里面有关于操作符(<<运算符 和 >>运算符 )的重载)
一种排序 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数:现 ...
- Python正则表达式指南(转载)
转载自:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html#3353540 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不 ...
- 树的判断(poj nyoj hduoj)
题目: http://ac.jobdu.com/problem.php?pid=1481 http://acm.nyist.net/JudgeOnline/problem.php?pid=129 ht ...
- android小知识之EditText输入框之值监控以及类型限制(数字,英语字母,下划线,是否为星号密码)
1.设置EditText的值监听事件 . <span style="font-size:14px;color:#990000;"> EditText ed=new Ed ...
- 关于mysqli 连接数不能正确释放的解决方案
/** * 析构函数 */ //解决重复链接的问题 private $db_handler = null; function __destruct() { Log::logWrite($this-&g ...
- java单链表代码实现
用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...
- 一、Cocos2dx在visualStudio或者vc++中环境搭建(入门篇)
本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=106 0.概述 Cocos2dx-win32的项目能够被向导生成 向导支持vs2008,vs2010 ...
- Javascript对象的声明
JavaScript 对象 对象由花括号分隔.在括号内部,对象的属性以名称和值对的形式 (name : value) 来定义.属性由逗号分隔: var person={firstname:" ...