lintcode First Unique Number In Stream
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.
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的更多相关文章
- LoadRunner压力测试之Unique Number参数类型、Random Number参数类型浅析
前几天工作需要用LoadRunner进行压力测试,期间对手机号进行参数化设置. 当时选用了<Value>137{Random_quhao}{Unique_weiyi}</Value& ...
- mysql生成不重复随机数(unique number generation)
转自:http://blog.csdn.net/dreamer2020/article/details/52049629 问题来源 业务中有时会遇到要生成不重复随机数的情况,例如,新生成一个商品编号. ...
- 【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 ...
- [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 ...
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [LintCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 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. ...
- 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: [ ...
- 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 ...
随机推荐
- selenium处理table表格
在UI自动化测试中经常会遇到表格的处理,下面是一点心得. 假设网页页面有一个表格,如何获取这个table的指定cell的值?你会说我们可以根据xpath定位到这个cell的行列,然后getText() ...
- 锐捷交换机实验案例:vlan间互访的配置与验证
组网需求: 1.如下图所示,某用户内网被划分为VLAN 10.VLAN 20.VLAN 30,以实现相互间的2 层隔离: 2.3 个VLAN 对应的IP 子网分别为192.168.10.0/24 .1 ...
- DQL-子查询
一:含义 嵌套在其他语句内部的select语句称之为子查询或内查询 外套的语句还可以是 insert,update,delete,一般用select比较多 外面如果是select语句,我们称之为外查询 ...
- Mybatis 原始dao CRUD方法
用到的相关jar包及所用版本如下: 其中的Mybatis可以到github.com的网站下载 <project xmlns="http://maven.apache.org/POM/4 ...
- 外键参数 onupdate,ondelete等(cascade,no adcion,set null,restrict)
MySQL外键约束On Delete.On Update各取值的含义 先看On Delete属性,可能取值如上图为:No Action, Cascade,Set Null, Restrict属性. 当 ...
- layui 图片与表单一起提交 + layer.photos图片层预览
HTML基本结构: <form class="layui-form" action="" id="feedBackForm"> ...
- Python豆瓣源
pip install -i https://pypi.doubanio.com/simple/ xxxx
- Linux上搭建svn资源库
一.安装 centos上安装 使用命令svn --version查看是否安装过svn: 如果出现 bash: svn: command not found 则显示没有安装 可以使用 yum in ...
- 1.Python是什么
前言 这里只是根据个人的理解而谈,庸俗浅薄,不是科学定义,也可以认为是假装自己理解啦,掩耳盗铃罢了.知无涯是多么的恐怖,哈哈 计算机语言 此处的语言不同于我们生活中所说的语言,因为生活中的语言 ...
- Django学习之模拟架构页面跳转
背景知识,需要有一定量的HTTP基础知识 在客户端游览器通过URL向服务端发送请求时,经历了两次过程.一次是URL向服务端发起请求,一次是服务端向客户端回发响应. 由图可知,客户端一共传递两个信息,一 ...