题目:在一个字符串中找到第一个只出现一次的字符。如输入 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. Hadoop-env.sh[翻译]

    说明: 某天 ,把hadoop-env.sh的注释看了看 , 感觉受益匪浅,于是想要写一篇告诉大家,文档是最靠谱的,鉴于我的水平有限,只能翻译大概,切勿吐槽,提建议请留言 摘要: 1.这个文件中只有J ...

  2. Unique Binary Search Trees [LeetCode]

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. js字符串处理

    1.获取字符串实际长度 var jmz = {}; function strlen(str) { ///<summary>获得字符串实际长度,中文2,英文1</summary> ...

  4. redhat enterprixe 5.0 NFS服务配置与管理

    一.了解NFS Samba 是主要用于实现Linux和Windows操作系统之间文件共享的协议,而NFS则是实现UNIX和Linux操作系统之间文件共享的协议. NFS可以把网络上远程的文件挂载到本机 ...

  5. mysql linux备份shell

    #!/bin/bash# export and backup the activity.sql  mysqldump  -uname -password activity --skip-lock-ta ...

  6. HTML5表单新增属性

    1.form 原来html里面,表单里的元素应该包裹在表单里,如 <form action="login.php" method="get"> &l ...

  7. Phonegap hello world 不容易啊~!

    今天一个项目要用phonegap,当初就是觉得phonegap配置太tmd的麻烦了,所以转头appcan,但今天项目必须用-- 先是看到官方说用nodejs装,tmd的,总是重复同一个错误,安装不起, ...

  8. 仿淘宝js图片切换

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. windows-docker开发我常用命令 docker-machine ssh default

    docker-machine ssh default  docker logs test sudo systemctl start docker       docker tag IMAGEID ne ...

  10. 线程系列4---sleep()和wait()方法区别

    2013-12-25 14:49:00 1. sleep()方法是Thread类的一个静态方法,可以在任意地方被调用,而wait()方法是object类的一个方法,只能在同步代码块或者同步方法里面,通 ...