题目链接

Find Common Characters - LeetCode

注意点

  • 不能单纯的以字母出现的次数来判断是否是公共的字母

解法

解法一:将第一个字符串的每个字母逐个在其他字符串中查找,如果所有的字符串都含有就加入res。时间复杂度O(n^2),n是所有字符串的长度之和。

class Solution {
public:
vector<string> commonChars(vector<string>& A) {
vector<string> res;
if(A.size() == 0) return res;
int i,j,k;
for(i = 0;i < A[0].size();i++)
{
for(j = 1;j < A.size();j++)
{
for(k = 0;k < A[j].size();k++)
{
if(A[j][k] == A[0][i])
{
A[j][k] -= 32;
break;
}
}
if(k >= A[j].size()) break;
}
if(j >= A.size())
{
string temp = "";
res.push_back(temp+A[0][i]);
}
}
return res;
}
};

解法二:先统计第一个字符串中所有字母出现的次数,然后逐个统计其他字符串中字母出现的次数,每统计完一个字符串对每个字母取较小的出现的次数。时间复杂度O(n)

class Solution {
public:
vector<string> commonChars(vector<string>& A) {
vector<string> res;
vector<int> count(26,0);
if(A.size() == 0) return res;
int i,j;
for(i = 0;i < A[0].size();i++) count[A[0][i]-'a']++;
for(i = 1;i < A.size();i++)
{
vector<int> tempCount(26,0);
for(j = 0;j < A[i].size();j++) tempCount[A[i][j]-'a']++;
for(j = 0;j < 26;j++) count[j] = min(count[j],tempCount[j]);
}
for(i = 0;i < 26;i++)
{
for(j = 0;j < count[i];j++) res.push_back(string(1,i+'a'));
}
return res;
}
};

小结

  • 这是今天早上周赛的第一道题,我做了半个小时,第一名只做了两分钟...

Find Common Characters - LeetCode的更多相关文章

  1. LeetCode 1002. Find Common Characters (查找常用字符)

    题目标签:Array, Hash Table 题目给了我们一个string array A,让我们找到common characters. 建立一个26 size 的int common array, ...

  2. 【LEETCODE】43、1002. Find Common Characters

    package y2019.Algorithm.array; import java.util.*; /** * @ProjectName: cutter-point * @Package: y201 ...

  3. 3-Longest Substring Without Repeating Characters @LeetCode

    3-Longest Substring Without Repeating Characters @LeetCode 题目 题目中得到的信息有: 一段字符串找出不重复子串的最大长度,只需要长度信息. ...

  4. Write a program that gives count of common characters presented in an array of strings..(or array of

    转自出处 Write a program that gives count of common characters presented in an array of strings..(or arr ...

  5. 【LeetCode】1002. Find Common Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  6. LeetCode.1002-寻找共有字符(Find Common Characters)

    这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都 ...

  7. 【leetcode】1002. Find Common Characters

    题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...

  8. Leetcode 1002. Find Common Characters

    python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...

  9. [Swift]LeetCode1002. 查找常用字符 | Find Common Characters

    Given an array A of strings made only from lowercase letters, return a list of all characters that s ...

随机推荐

  1. lua栈

    既然Lua虚拟机模拟的是CPU的运作,那么Lua栈模拟的就是内存的角色.在Lua内部,参数的传递是通过Lua栈,同时Lua与C等外部进行交互的时候也是使用的栈.,先关注的是Lua栈的分配,管理和相关的 ...

  2. eclipse检出SVN代码的详细流程

    1.添加SVN资源库位置(未安装SVN,请先安装SVN) 2.因为该项目不是maven项目 所以还需要加入jar包(将项目lib里面的jar都Buile Path) 3.我这个项目需要修改编码格式 右 ...

  3. Netty源码分析第5章(ByteBuf)---->第5节: directArena分配缓冲区概述

    Netty源码分析第五章: ByteBuf 第五节: directArena分配缓冲区概述 上一小节简单分析了PooledByteBufAllocator中, 线程局部缓存和arean的相关逻辑, 这 ...

  4. webpack开发和生产两个环境的配置详解

    一开始在接触webpack 的时候,简直痛不欲生,现在回头看,做个注释,当然参考了很多文章.这是一个关于vue 开发的webpack 架构会列举出来webpack 系列教程Webpack——令人困惑的 ...

  5. “Hello World!”团队第五周第六次会议

    “Hello World!”团队第五周第六次会议   博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.checkout& ...

  6. datetime类型转换

    Select CONVERT(varchar(100), GETDATE(), 8):14:53:14 Select CONVERT(varchar(100), GETDATE(), 9): 06  ...

  7. 项目Beta冲刺(团队)第七天

    1.昨天的困难 服务器部署出了问题,本地服务器差点崩掉 运行一直闪退,在查找哪里出现问题的路上一去不复返 2.今天解决的进度 成员 进度 陈家权 消息功能模块 赖晓连 问答功能模块 雷晶 部署服务器到 ...

  8. 用JAVA制作微型操作系统4月23日情况

    弄好了一个自认为十分精美的界面,但本想着昨天就在开始按钮上先套入控制jp222面板上的jb2标签上的时间更新,这按钮起到开始线程的作用(我认为按钮应该可以通过t.start()来触发线程,结果不知为什 ...

  9. Internet History, Technology and Security (Week⑨)

    Week ⑨ We are now on the second to last week of the class and finishing up our look at Internet Secu ...

  10. ns3 回调机制

    (1)目的:为了实现两个模块之间的通信(这两个模块没有任何依赖关系) (2) C语言中的函数指针 int (*a)(int q) = 0; //声明一个函数指针a,初始值设为0 //. //. //. ...