leetcode刷题/哈希表 1002. 查找常用字符

1002. 查找常用字符

题意:

给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。

你可以按任意顺序返回答案。

示例 1:

输入:["bella","label","roller"]
输出:["e","l","l"]

示例 2:

输入:["cool","lock","cook"]
输出:["c","o"]
第一种解题思路:

第一种解法:

  • 需要用到两个map容器,第一个map存第一个字符串出现的字母,第二个字符全部设置为0
  • 循环从第二个字符串开始,每一个字符串都把字符存入map容器中
  • 然后跟第一个容器比较小,取小的值(如果后面不出现的字符那么一定为0),直到最后一个字符串
  • 遍历第一个map容器,就是我们需要的结果

优点:简单,易懂.

缺点:时间复杂度太高,运行时间惨不忍睹…

代码:
class Solution {
public:
    vector<string> commonChars(vector<string>& words) {
    if(words.size() == 0)
        return {};
	map<string, int> hash;
	map<string, int>map;
	vector<string> res;
	for (auto c : words[0])			
	{
		string d(1, c);
		++map[d];
	}
	for (int i = 1; i < words.size();i++)
	{
		string str = words[i];
		hash.clear();
		for (const char c : str)
		{
			string b(1, c);
			++hash[b];
		}
		for (const char c : words[0])
		{
			string b(1, c);
			map[b] = min(map[b], hash[b]);
		}
	}
	for (auto it = map.begin();it != map.end();it++)
	{
		for (int i = 0; i < it->second; i++)
		{
			res.push_back(it->first);
		}
	}
    return res;
    }
};
运行结果:

1002查找常用字符_STL哈希表_.png

第二种解法思路

其实并不能算新的解题思路,就是在原来的基础上做改进.

  • 把map容器换为26个字母的数组,静态数组速度比较快
代码:
class Solution {
public:
    vector<string> commonChars(vector<string>& words) {
    if(words.size() == 0)
        return {};
	int map[26] = { 0 };
	for (auto c : words[0])
	{
		++map[c - 97];
	}
	for (int i = 1; i < words.size();i++)
	{
		int hash[26] = { 0 };
		string str = words[i];
		for (const char c : str)
		{
			++hash[c - 97];
		}

		for (const char c : words[0])
		{
			
			map[c - 97] = min(map[c - 97], hash[c - 97]);
		}
	}
	vector<string> res;
	for (int i = 0; i < 26; i++)
	{
		for (int j = 0; j < map[i]; j++)
		{
			res.emplace_back(1, i + 'a');
		}
	}
    return res;
    }
};
数组运行结果:

1002查找常用字符_数组哈希表_.png

把数组改为vector后运行结果:

1002查找常用字符_STL.vector_.png

总结:

这道题我尝试了很多中方法来降低时间,但是都没成功,我最少的都是8ms.后来把数组改为vector容器,运行时间是一样的,就是空间减少了点,是因为我用数组时清0比较麻烦,所有我直接在每一次循环体都重新定义了一个数组,这样省去清0的麻烦.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

公仔面i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值