题目:在一个字符串中找到第一个只出现一次的字符。如输入 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. robotframework笔记9

    列表和字典 通过专用关键字创建了列表和字典.我们将在这里看到创建的两个例子 ︰ 选择 *** Settings *** Library BuiltIn Library Collections *** ...

  2. CSS3:不可思议的border属性&Web字体图标Font Awesome

     CSS3:不可思议的border属性 转载至——译文:不可思议的CSS border属性 原文:Magic of CSS border property Web字体图标Font Awesome 转载 ...

  3. Eclipse管理Java工程(j2se/j2ee/maven)

    Eclipse管理J2SE/J2EE(Maven)项目 eclipse是一个集成开发工具,有编译,运行,打包部署等功能.eclipse可以新建多种项目,不同的项目有不同的IDE层次结构,方便用户管理资 ...

  4. cf------(round)#1 C. Ancient Berland Circus(几何)

    C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input sta ...

  5. maven的安装与使用

    一.Maven是什么 Maven是一个采用纯Java编写的开 源项目管理工具.Maven采用了一种被称之为project object model (POM)概念来管理项目,所有的项目配置信息都被定义 ...

  6. javascript 倒计时获取验证码

    var wait=60;function reSendCode(id) { var obj = $("#"+id); if (wait == 0) { obj.attr(" ...

  7. sharepoint2010无法创建网站集

    出现以上错误,查看IIS中有关Sharepoint的网站中的“身份验证”中ASP.Net模拟是否为禁用,如果为禁用,请启用即可.

  8. bzoj 2037: [Sdoi2008]Sue的小球

    #include<cstdio> #include<iostream> #include<algorithm> using namespace std; struc ...

  9. [Elasticsearch] 多字段搜索 (三) - multi_match查询和多数字段 <译>

    multi_match查询 multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询. NOTE 存在几种类型的multi_match查询,其中的3种正好和在“了解你的数据”一节中提 ...

  10. numpy 总结

    1.array.sum() from numpy import * import operator group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) ...