题目:在一个字符串中找到第一个只出现一次的字符。如输入 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的更多相关文章

  1. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

  2. WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】

    http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF ...

  3. Java Swing interview

    http://www.careerride.com/Swing-AWT-Interview-Questions.aspx   Swing interview questions and answers ...

  4. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  5. [转]Design Pattern Interview Questions - Part 3

    State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...

  6. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  7. [LintCode] Single Number 单独的数字

    Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...

  8. 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 ...

  9. Bungie Interview with Halo3 Developer

    http://www.realtimerendering.com/blog/tag/bungie/ Digital Foundry interview with Halo: Reach develop ...

随机推荐

  1. 重装系统(win7)

    -_-|| 第一次装,可谓艰难险阻 一:准备 1.U盘(大小至少4G)——本人用了4G的 2.查询自己的电脑进入BIOS的方法——本人电脑机型为华硕X550VC,开机长按F2(当时为保险起见,也同时按 ...

  2. uva 1639--精度处理方法之取对数(uva 1639)

    1639 - Candy Time limit: 3.000 seconds 1639 CandyLazyChild is a lazy child who likes candy very much ...

  3. excel快捷键设置

    Excel技能 按键 结果 序号 Alt+1 合并后居中 1 ALT+2 边框 2 ALT+3 边框线型 3 ALT+4 格式刷 4 ALT+5 清除格式 5 CTRL+SHIFT+P 设置字体 6 ...

  4. input固定定位后,当input框获取到焦点时,会离开手机软键盘的解决方法

    前些天做页面时候遇到这样一个问题,将input框用position:fixed固定定位在最底部的时候,当Input获取焦点的时候,input框离开了手机软键盘,而不是吸附在软键盘上,效果如下图: 找了 ...

  5. shell学习记录002-知识点储备

    1.echo "4*0.33" |bc    #计算机功能的运用 [root@oc3408554812 shell]# ss=22; [root@oc3408554812 shel ...

  6. 水水更健康~~~~~~~~~~~~~~~AutoRun免疫的原理

    免疫AutoRun病毒的原理建立在一个无法删除的AutoRun.inf文件夹,以防止病毒生成用来运行的AutoRun.inf文件 打开命令提示符 输入: 1.cd \2.mkdir autorun.i ...

  7. Asp.Net MVC3.0网站统计登录认证的在线人数

    Asp.Net MVC3.0网站统计登录认证的在线人数 前言 对于一个网站来说,统计在线人数是一个很重要的工作.平时也发现很多的网站论坛等都有在线人数的显示.对于一个网站如果在线人数很多,用户看到了这 ...

  8. PHP后台

    一.ajax提交表单 先引入ajax.js function ajax(url, fnSucc, fnFaild) { //1.创建Ajax对象 var oAjax=null; if(window.X ...

  9. NSDateFormatter 根据时间戳求出时间

    NSDateFormatter 根据时间戳求出时间 - (void)detailWithStyle:(NSString*)style time:(NSInteger)time { // NSStrin ...

  10. bzoj 1854: [Scoi2010]游戏

    #include<cstdio> #include<iostream> #include<cstring> #define M 2000008 using name ...