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的更多相关文章

  1. [Leetcode 217&219]寻找数组中的重复值Contains Duplicate I & II

    [题目1] Given an array of integers, find if the array contains any duplicates. Your function should re ...

  2. 2017年11月Dyn365/CRM用户社区活动报名

    UG是全球最大Dynamics的用户组织,由最终用户自发组织,由行业有经验的专家自愿贡献知识和经验的非营利机构,与会人员本着务实中立的态度,不进行推介产品,服务以及其他营销行为.在美国,微软Dynam ...

  3. WPS 表格筛选两列相同数据-完美-2017年11月1日更新

    应用: 1.选出A列中的数据是否在B列中出现过: 2.筛选出某一批序号在一个表格里面的位置(整批找出) 3.其实还有其他很多应用,难描述出来... ... A列中有几百的名字,本人想帅选出B列中的名字 ...

  4. 2017年11月GitHub上最热门的Java项目出炉

    2017年11月GitHub上最热门的Java项目出炉~ 一起来看看这些项目你使用过哪些呢? 1分布式 RPC 服务框架 dubbohttps://github.com/alibaba/dubbo S ...

  5. 【主席树维护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 ...

  6. leetcode——217. 存在重复元素

    leetcode--217. 存在重复元素 题目描述:给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 false ...

  7. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  8. &lt;LeetCode OJ&gt; 217./219. Contains Duplicate (I / II)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  9. 【LeetCode】219. Contains Duplicate II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...

随机推荐

  1. python3设置打开文件的编码

    f = open(file_path,'r',encoding='utf8') 用起来很方便,不需要先读取再转码了.

  2. 【sqli-labs】 less7 GET - Dump into outfile - String (导出文件GET字符型注入)

    使用此漏洞需要知道WEB程序的根路径并且MYSQL的secure-file-priv配置应为可写入该路径 添加单引号,发现错误被屏蔽 对应的SQL语句应为 ')) ... 字段还是3个 在数据库中执行 ...

  3. 团体程序设计天梯赛-练习集-L1-048. 矩阵A乘以B

    L1-048. 矩阵A乘以B 给定两个矩阵A和B,要求你计算它们的乘积矩阵AB.需要注意的是,只有规模匹配的矩阵才可以相乘.即若A有Ra行.Ca列,B有Rb行.Cb列,则只有Ca与Rb相等时,两个矩阵 ...

  4. BZOJ 4817: [Sdoi2017]树点涂色 LCT+Access的性质+DFS序+线段树

    Code: #include<bits/stdc++.h> #define maxn 200003 #define inf -1000000 using namespace std; vo ...

  5. 【剑指Offer】17、树的子结构

      题目描述:   输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构)   解题思路:   要查找树A中是否存在和树B结构一样的子树,我们可以分为两步:第一步, ...

  6. swift-UITableView的基本使用

    废话不多说了,直接贴我今天写的代码吧:如果新手有什么不懂的,可以发我邮箱. // //  singleInfo.swift            个人信息 //  Housekeeper // //  ...

  7. Linux思维导图之查找命令

    常用查找命令的区别:

  8. 35.multi-index和multi-type搜索模式

        一.multi-index和multi-type搜索模式 /_search:所有索引,所有type下的所有数据都搜索出来 /index1/_search:指定一个index,搜索其下所有typ ...

  9. 安装配置开源的laravel项目到本地环境

    前言 从https://github.com 上down了一个laravel项目安装到本地环境的时候,其中遇到一些问题,这里梳理并记录下整个流程. git上下载项目代码,部署laravel项目的时候会 ...

  10. python实现RGB转化为灰度图像

    问题: 我正尝试使用matplotlib读取RGB图像并将其转换为灰度.在matlab中,我使用这个: 1 img = rgb2gray(imread('image.png')); 在matplotl ...