[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和 ...
随机推荐
- spring mvc 简单搭建
文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下. web.xml配置: </load-on-startup> </servlet> ...
- 浅谈Spring(四)
一.Spring+MyBatis整合 spring大大简化了Mybatis的开发步骤. 1.MyBatis的开发要点: mybatis-config.xml配置文件:配置与数据库的链接.mapper文 ...
- Java threadpool机制深入分析
简介 在前面的一篇文章里我对java threadpool的几种基本应用方法做了个总结.Java的线程池针对不同应用的场景,主要有固定长度类型.可变长度类型以及定时执行等几种.针对这几种类型的创建,j ...
- Android 电源系列小结s
package com.ritterliu.newBatteryWidget; import android.app.Activity; import android.app.Service; imp ...
- zoj 2071 Technology Trader 最大权闭合子图
传送门 和上一题一样, 也是一个最大权闭合子图.不过建图好麻烦的感觉 写了好久. 源点和原材料连边, 权值为val. 汇点和产品连边, 权值为val. 产品与和它有关系的材料连边, 权值inf. 最 ...
- Redhat Linux内核升级全记录(转)
http://www.sina.com.cn 2001/06/15 15:38 中国电脑教育报 李红 Redhat Linux因为比较容易上手,所以用户很多.它系统配置完善,预装了丰富的应 ...
- HDU 5755 Gambler Bo(高斯消元)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5755 [题目大意] 一个n*m由0,1,2组成的矩阵,每次操作可以选取一个方格,使得它加上2之后对 ...
- POJ 3169 Layout (图论-差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6574 Accepted: 3177 Descriptio ...
- Linux下PHP与普通C程序通信
Linux下的普通C程序之前可以使用FIFO(有名管道来进行进程间通信,因为这个管道以一个文件的形式存在于文件系统上,因此只要能读写这个文件就可以实现进程间通信. 首先使用mkfifo命令有文件系统上 ...
- java final 关键字醍醐灌顶
醍醐灌顶: final 关键字,它可以修饰数据 .方法.类. 可能有些同学傻傻分不清出,这里可以快速弄懂final; final 实例域: 可以将实例域定义为final,构建对象时必须初始化这样的域, ...