【面试题总结】1、统计字符串中某个单词出现的次数

【解决方法一】C++ map解决

一、map中的find函数:

用于查找map中是否包含某个关键字条目,传入的参数是要查找的key,最后返回一个迭代器,如果没有找到,则返回的迭代器等于end()返回的迭代器。示例代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 using namespace std;
 5
 6 int main() {
 7     map<int, string> mapStudent;
 8
 9     mapStudent.insert(pair<int, string>(1, "student_one"));
10     mapStudent.insert(pair<int, string>(2, "student_two"));
11     mapStudent.insert(pair<int, string>(3, "student_three"));
12
13     map<int, string>::iterator iter;
14     iter = mapStudent.find(3);
15     if (iter != mapStudent.end())
16         cout << "Find,the value is: " << iter->second << endl;
17     system("pause");
18     return 0;
19 }

运行结果:

二、map的插入方法:

插入的方法有好几种,下面介绍:map.insert(pair<type1,type2>(key,value))这种,还有一种是map[key] = value,前者出现重复不会发生改变,后者出现重复则会发生覆盖,后者如果没有给value值,直接使用map[key],则其value值默认为0。示例代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 using namespace std;
 5
 6 int main() {
 7
 8     map<int, string> mapStudent;
 9     mapStudent.insert(pair<int, string>(1, "student_one"));
10     mapStudent.insert(pair<int, string>(2, "student_two"));
11     mapStudent.insert(pair<int, string>(2, "xxooxxooxxo"));//不起作用
12
13     map<int, string>::iterator iter;
14     for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
15         cout << iter->first << ‘ ‘ << iter->second << endl;
16     }
17     cout << endl;
18     mapStudent[2] = "xxooxxooxxoo";//覆盖掉前面的value
19     for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
20         cout << iter->first << ‘ ‘ << iter->second << endl;
21     }
22     system("pause");
23     return 0;
24 }

运行结果:

三、是否插入成功?

我们通过pair来获取是否插入成功,pair返回两个变量,第一个是map的迭代器,第二个是插入成功的标志,插入成功pair的第二个参数是true,插入失败,第二个参数为false。实例代码:

1 pair<map<type1,type2>::iterator,bool> ret;
2
3 ret = map_s.insert(pair<type1,type2>(key,value))
4
5 if(ret.second==ture)
6
7     cout<<"插入成功"<<endl;
8 else
9     cout<<"插入失败"<<endl;

四、统计字符出现个数:

思路1:先创建一个map,遍历字符串,逐个判断如果存在则count++,不存在则创建一个,让其value为1

思路2:通过插入失败来增加字符的个数,如果插入失败则表明map中存在该字符,count++即可

思路3:需要对map了解,直接使用库里提供的[]运算符重载。通过键值找节点,直接给给实值+1.

思路1:先创建一个map,遍历字符串,逐个判断如果存在则count++,不存在则创建一个,让其value为1。代码如下:

 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 using namespace std;
 5
 6 // 方法1:
 7 int main()
 8 {
 9     map<char, int> map_s;
10     string str = "kkk ahguird-j l";
11
12     for (int i = 0; i < str.length(); ++i)
13     {
14         map<char, int>::iterator iter = map_s.find(str[i]);
15         if (iter != map_s.end())
16         {
17             iter->second++;
18         }
19         else                                                                            // 如果找不到就添加一个,找到了就count++
20         {
21             map_s.insert(pair<char, int>(str[i], 1));                // 如果测试用例为 "qwerqwer"时,前4次循环都是执行的这句else即insert插入操作
22         }
23     }
24     map<char, int>::iterator iter = map_s.begin();
25
26     for (; iter != map_s.end(); iter++)
27     {
28         cout << iter->first << ‘ ‘ << iter->second << endl;
29     }
30     cout << endl;
31
32     system("pause");
33     return 0;
34 }

运行结果:

思路2:通过插入失败来增加字符的个数,如果插入失败则表明map中存在该字符,count++即可。代码如下:

 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 using namespace std;
 5
 6 int main()
 7 {
 8     map<char, int> map_s;
 9     string str = "kkk ahguird-j l";
10
11     pair<map<char, int>::iterator, bool> ret;
12     for (int i = 0; i < str.length(); ++i)
13     {
14         ret = map_s.insert(pair<char, int>(str[i], 1));
15         if (ret.second == false)                                     // 如果插入失败,则该迭代器的第一个参数的value++
16         {
17             ret.first->second++;
18         }
19     }
20     map<char, int>::iterator iter = map_s.begin();
21     for (; iter != map_s.end(); iter++)
22     {
23         cout << iter->first << ‘ ‘ << iter->second << endl;
24     }
25     cout << endl;
26
27     system("pause");
28     return 0;
29 }

运行结果:

思路3:直接使用库里提供的[]运算符重载。通过键值找节点,直接给给实值+1.

 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 using namespace std;
 5
 6 int main() {
 7     string str;
 8     map<char, int> map_s;
 9     while (cin >> str) {
10         for (int i = 0; i < str.length(); ++i) {
11             map_s[str[i]]++;
12         }
13         map<char, int>::iterator iter;
14         for (iter = map_s.begin(); iter != map_s.end(); ++iter) {
15             cout << iter->first << ‘:‘ << iter->second << endl;
16         }
17     }
18 }

