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. SimpleProfile_GetParameter && SimpleProfile_SetParameter

    /********************************************************************* * @fn SimpleProfile_GetParame ...

  2. MySQL优化之Explain命令解读

    简述: explain为mysql提供语句的执行计划信息.可以应用在select.delete.insert.update和place语句上.explain的执行计划,只是作为语句执行过程的一个参考, ...

  3. Oracle中转义下划线

    原意是查询出所有的月粒度模型,但是在oracle中,下划线也代表匹配单一任何字符,导致15分钟粒度的模型也被查询出来,在此,需要对下划线做转义,使其只表示下划线的含义,可以使用ESCAPE()函数. ...

  4. Swift _ OC _ 混编

    Swift _ OC _ 混编 在OC环境下使用Swift. GitHub源码

  5. cop2000实现补码两位乘

    程序地址 机器码 反汇编语言 指令说明 ;IN 可以使用此指令在cop2000上输入数据 00 7C4B MOV A,#4BH 模拟输入X补 02 80 MOV R0,A 放入R0 03 88F9 M ...

  6. Eclipse切换字体颜色

    打开window-preferences

  7. canvas+javascript实现淘宝商品放大镜效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. company.scss

    .company{ @extend .layout; width:100%; h3{ display: block; margin: 20px 0; text-align: left; } .comp ...

  9. python中mysql主从同步配置的方法

    1)安装mysql ubuntu中安装一台mysql了,docker安装另外一台mysql 获取mysql的镜像,主从同步尽量保证多台mysql的版本相同,我的ubuntu中存在的mysql是5.7. ...

  10. Hive(7)-基本查询语句

    一. 表和数据准备 1. 数据地址 链接:https://pan.baidu.com/s/1crr8B9bD_0Phfm99vLCWjg  提取码:5jzw 2. 建表语句 create table ...