今天开始刷刷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的更多相关文章

  1. Codility经典算法题之九:MissingInteger

    Task description: This is a demo task. Write a function: that, given an array A of N integers, retur ...

  2. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  3. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  4. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  5. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  6. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  7. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  8. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  9. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

随机推荐

  1. PHP获取时间日期的多种方法

    分享下PHP获取时间日期的多种方法. <?php echo "今天:".date("Y-m-d")."<br>";     ...

  2. 伴随ListView、RecyclerView、ScrollView滚动滑入滑出小图标--第三方开源--FloatingActionButton

    FloatingActionButton在github上的项目主页是:https://github.com/makovkastar/FloatingActionButton 它的依赖包NineOldA ...

  3. LeapMotion(2):追踪五指

    上一篇文章,我们实现了Leap Motion的简单测试.追踪其中一个手指并用红色圆形表示其在空间的位置. 这篇文章,我们来实现五指的追踪. 其实,能够实现一指的追踪,那么五指的追踪自然不成问题.但是, ...

  4. python之range(), xrange()

    可以这样理解: range()立即执行,返回结果 xrange()延迟执行,需要时再返回结果.

  5. yii2怎样写规则可以隐藏url地址里的控制器名字

    yii2怎样写规则可以隐藏url地址里的控制器名字,例如现在的是***.com/site/index.html要变成***.com/index.html '<action:index>.h ...

  6. 纯JS文本比较工具

    前段时间由于工作需要写了一个纯JS文本比较工具 在这里与大家分享下 算法有待优化,还希望大家多多指教 先上效果图: 奉上源码(把源码保存为html格式的文件就可以直接运行了): <!doctyp ...

  7. Android之EditText

    EditText 属性介绍: maxLength:设置最大输入字符数. hint:设置空白提示文字. textColorHint:设置空白提示文字的颜色. enabled:设置是否可编辑(可以获得焦点 ...

  8. 深入理解ThreadLocal(二)

    3 InheritableThreadLocal的使用 通过上面的分析知道通过ThreadLocal保存的值是线程隔离的.其实在Thread对象中,还有一个ThreadLocal.ThreadLoca ...

  9. 【BZOJ 1007】 [HNOI2008]水平可见直线

    Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.    例如,对于直线:    ...

  10. 使用ASP.NET注册工具aspnet_regiis.exe注册IIS

    该工具的名称为aspnet_regiis.exe,在32位机上,该工具存在于C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727,在64位机中“Framework ...