思路3是真的6阿~

参考连接:https://blog.csdn.net/qq_34312386/article/details/55281662

对思路3补充说明:假设一个map对象map_s中只存在一对pair<char,int>(‘a‘,1),现在执行map_s[‘b‘]则map_s中存在两对pair分别是(‘a‘,1),(‘b‘,0),是的,没错,‘b‘的value默认是0,那么如果刚才执行的不是map_s[‘b‘],而是map_s[‘b‘]++,那么map_s中的两对pair为(‘a‘,1)和(‘b‘,1),理解为插入‘b’,然后value值++,所以,map_s[‘b‘]++这个语句的理解如下:

如果map_s中不存在key为‘b’的元素,那么在map_s中添加‘b’,value默认为0,然后map_s[‘b‘]++,value变成1.

如果map_s中存在key为 ‘b‘ 的元素,那么该语句表示map_s[‘b‘]的value++.

 1 #include<iostream>
 2 #include<map>
 3 using namespace std;
 4
 5 int main() {
 6     map<char, int> m;
 7     m.insert(pair<char, int>(‘a‘, 1));
 8     map<char, int>::iterator iter = m.begin();
 9     cout << iter->first << ‘ ‘<<iter->second<<endl;
10     m[‘b‘];
11     iter++;
12     cout << iter->first << ‘ ‘ << iter->second << endl;//value默认是0
13     m[‘b‘]++;
14     cout << iter->first << ‘ ‘ << iter->second << endl;//将value++
15     while (1);
16 }

原文地址:https://www.cnblogs.com/xuelisheng/p/10976949.html

时间: 2024-08-29 13:45:26

【面试题总结】1、统计字符串中某个单词出现的次数的相关文章

Java基础知识强化之集合框架笔记61:Map集合之统计字符串中每个字符出现的次数的案例

1. 首先我们看看统计字符串中每个字符出现的次数的案例图解:

用python统计list中各元素出现的次数(同理统计字符串中各字符出现的次数)

统计list中各元素出现的次数,下面的方法也适用于统计字符串中各字符出现的次数 1.用字典的形式来处理 a = "abhcjdjje" a_dict = {}for i in a: a_dict[i] = a.count(i)print(a_dict) 2.用count函数直接打印出来 L = [2,4,5,6,2,6,0,4] for i in L: print("%d的次数:%d"%(i,L.count(i))) 3.用collections的Counter函数

统计字符串中的单词数目

统计字符串中单词的数目,更复杂的话从一个文本中读出字符串并生成单词数目统计结果.         第一个问题:这个问题的解决方案是,字符串之所以可以成为单词就是因为有空格符的出现,那么对于字符串中单词的数目来说,只需要统计其中空格符出现的次数就可以了~~~ 第二个问题,从文本中读出字符串并统计每一个单词的统计结果,那么久需要借助于字典map了,每一个单词使用了一个位置 ,如果是已经出现的单词,那么就给相应的单词数量加一,如果没有出现在字符串中,那么就添加该单词. 对于一串字符串来说,如果需要对于

二叉排序树统计字符串中出现的字符及其次数

二叉排序树统计字符串 结点的类型: typedef struct tnode { char ch; //字符 int count; //出现次数 struct tnode *lchild,*rchild; } tnode,*BTree; 完整代码 //文件名:exp9-5.cpp #include<iostream> using namespace std; #define MAXWORD 100 typedef struct tnode // typedef tnode *BTree { c

python统计字符串中每个单词出现的个数【一行】

s = 'i am very very like you and like you' dict( [(i, s.split().count(i)) for i in s.split()] ) Out[2]: {'i': 1, 'am': 1, 'very': 2, 'like': 2, 'you': 2, 'and': 1} set( map(lambda x:(x, s.split().count(x)), s.split()) ) Out[6]: {('am', 1), ('and', 1)

python之统计字符串中字母出现次数

dic=dict() d={} s=set() s='helloworld' (1)d=dict() for x in s: if x not in d.keys(): d[x]=1 else: d[x]=d[x]+1 print(d) (2)d2=dict() for x in s: d2[x]=d2.get(x,0)+1 print(d2) (3)d3=dict() for x in s: d3[x]=s.count(x) print(d3) 上面一共给出了三种方法,均是以字典的形式输出,但

力扣(LeetCode)字符串中的单词数 个人题解

统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" 输出: 5 题目描述比较不清楚,这里只要是用空格隔开的一律当作字符,包括非字母.使用JAVA自带库函数解决问题.记得忽略空格情况 当然这里使用了较大的内存保存分割后的ss字符串数组,如果对内存比较敏感的可以对字符串手动以空格划分.(这里空格可能多个,所以可以使用正则表达式较为方便去匹配) 代码如下: cl

leetcode434 字符串中的单词树(python)

统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John"输出: 5 class Solution(object): def countSegments(self, s): """ :type s: str :rtype: int """ s = s.split() return len(s) split

2014华为实习上级笔试题-- 统计字符串中出现的单词

#include<iostream> //#include<string> using namespace std; struct node { char word[10]; int num; }; node obj[100]; void my_word(char input[], char output[]) { int sum=0,flag=0; int i=0,j=0,k=0; while(input[i]!='\0')///////////读入单词 { if((input[