Interview----First single charactor
题目:在一个字符串中找到第一个只出现一次的字符。如输入 abaccdeff,则输出 b。
分析:这道题是 2006 年 google 的一道笔试题。
分析:
用 Hash, 时间和空间复杂度是 O(N)
当然,如果字符是 ASCII 编码的话,可以开一个 256长的数组来对每个字符出现的次数进行记录。
下面的代码采用了 unordered_map, 用 HASH 实现。。
有几点注意的地方:
1. unordered_map[key] ,返回一个 reference, 指向键值是 key 的元素。
如果该元素尚未存在,则自动插入这元素,value 值调用默认构造函数。对于那些没有默认构造函数的一定要注意。
// copyright @ L.J.SHOU Mar.10, 2014
// find 1st single number
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std; void FirstSingleNumber(const string& str)
{
if(str.empty()) return;
unordered_map<char, int> char_set; for(int i=0; i<str.size(); ++i)
++ char_set[str[i]]; for(int i=0; i<str.size(); ++i)
{
// char_set[i] will automatically insert i if i not existed
// value is set by default constructor
if(char_set[str[i]] == 1)
{
cout << str[i] << endl;
return;
}
}
} int main(void)
{
string str("abaccdeff"); FirstSingleNumber(str); return 0;
}
Interview----First single charactor的更多相关文章
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】
http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF ...
- Java Swing interview
http://www.careerride.com/Swing-AWT-Interview-Questions.aspx Swing interview questions and answers ...
- WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】
WCF Interview Questions – Part 4 This WCF service tutorial is part-4 in series of WCF Interview Qu ...
- [转]Design Pattern Interview Questions - Part 3
State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- [LintCode] Single Number 单独的数字
Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...
- Recruit Coupon Purchase Winner's Interview: 2nd place, Halla Yang
Recruit Coupon Purchase Winner's Interview: 2nd place, Halla Yang Recruit Ponpare is Japan's leading ...
- Bungie Interview with Halo3 Developer
http://www.realtimerendering.com/blog/tag/bungie/ Digital Foundry interview with Halo: Reach develop ...
随机推荐
- js 数组的判断
<javascript语言精粹>中的 var is_array = function(value){ return value && //判断值是否为真,不接受null和其 ...
- static与C#中的static
Static 1.静态方法与非静态方法 a.静态方法的调用类.静态方法名([参数列表]) 非静态方法的调用类 对象 = new 类的构造函数([参数列表])对象.非静态方法名([参数列表]) 静态方法 ...
- Metro UI(Win 8风格)页面设计小记
一.Metro风格菜单——简单 HTML <div class="pagina "> <div class="linha"> <d ...
- juery 选择器 选择多个元素
使用,号: $("#goodSource,#mailState") 选择了id为goodSource或者mailState的元素,当两者之间有任何一个有改变时,将会触发该操作. / ...
- Caffe 深度学习框架介绍
转自:http://suanfazu.com/t/caffe/281 Caffe是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的贾扬清,目前在Google工作. Caffe是 ...
- FaceBook Twitter实习生简历求内推
写在博客里面吧. 有一个朋友,男,博士在读,研究方向为图像处理,计算机视觉相关. 想在在读期间有一些海外实习经历.不知道哪位博友,有相关的人脉,求内推啊.内推成功的话请吃大餐,哈哈!
- 使用nexus搭建maven仓库(本地私服)
我们在使用maven的时候,对于项目所依赖的jar包,maven默认会在中央仓库下载jar包,到本地的磁盘目录(如果没有配置则是用户目录下/.m2/repository文件夹下).如果公司内部搭了一个 ...
- .NET开发知识体系
记得几年前写过一篇关于.NET开发方面的知识总结,但是随着技术的发展以及自己技术理解的提升,觉得有必要对那篇文章加以更新和完善. 最近在园子里也看到有人写关于.NET知识体系的文章,特别是灵感之源写的 ...
- sqlserver 2008 左补齐字符串
SQLServer:right函数 语法 Right(string, length) Right 函数的语法具有下面的命名参数: 部分 说明 string 必要参数.字符串表达式,从中最右边的 ...
- jdk和tomcat环境部署
部署前需要准备的东西: 1.确定操作系统(32位或64位) 2.准备对应的jdk和tomcat软件 3.准备一份环境变量配置说明,省的到时候忘记了 步骤: 1.安装JDK 安装好JDK后,再配置JDK ...