题目描述: 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置. 分析: 用一个数组统计每个字符出现的次数. 再次扫描数组,如果找到第一个字符次数为1的,那么返回它的位置. 代码: class Solution { public: int FirstNotRepeatingChar(string str) { int strLen = str.length(); ]; memset(times, , )); ; i < strLen…
题目描述 在一个字符串(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,…
思路: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,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置,…
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]]…
/************************************************************************* > File Name: 33_FirstNotRepeatChar.c > Author: Juntaran > Mail: JuntaranMail@gmail.com > Created Time: 2016年09月02日 星期五 13时43分20秒 ***************************************…
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码值作为数组的下标对应数组的一个数…
打算写 图解剑指 offer 66 题 的系列文章,不知道大家有没有兴趣…
题目:请实现一个函数用来找出字符流中第一个仅仅出现一次的字符. 举例说明 比如,当从字符流中仅仅读出前两个字符"go"时.第一个仅仅出现一次的字符是'g'.当从该字符流中读出前六个字符"google"时,第一个仅仅出现1次的字符是"l". 解题思路 字符仅仅能一个接着一个从字符流中读出来.能够定义一个数据容器来保存字符在字符流中的位置.当一个字符第一次从字符流中读出来时,把它在字符流中的位置保存到数据容器里.当这个字符再次从字符流中被读出来时.那…