C++版 - 剑指Offer 面试题35:第一个只出现一次的字符 解题报告(华为OJ034-找出字符串中第一个只出现一次的字符)
面试题35:第一个只出现一次的字符
题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。(2006年google的一道笔试题。)
分析:
首先应向确认一下是ASCII字符串,而不是Unicode字符串。用hash表求解即可,由于需要先遍历一次,时间复杂度为O(n),空间复杂度为O(1) (256个ASCII字符).
满足题意的代码如下:
#include<cstdio>
#include<string>
#include<unordered_map>
using namespace std;
class Solution {
public:
char FirstNotRepeatingChar(string str) {
if(str.size() == 0) return '\0';
unordered_map<char, int> countMap; // 使用C++中的map的insert时,返回结果是自动按key排序(增序)后的结果
for(int i = 0;i < str.size();i++){
if(countMap.find(str[i]) == countMap.end())
countMap[str[i]]=1;
// countMap.insert({str[i], 1}); // 映射表中没找到相关记录,加到映射表,次数设置为1
else countMap[str[i]]++; // 映射表中找到了相关记录,将映射表中的次数+1
}
// int pos = -1;
char ch;
for(int i = 0;i < str.size();i++){
if(countMap[str[i]] == 1){
// pos = i;
ch=str[i];
break;
}
}
return ch;
}
};
// 以下为测试
int main()
{
Solution sol;
string str1="abaccdeff";
string str2="cbacnba";
char res1 = sol.FirstNotRepeatingChar(str1);
char res2 = sol.FirstNotRepeatingChar(str2);
printf("%c\n", res1);
printf("%c\n", res2);
return 0;
}
也可简化为:
#include<cstdio>
#include<string>
#include<unordered_map>
using namespace std;
class Solution {
public:
char FirstNotRepeatingChar(string str) {
int hash[256] = {0};
for(auto c : str){ // 遍历一次,复杂度为O(n)
hash[c] ++;
}
char ch;
for(int i=0;i<str.size();i++){
if(hash[str[i]] == 1){
//return i;
return str[i];
}
}
return '\0'; // if(str.size() == 0) return '\0';
}
};
// 以下为测试
int main()
{
Solution sol;
string str1="ABACCDEFF";
string str2="cbacnba";
char res1 = sol.FirstNotRepeatingChar(str1);
char res2 = sol.FirstNotRepeatingChar(str2);
printf("%c\n", res1);
printf("%c\n", res2);
return 0;
}
相比而言,前一种方法更高效,256个字符可能只出现很少的一部分,后面这种方法在空间上消耗多一点...
九度OJ 提交网址 http://ac.jobdu.com/problem.php?pid=1283
牛客网OJ 改编: 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符的位置。若为空串,返回-1。位置索引从0开始。
提交网址: http://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187
- 输入:
-
一个字符串。
- 输出:
-
输出第一个只出现一次的字符下标,没有只出现一次的字符则输出-1。
- 样例输入:
- ABACCDEFF
AA
- 样例输出:
- 1
-1 - 牛客网 AC代码:
class Solution {
public:
int FirstNotRepeatingChar(string str) {
if(str.size() == 0) return -1;
unordered_map<char, int> countMap; // 使用C++中的map的insert时,返回结果是自动按key排序(增序)后的结果
for(int i = 0;i < str.size();i++){
if(countMap.find(str[i]) == countMap.end())
countMap[str[i]]=1; // 映射表中没找到相关记录,加到映射表,次数设置为1
else countMap[str[i]]++; // 映射表中找到了相关记录,将映射表中的次数+1
}
int pos = -1;
for(int i = 0;i < str.size();i++){
if(countMap[str[i]] == 1){
pos = i;
break;
}
}
return pos;
}
};
精简之后:
class Solution {
public:
int FirstNotRepeatingChar(string str) {
int hash[256] = {0};
for(auto c : str){ // 遍历一次,复杂度为O(n)
hash[c] ++;
}
for(int i=0;i<str.size();i++){
if(hash[str[i]] == 1){
return i;
}
}
return -1; // if(str.size() == 0) return -1;
}
};
华为OJ034-找出字符串中第一个只出现一次的字符
题目描述
找出字符串中第一个只出现一次的字符
接口说明
原型:
char FindChar(InputString);
输入参数:字符串InputString
输出参数:
如果无此字符 请输出该字符;如果无此字符,请输出'.'。
输入描述
输入一串字符
输出描述
输出一个字符
输入例子
asdfasdfo
输出例子
o
AC代码(C++风格):
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
char FirstNotRepeatingChar(string str)
{
if(str.size() == 0) return '.';
unordered_map<char, int> countMap;
for(int i = 0;i < str.size();i++){
if(countMap.find(str[i]) == countMap.end())
countMap[str[i]]=1;
else countMap[str[i]]++; // 映射表中找到了相关记录,将映射表中的次数+1
}
char ch;
for(int i = 0;i < str.size();i++){
if(countMap[str[i]] == 1){
ch=str[i];
break;
}
}
return ch;
}
int main() {
string str;
while(cin>>str) {
char ch=FirstNotRepeatingChar(str);
cout<<ch<<endl;
}
return 0;
}
或
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
char FindChar(string str)
{
unordered_map<char,int> m;
char ch;
for(unsigned int i=0; i<str.size(); i++)
m[str[i]]++;
unsigned int i;
for(i=0; i<str.size(); i++) {
if(m[str[i]]==1) {
ch=str[i]; break;
}
}
if(i==str.size()) ch='.';
return ch;
}
int main() {
string str;
while(cin>>str) {
char ch=FindChar(str);
cout<<ch<<endl;
}
return 0;
}
AC代码(使用指针):
#include<iostream>
#include<string>
using namespace std;
char FindChar(char *input)
{
char *p=input;
int hash[256];
for(int i=0;i!=256;i++)
hash[i]=0;
while(*p!='\0')
{
hash[*p]++;
p++;
}
char *p1=input;
while(*p1!='\0')
{
if(hash[*p1]==1)
{
return *p1;
}
p1++;
}
return '.';
}
int main()
{
char str[1000];
while(cin>>str)
{
char ch=FindChar(str);
cout<<ch<<endl;
}
return 0;
}
C++版 - 剑指Offer 面试题35:第一个只出现一次的字符 解题报告(华为OJ034-找出字符串中第一个只出现一次的字符)的更多相关文章
- C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解
剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
- C++版 - 剑指offer 面试题22:栈的压入、弹出序列 题解
剑指offer 面试题22:栈的压入.弹出序列 提交网址: http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId ...
- C#版 - 剑指offer 面试题9:斐波那契数列及其变形(跳台阶、矩形覆盖) 题解
面试题9:斐波那契数列及其变形(跳台阶.矩形覆盖) 提交网址: http://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tp ...
- C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...
随机推荐
- 两种方法上传本地文件到github(转)
自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的原因是我习惯本地编辑,完成以后再一起上传github.看过了几个教程,总结出最 ...
- BZOJ1757 : Apple 偷苹果
设$f0[i][j][x][y][S]$表示盗贼位于$(i,j)$,守卫位于$(x,y)$,每棵苹果树苹果数量为$S$,盗贼先手时盗贼还能偷多少苹果. 设$f1[i][j][x][y][S]$表示盗贼 ...
- SQL Server查询重复数据
1.查询单列重复: select * from test where name in (select name from test group by name having count (name) ...
- 类型后面加问号 int?
类型后面加问号 int? 单问号---用于给变量设初值的时候,给变量(int类型)赋值为null,而不是0! 双问号---用于判断并赋值,先判断当前变量是否为null,如果是就可以赋一个新值,否则跳过 ...
- React事件绑定几种方法测试
前提 es6写法的类方法默认没有绑定this,不手动绑定this值为undefined. 因此讨论以下几种绑定方式. 一.构造函数constructor中用bind绑定 class App exten ...
- 从C过渡到C++的几个知识点(结构体、引用、重载运算符)
一.结构体和类(class) 下面一个使用结构体类型的例子 #include <iostream> using namespace std; struct Point{ // 声明Poin ...
- python中的单向循环链表实现
引子 所谓单向循环链表,不过是在单向链表的基础上,如响尾蛇般将其首尾相连,也因此有诸多类似之处与务必留心之点.尤其是可能涉及到头尾节点的操作,不可疏忽. 对于诸多操所必须的遍历,这时的条件是什么?又应 ...
- js-day05-JSON-jQuery初体验
JSON数据格式 JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.易于人阅读和编写,同时也易于机器解析和生成(网络传输速度快)JSON是JavaSc ...
- FTP出现PORT模式成功, 请更新你的站点配置文件
最近用FTP连接站点,经常出现连接不上或者连接失败,提示以PASV模式连接失败,正在使用PORT模式连接,最后才能连接成功,连接时间也是相当长,又慢又不稳定. 工具/原料 FlashFXP等F ...
- QEMU KVM Libvirt手册(11): Managing Storage
When managing a VM Guest on the VM Host Server itself, it is possible to access the complete file sy ...