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. php导出word格式数据的代码(转)

    本节内容:一个php导出文档的类 例子:<?php /*** 生成word文档的类* by www.jbxue.com*/class word{     function start()    ...

  2. 用js写三个数,让三个数从小到大排列

    console.log('请输入三个数:'); let num1 = readline.question() - 0; let num2 = readline.question() - 0; let ...

  3. Python 学习笔记(九)Python元组和字典(三)

    字典常用方法 copy() 返回一个字典的浅复制 示例:浅拷贝d.copy() 深拷贝引入import copy   copy.deepcopy() >>> help(dict.co ...

  4. 【Django笔记三】Django2.0配置mysql模型

    一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53   安装mysql 二.安装Mysqlclient: 1. ...

  5. activemq整合springboot使用(个人微信小程序用)

    1.引入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  6. 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)

    Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...

  7. 复习宝典之SpringMVC

    查看更多宝典,请点击<金三银四,你的专属面试宝典> 第七章:SpringMVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(co ...

  8. linux 学习第九天

    一.磁盘 (FHS:Filesystem Hierarchy Standard(文件系统层次化标准)的缩写) 1.常用目录 /var  主要存放经常变化的文件,如日志 /usr/local  用户自行 ...

  9. 5 替换空格 JavaScript

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy.   原来一个空格字符,替换之后 ...

  10. SHOPEX快递物流单号查询插件

    本SHOPEX快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...