【剑指offer-解题系列(35)】第一个只出现一次的字符
来源:互联网 发布:淘宝退款被拒绝怎么办 编辑:程序博客网 时间:2024/06/10 20:01
题目描述
在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
分析
采用类似直方图统计的方法:
一个数组统计字符出现的频率次数,另一个数组记录第一次出现的位置
两个数组大小都是 26*2(大小写字母总数)
代码实现
- int FirstNotRepeatingChar(string str) {
if(str.size()<=0)
return -1;
int times[52]={0} ;
int first_show_position[52];
memset(first_show_position,-1,52*sizeof(int));
////memset 格式注意
for(int i =0;i<str.size();i++){
if( (str[i]>=97 && str[i]<=122 ) || (str[i]>=65 &&str[i]<=90 ))
{
if((str[i]>=97 && str[i]<=122 )) {
times[str[i]-97]++;
if( first_show_position[str[i]-97] == -1 ){
first_show_position[str[i]-97] = i;
}
}
else{
times[str[i]-65+26]++;
if( first_show_position[str[i]-65+26] == -1 ){
first_show_position[str[i]-65+26] = i;
}
}
}
}
int first = INT_MAX;
for(int i =0;i<52;i++){
if( times[i] == 1){
if(first_show_position[i]<first){
first = first_show_position[i];
}
}
}
return first;
}
//a-z:97-122 A-Z:65-90
阅读全文
0 0
- 【剑指offer-解题系列(35)】第一个只出现一次的字符
- 剑指offer(35)第一个只出现一次的字符
- 剑指offer-35 第一个只出现一次的字符
- 剑指Offer 35 第一个只出现一次的字符
- 剑指offer 35 第一个只出现一次的字符
- 剑指Offer 面试题35:第一个只出现一次的字符 解题报告(华为OJ034-找出字符串中第一个只出现一次的字符)
- 第一个只出现一次的字符 (剑指offer)
- 剑指Offer系列-面试题35:第一个只出现一次的字符
- 【剑指offer】第一个只出现一次的字符
- 剑指offer--第一个只出现一次的字符
- 剑指Offer之 - 第一个只出现一次的字符
- 《剑指offer》第一个只出现一次的字符位置
- 剑指offer:第一个只出现一次的字符位置
- 剑指Offer--第一个只出现一次的字符位置
- 【剑指Offer】第一个只出现一次的字符位置
- 【剑指offer】之第一个只出现一次的字符
- 剑指offer:第一个只出现一次的字符位置
- [剑指offer]第一个只出现一次的字符位置
- Markdown基本语法
- Maven入门教程(内含实例)
- Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed with multiple
- hook_导出表eat
- 主流深度学习开源框架
- 【剑指offer-解题系列(35)】第一个只出现一次的字符
- 03_hibernate5快速入门_JPA的方式
- 理解Angular的作用域
- 解决 jqGrid('getRowData') 获取 少最后一行 bug
- python scrapy crawl csdnblog出现indentationError:unident does not matcher any outer indentation level
- 第2章 Oracle 的安装与配置
- Proposal of Wireless Communication
- c++实现二叉堆及堆排序
- 不用+的加法