[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和 ...
随机推荐
- ubuntu, Debian, CentOS
ubuntu源自debian,内核很多文档都还是debian的字样,稳定性逐渐增强,基本满足日常开发. debian的核心稳定,性能强劲. centos的内核版本低,安全性高. 选择Debian是因为 ...
- hadoop搭建杂记:Linux下不同linux主机之间文件copy的scp命令
不同的Linux之间copy文件常用有3种方法: 不同的Linux之间copy文件常用有3种方法: ①ftp 就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的程序来进行文件 ...
- 阿里云服务器linux(centos)常用命令
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- SQL调优日志--内存问题
SQL调优日志--内存问题排查入门篇 概述 很多系统的性能问题,是由内存导致的.内存不够会导致页面频繁换入换出,IO队列高,进而影响数据库整体性能. 排查 内存对数据库性能非常重要.那么我当出现问 ...
- CSS3自适应字体大小(vw vh)
viewpoint css3提供了一些与当前viewpoint相关的元素,vw,vh,vmin, vmax等. “viewpoint” = window size vw = 1% of viewpor ...
- Android程序报错 Connection refused 处理
在用Android测试JSON数据解析的时候,报了这样一个异常: java.net.ConnectException: localhost/ - Connection refused 原来模拟器默认把 ...
- DOM ISO - get current element's XPATH
DOM ISO - get current element's XPATH DOM ISO - get current element's XPATH
- 多名Uber司机被指刷单遭封号 一周薪水为0
昨天,一司机在Uber“司机之家”办公地墙上写了泄愤的话 摄/法制晚报记者 苏妮 司机展示的账单显示,上周的薪水几乎为零,上面用英文标注了“欺诈行为”的字样 摄/法制晚报记者 苏妮 法制晚报讯(记者 ...
- Android常用控件之ExpandableList的使用
先来看下什么是ExpandableListView 跟列表有点像,这种是可以折叠的列表,下面来看下是如何在代码中实现 一.在布局文件中声明一个ExpandableListView <Linear ...
- Linux c 信号—pause、sigsuspend 的相同于区别
pause函数: 功能:让进程暂停直到信号出现 #include<unistd.h> intpause(); 函数说明:pause()会令目前的进程暂停(进入睡眠状态),直至信号 ...