First Unique Number In Stream

描述:

Given a continuous stream of numbers, write a function that returns the first unique number whenever terminating number is reached(include terminating number). If there no unique number before terminating number or you can't find this terminating number, return -1.

您在真实的面试中是否遇到过这个题?

Yes
样例

Given a stream [1, 2, 2, 1, 3, 4, 4, 5, 6] and a number 5
return 3

Given a stream [1, 2, 2, 1, 3, 4, 4, 5, 6] and a number 7
return -1

代码:

 class Solution {
public:
/*
* @param : a continuous stream of numbers
* @param : a number
* @return: returns the first unique number
*/
int firstUniqueNumber(vector<int> nums, int number) {
// Write your code here
bool flag = false;
int pos = ; //用来记录number的位置
for (int i = ; i < nums.size(); i++) {
pos++;
if (number == nums[i]) {
flag = true;
break;
}
}
if (flag == false) return -; map<int, int> s;
for (int i = ; i < pos; i++) {
s[nums[i]]++;
}
for (int i = ; i < pos; i++) {
if (s[nums[i]] == ) {
return nums[i];
}
}
return -;
}
};

lintcode First Unique Number In Stream的更多相关文章

  1. LoadRunner压力测试之Unique Number参数类型、Random Number参数类型浅析

    前几天工作需要用LoadRunner进行压力测试,期间对手机号进行参数化设置. 当时选用了<Value>137{Random_quhao}{Unique_weiyi}</Value& ...

  2. mysql生成不重复随机数(unique number generation)

    转自:http://blog.csdn.net/dreamer2020/article/details/52049629 问题来源 业务中有时会遇到要生成不重复随机数的情况,例如,新生成一个商品编号. ...

  3. 【leetcode】1207. Unique Number of Occurrences

    题目如下: Given an array of integers arr, write a function that returns true if and only if the number o ...

  4. [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字

    Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...

  5. [LintCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  6. [LintCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  7. Lintcode: Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  8. Lintcode: Kth Smallest Number in Sorted Matrix

    Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...

  9. Lintcode: Kth Prime Number (Original Name: Ugly Number)

    Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...

随机推荐

  1. 利用来JS控制页面控件显示和隐藏有两种方法

    利用来JS控制页面控件显示和隐藏有两种方法,两种方法分别利用HTML的style中的两个属性,两种方法的不同之处在于控件隐藏后是否还在页面上占空位. 方法一:  1 2 document.getEle ...

  2. iOS中break、continue、return三者的区别

    iOS中break.continue.return三者的区别 1. break         直接跳出当前层次的循环. 如果嵌套循环如for中嵌套while,break只能跳出内层的while循环, ...

  3. 淘宝前端框架kissyui

    http://docs.kissyui.com/1.4/docs/html/guideline/why-kissy.html

  4. 关于sharepoint如何做SSO,如何做OOS监视编辑

    应客户需求,需要做sharepoint SSO,以前都是默认的AD验证,如果客户已经有一套SSO系统,验证过SSO之后就能自动登录,而不是浏览器上设置保存用户名密码的AD登陆. 怎么做呢? 首先sha ...

  5. 基于多用户的Oracle数据泵导入导出数据

    登陆SqlPlus: SqlPlus sys/syspwd@MyOrcl AS sysdba 其中:syspwd:sys的登陆密码:MyOrcl:所创建的数据库服务名. 创建数据泵: create o ...

  6. linux 学习第七天

    一.bash 使用(for循环.while循环) 1.1.批量添加用户 1.2.查看用户是否存在 A.cut -d : -f 1 /etc/passwd B.id dream  (id 用户名称) C ...

  7. Docker 相关命令汇总

    操作容器的命令 镜像中的容器启动之后可以在 docker 中操作和查看容器的信息 l   docker ps 查看运行的容器,如果想查看全部加上参数-a 即可 l   docker create 完整 ...

  8. vue的实例

    vue的实例 创建一个vue实例的写法和创建一个变量一样 var vm = new Vue{{ //我们一般用vm来接收vue的实例,vm是 ViewModel的缩写 }} 然后,我们就可以给vue实 ...

  9. Canvas路径方向

    使用Canvas路径画图需要注意方向,画图方向是顺时针还是逆时针需要记住.下面让我们看看Canvas常见路径方向. arc 参数值 context.arc(x,y,r,sAngle,eAngle,co ...

  10. go加密算法:CBC对称加密(一)--DES

    package main import ( "bytes" //"crypto/aes" "crypto/cipher" "cry ...