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 ...
随机推荐
- Python 学习笔记(十一)Python语句(二)
For 循环语句 基础知识 for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for 循环规则: do sth >>> for i in "python ...
- 使用nuget过程中一些问题总结
更新System.Web.Http组件以及其相关依赖项使用以下命令更新: Update-Package Microsoft.AspNet.WebApi –reinstall 如果没有这个引用,则先添加 ...
- 1.Redis介绍以及安装
Redis介绍 Redis是一个开源的(BSD开源协议),内存数据结构存储,被用于作为数据库,缓存和消息代理. Redis支持如下数据结构: string(字符串) hashes(哈希) lists ...
- app后端api设计【转】
博客:https://blog.csdn.net/newjueqi/article/details/44037011 app和后端的交互,一般都是通过后端提供的api实现.api的设计,估计很多刚进入 ...
- 【memcached启动报错】
#前台启动不了 #指定-u root #后台启动 #扩展选项: #利用telnet连接memcached 的端口登录memcached服务 #error表示有语法错误 #store表示正确
- canvas常用属性方法由浅下沉
首先引入<canvas></canvas>标签就不必说了. 其次就是得到canvas的2d环境了( var ctx = canvasDom.getContext('2d') ) ...
- 学习新框架laravel4 第三天
请求与输入 获取请求参数 如果没有传递默认值位1 $id= Input::get('id',1); //获取所有请求内容 Input::all() 取得请求 URI $uri = Request::p ...
- 【Hbase三】Java,python操作Hbase
Java,python操作Hbase 操作Hbase python操作Hbase 安装Thrift之前所需准备 安装Thrift 产生针对Python的Hbase的API 启动Thrift服务 执行p ...
- Python知乎热门话题爬取
本例子是参考崔老师的Python3网络爬虫开发实战写的 看网页界面: 热门话题都在 explore-feed feed-item的div里面 源码如下: import requests from py ...
- TensorFlow实现线性回归
from __future__ import print_function import tensorflow as tf import numpy import matplotlib.pyplot ...