2017-3-11 leetcode 217 219 228
ji那天好像是周六。。。。。吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333
======================================================================
leetcode217 Contains Duplicate
leetcode219 Contains Duplicate II
leetcode228 Summary Ranges
=======================================================================
217讲的是
给你n个数字,判断下是否有重复的数据出现。
我的思路
丢到unordered_map里检查下就行。。。
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int n=nums.size();
unordered_map<int,int> m;
for(int i=;i<n;i++){
if(m.find(nums[i])==m.end()){
m[nums[i]]=;
}else return true;
}
return false;
}
};
217
讨论版里没有什么新奇的思路。。。
=======================================================================
219讲的是
给你n个数字和一个数字k,判断下是否有重复的数据出现在任意长度为k的区间内。
也就是说如果有任意ij满足nums[i]==nums[j]&&abs(i-j)<=k则返回真
我的思路
丢到unordered_map里检查下就行。。。距离小于i-k的erase掉就好。。。
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int n=nums.size();
unordered_map<int,int> m;
for(int i=;i<min(n,k+);i++){
if(m.find(nums[i])==m.end()){
m[nums[i]]=;
}else return true;
}
for(int i=min(n,k+);i<n;i++){
m.erase(nums[i-k-]);
if(m.find(nums[i])==m.end()){
m[nums[i]]=;
}else return true;
}
return false;
}
};
219
恩,其实这种判断元素存在性的题目,用set会更好些。
========================================================================
228讲的是
给你n个数字(有序,无重),把点转化为区间。
比如输入[0,1,2,4,5,7],输出["0->2","4->5","7"](字符串数组)
我的思路
线性的扫就好了。。。O(n)啊。。。不知道为什么难度被定义为中等。。
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int n=nums.size();
vector<string> aim;
if(!n)return aim;
int s=nums[],t=nums[];
for(int i=;i<n;i++){
if(nums[i]!=nums[i-]+){
t=nums[i-];
char temp[];
if(s==t)
sprintf(temp,"%d",s);
else sprintf(temp,"%d->%d",s,t);
string s_temp(temp);
aim.push_back(s_temp);
s=nums[i];
}
}
t=nums[n-];
char temp[];
if(s==t)
sprintf(temp,"%d",s);
else sprintf(temp,"%d->%d",s,t);
string s_temp(temp);
aim.push_back(s_temp);
return aim;
}
};
228
我想使用to_string函数来着的,但是我的codeblocks一直报错。。。重装系统+VS当做官方IDE已经迫在眉睫了
2017-3-11 leetcode 217 219 228的更多相关文章
- [Leetcode 217&219]寻找数组中的重复值Contains Duplicate I & II
[题目1] Given an array of integers, find if the array contains any duplicates. Your function should re ...
- 2017年11月Dyn365/CRM用户社区活动报名
UG是全球最大Dynamics的用户组织,由最终用户自发组织,由行业有经验的专家自愿贡献知识和经验的非营利机构,与会人员本着务实中立的态度,不进行推介产品,服务以及其他营销行为.在美国,微软Dynam ...
- WPS 表格筛选两列相同数据-完美-2017年11月1日更新
应用: 1.选出A列中的数据是否在B列中出现过: 2.筛选出某一批序号在一个表格里面的位置(整批找出) 3.其实还有其他很多应用,难描述出来... ... A列中有几百的名字,本人想帅选出B列中的名字 ...
- 2017年11月GitHub上最热门的Java项目出炉
2017年11月GitHub上最热门的Java项目出炉~ 一起来看看这些项目你使用过哪些呢? 1分布式 RPC 服务框架 dubbohttps://github.com/alibaba/dubbo S ...
- 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11
Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...
- leetcode——217. 存在重复元素
leetcode--217. 存在重复元素 题目描述:给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 false ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- <LeetCode OJ> 217./219. Contains Duplicate (I / II)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 【LeetCode】219. Contains Duplicate II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...
随机推荐
- 2.0 Linux系统的安装之Fedora安装单系统(2)
2.0 Linux系统的安装之Fedora安装单系统(2) *Linux系统的安装之Fedora安装单系统 恐怕最好装的系统就是Linux系统了,或者与Windows并列.此篇教程为Fedora的单系 ...
- Android Fragment间的广播消息接收
这种方式不用在配置文件加东西,我比较喜欢. 广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里. LocalBroadcastM ...
- 解决无法移除tomcat中的项目
问题:启动myeclipse,tomcat提示报错,blind,但是你移除的时候无法移除,只会显示一个黄色的感叹号,此时你直接在webapp中删除时,也提示呗占用无法删除. 办法:关掉myeclips ...
- WEB笔记-CSS 实现多级导航效果
代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- Arduino LM35温度检测
一. 接线原理图 二.实物图 三.代码例子
- nim游戏解法(转)
转自:http://acm.hdu.edu.cn/forum/read.php?fid=9&tid=10617 取火柴的游戏 题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若 ...
- C# 判断字符串是否左包含
//测试字符串 左包含 //string str = "AAABBBCCC"; //char[] ss = str.ToArray(); //0-8 字符数组 //char[] s ...
- MVC 返回404,返回图片,流到数组,apk信息
return HttpNotFound(); byte[] buffer0 = QRCode(); return File(buffer0, @"image/jpeg"); // ...
- iconfont
查看一些网站代码的过程中,会发现许多的图片是不是背景图片或者<img>,而是类似于下面这样: .iconfont{ font-family:"iconfont" !im ...
- java将父类所有的属性COPY到子类中
public class FatherToChildUtils { /* * 将父类所有的属性COPY到子类中. * 类定义中child一定要extends father: * 而且child和fat ...