class Solution { public: int FirstNotRepeatingChar(string str) { if(!str.size()) ; ]={}; ;i<str.size();i++) { num[str[i]]++; } ; ) i++; if(i==str.size()) ; else return i; } }; 字符char是一个长度为8的数据类型,总共有256种可能.因此创建一个长度为256的数组,每个字母根据其ASCII码值作为数组的下标对应数组的一个数…
题目描述: 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置. 分析: 用一个数组统计每个字符出现的次数. 再次扫描数组,如果找到第一个字符次数为1的,那么返回它的位置. 代码: class Solution { public: int FirstNotRepeatingChar(string str) { int strLen = str.length(); ]; memset(times, , )); ; i < strLen…
map默认对key进行排序,unordered_map不对键或值进行排序,但是也不是默认插入的顺序 -------------------------------------------------------------------------------------------------------------------------------------------------------- 时间限制:1秒 空间限制:32768K 热度指数:231606 本题知识点: 字符串 题目描述…
题目: 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 分析: 遍历字符串,利用Hashmap将每一个字符出现的值存储起来,然后再遍历字符串,返回第一个字符值为1的索引即可. 程序: C++ class Solution { public: int FirstNotRepeatingChar(string str) { ; i < str.size(); ++i){ m[str[i]]…
题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 牛客网链接 js代码 function FirstNotRepeatingChar(str) { // write code here let map = new Map() for (let i of str) { if (map.get(i) === undefined) map.set(i, 1) else map.set(i,…
/************************************************************************* > File Name: 33_FirstNotRepeatChar.c > Author: Juntaran > Mail: JuntaranMail@gmail.com > Created Time: 2016年09月02日 星期五 13时43分20秒 ***************************************…
思路:i表示字符的ASCII码值,cntp[i]表示字符出现的次数. AC代码 class Solution { public: int FirstNotRepeatingChar(string str) { int n = str.length(); if(n == 0) return -1; int cnt[500]; memset(cnt, 0, sizeof(cnt)); for(int i = 0; i < n; ++i) { cnt[str[i]]++; } int ans = 0;…
题目链接 https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking 题意 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置,…
时间限制:1秒 空间限制:32768K 热度指数:198150 本题知识点: 数组 题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. class Solution { public: void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) { for(int x:data) { ) { *num1 = x; break; } } for(vector<int>:…
在看剑指offer的时候,感觉这三个题目很像,都是用哈希表可以解决,所以把这三个题整理出来,以供复习. 剑指offer35题:第一个只出现一次的字符 题目描述:在字符串中找出第一个只出现一次的字符.如输入“abaccdeff”,则输出‘b’. 题目分析:统计字符串每个字符出现的次数,然后找出第一个只出现一次的字符.我们可以定义一个哈希表,这个哈希表的键值key是字符,value是该字符出现的次数.这样我们需要对字符串扫描两次,第一次扫描字符串时,每扫描一个字符就在哈希表对应项中把次数加1.第二次…