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. hdu 2098 分拆素数和(一个偶数拆分成两个不同素数和 拆法数量)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2098 分拆素数和 Time Limit: 1000/1000 MS (Java/Others)     ...

  2. Subnetting

    Subnet Addressing To better utilize IP address Subnet addressing introduces another hierarchical(分层) ...

  3. python 使用gRPC

    Python gRPC 简介 grpc 是google 开源的一款rpc服务框架,可以轻松的实现跨语言的微服务,将项目中的各个模块独立出来,单独部署,独立升级,也可以根据模块的情况进行不同语言的变成. ...

  4. iOS 类似美团或饿了么评价中的星星评分控件

    1.做的好几个项目都用到了评分控件,可以用来展示评分,也可以用来写评分,图片和间距大小都可以定制,之前就已经简单封装了一个,现在把它分享出来,有需要的拿去用. 2.下面是展示截图:   image.p ...

  5. 高性能mysql:创建高性能的索引

    本文系阅读<高性能MySQL>,Baron Schwartz等著一书中第五章 创建高性能的索引的笔记,索引是存储引擎用于快速找到记录的一种数据结构. 索引对于良好的性能非常关键,尤其是当表 ...

  6. Filebeat使用模块收集日志

    1.先决条件 在运行Filebeat模块之前: 安装并配置Elastic stack 完成Filebeat的安装 检查Elasticsearch和Kibana是否正在运行,以及Elasticsearc ...

  7. 04JavaScript语法

    1.JavaScript 语法 JavaScript 是一个脚本语言. 它是一个轻量级,但功能强大的编程语言 2.JavaScript 字面量 在编程语言中,一般固定值称为字面量,如 3.14. 数字 ...

  8. 理解Redux以及如何在项目中的使用

    今天我们来聊聊Redux,这篇文章是一个进阶的文章,建议大家先对redux的基础有一定的了解,在这里给大家推荐一下阮一峰老师的文章: http://www.ruanyifeng.com/blog/20 ...

  9. laravel 安装添加多站点

    官方文档如下 https://learnku.com/laravel/t/1160/laravel-nginx-multi-site-configuration

  10. day 21继承

    1.了解Python2和python3类的区别:   python2.3之前使用的是经典类, 在2.3版本之后组,使用的是新式类 MRO: method resolution order  方法的查找 ...