*[codility]MissingInteger
今天开始刷刷codility上respectable的题目,难度适中。
https://codility.com/demo/take-sample-test/missing_integer
本题是找出数组里第一个非负的整数,要求复杂度O(n)。那么首先想到的做法是排序,但这样负责度就上去了。要从nlogn降到n,常见的一个做法是用hashtable,这里就可以用set记录。要注意的是全负的情况,所以这里用了maxVal=0作为初值。
#include <unordered_set>
using namespace std; int solution(vector<int> &A) {
// write your code in C++11
unordered_set<int> positives;
int maxVal = 0;
for (int i = 0; i < A.size(); i++)
{
if (A[i] > 0 && positives.find(A[i]) == positives.end())
{
positives.insert(A[i]);
if (A[i] > maxVal)
maxVal = A[i];
}
}
if (positives.size() == maxVal)
return maxVal+1;
for (int i = 1; i < maxVal; i++)
{
if (positives.find(i) == positives.end())
{
return i;
}
}
}
*[codility]MissingInteger的更多相关文章
- Codility经典算法题之九:MissingInteger
Task description: This is a demo task. Write a function: that, given an array A of N integers, retur ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
随机推荐
- javascript Date 函数的坑
Javascrip中对日期的解析.格式化简直是混乱不堪,各种的坑,攻城狮们多小心. 1. 不要用 Date("yyyy-mm-dd") 构造函数解析字符串. IE.firefo ...
- emmet(Zen coding)帮助文档
葵花宝典终可成,半途而废万事空. 官方地址:http://docs.emmet.io/cheat-sheet/ 我导出了pdf版,需要的同学可以下载: 链接:http://pan.baidu.com/ ...
- sqlserver中distinct的用法(不重复的记录)
下面先来看看例子: table表 字段1 字段2 id name 1 a 2 b 3 c 4 ...
- sql 对一张表进行按照不同条件进行多次统计
最近一直在做数据统计,在此过程中,遇到过好多种情况都是对一张表按照不同的条件进行多次统计,以前的做法是统计几次按照不同的条件left join 几次,虽然也能得到想要的结果,但是效率太低,反映在页面就 ...
- Quartz.net 的开源任务管理平台
Quartz.net 的开源任务管理平台 前面总结了很多,关于Quartz.net 的文章,介绍了如何使用Quartz.net.不清楚的朋友,可以看我之前的系列文章,http://www.cnblog ...
- SqlServer里DateTime转字符串
Select CONVERT(varchar(100), GETDATE(), 8):14:53:14 Select CONVERT(varchar(100), GETDATE(), 9): 06 ...
- python学习小结1:for循环控制语句
用一个列表来确定for循环的范围 >>> x = [0,1,2,3,4] >>> for i in x: print i, 0 1 2 3 4 循环一个字符串 & ...
- 【软件工程-Teamwork 3】团队角色分配和团队贡献分分配规则
Part 1 团队角色分配 1.人员分配概要: Project Manager:1名 / Developer:4名 / Test: 1名 2.具体人员分配及职责: Project Manager(PM ...
- 1891: 丘比特的烦恼 - BZOJ
Description 随着社会的不断发展,人与人之间的感情越来越功利化.最近,爱神丘比特发现,爱情也已不再是完全纯洁的了.这使得丘比特很是苦恼,他越来越难找到合适的男女,并向他们射去丘比特之箭.于是 ...
- c++ std::string 用法
std::string用法总结 在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询.在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(co ...