今天开始刷刷codility上respectable的题目,难度适中。

https://codility.com/demo/take-sample-test/missing_integer

本题是找出数组里第一个非负的整数,要求复杂度O(n)。那么首先想到的做法是排序,但这样负责度就上去了。要从nlogn降到n,常见的一个做法是用hashtable,这里就可以用set记录。要注意的是全负的情况,所以这里用了maxVal=0作为初值。

  1. #include <unordered_set>
  2. using namespace std;
  3.  
  4. int solution(vector<int> &A) {
  5. // write your code in C++11
  6. unordered_set<int> positives;
  7. int maxVal = 0;
  8. for (int i = 0; i < A.size(); i++)
  9. {
  10. if (A[i] > 0 && positives.find(A[i]) == positives.end())
  11. {
  12. positives.insert(A[i]);
  13. if (A[i] > maxVal)
  14. maxVal = A[i];
  15. }
  16. }
  17. if (positives.size() == maxVal)
  18. return maxVal+1;
  19. for (int i = 1; i < maxVal; i++)
  20. {
  21. if (positives.find(i) == positives.end())
  22. {
  23. return i;
  24. }
  25. }
  26. }

  

*[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. silverlight视频、音频

    几天发现MediaElement播放不了wav格式的音频文件,在网上找到一篇解决的文章: http://www.cnblogs.com/rupeng/archive/2011/02/20/195936 ...

  2. 深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复

    在某些参考资料中是说这两个方法任选其一就可,但经我测试,必须两个方法同时使用,才不会再出现错误提示 PHP Warning: strtotime(): It is not safe to rely o ...

  3. (转)android底部弹出iOS7风格对话选项框(QQ对话框)--第三方开源--IOS_Dialog_Library

    本文转载于:http://blog.csdn.net/zhangphil/article/details/44940339 完成这个效果的是使用了 IOS_Dialog_Library 下载地址:ht ...

  4. .NET SDK和下载

    http://blogs.msdn.com/b/dotnet/p/dotnet_sdks.aspx .NET SDK和下载 您可以通过下载.NET框架针对包和软件开发工具包,并使用它们与Visual ...

  5. 1. opencv的初体验

    http://guoming.me/opencv-config  这篇文章有讲解opencv的安装与配置 一些常用库 opencv_core249d.lib opencv_imgproc249d.li ...

  6. C# 缓存学习总结

    昨天整理了一下缓存的基本用法,和缓存依赖类 CacheDependency类的使用,今天整理一下缓存的数据库依赖类SqlCacheDependency 1.数据库依赖类SqlCacheDependen ...

  7. 1017. Queueing at Bank (25)

    Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...

  8. WPF中的Style

    一.Style基础知识 构成Style最重要的两种元素是Setter和Trigger Setter类帮助我们设置控件的静态外观风格 Trigger类帮助我们设置控件的行为风格 Setter类的Prop ...

  9. C++11右值引用,移动主义

    理解1: 左值和右值针对等号而言, 等号左边称为左值, 等号右连称为右值. 理解2: 左值和右值针对表达式而言, 表达式结束后依然存在的持久对象称为左值, 表达式结束后不存在的持久对象称为右值. 理解 ...

  10. NodeJS -Express 4.0 用include取代partial

    在Express 4.0 下按如下方法设置: (1)运行cmd 输入:npm install express-partials -g (2)下载成功后.在app.js 中引用此插件   var par ...