Find Common Characters - LeetCode
题目链接
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的更多相关文章
- LeetCode 1002. Find Common Characters (查找常用字符)
题目标签:Array, Hash Table 题目给了我们一个string array A,让我们找到common characters. 建立一个26 size 的int common array, ...
- 【LEETCODE】43、1002. Find Common Characters
package y2019.Algorithm.array; import java.util.*; /** * @ProjectName: cutter-point * @Package: y201 ...
- 3-Longest Substring Without Repeating Characters @LeetCode
3-Longest Substring Without Repeating Characters @LeetCode 题目 题目中得到的信息有: 一段字符串找出不重复子串的最大长度,只需要长度信息. ...
- 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 ...
- 【LeetCode】1002. Find Common Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- LeetCode.1002-寻找共有字符(Find Common Characters)
这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都 ...
- 【leetcode】1002. Find Common Characters
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- Leetcode 1002. Find Common Characters
python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...
- [Swift]LeetCode1002. 查找常用字符 | Find Common Characters
Given an array A of strings made only from lowercase letters, return a list of all characters that s ...
随机推荐
- 自动分配ip的方法- 【Linux】
1. 查看本机无线网络使用的网卡 2. 设置vbox的网络连接为桥接,并选择本机无线网络对应的网卡 3. 进入系统,输入ifconfig命令,记录下系统的HWaddr 4. 修改系统ip配置文 ...
- 013-- mysql常用的查询优化方法
浅谈MySQL中优化sql语句查询常用的30种方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句 ...
- 如何用Python为你的邮箱加油?还有这种操作!
我来介绍一下我是如何使用 Python 来节省成本的. 我最近在开一辆烧 93 号汽油的车子.根据汽车制造商的说法,它只需要加 91 号汽油就可以了.然而,在美国只能买到 87 号.89 号.93 号 ...
- Kubernetes网络方案 Flannel和calico
摘抄某博客 1. Flannel Flannel是为kubernetes设计的一个非常简洁的多节点三层网络方案,解决不同host上的容器互联问题,原理是为每个host分配一个subnet,容器从此 ...
- Webstorm使用时发生Page 'http://localhost:63340/n…tok/css/bootstrap.css.map' requested without authorization, you can copy URL and open it in browser to trust it.
在使用webstorm编辑器开发时候,点击4处发生以下错误: Page 'http://localhost:63340/n…tok/css/bootstrap.css.map' requested w ...
- -lPods-NewsPushClientSDK is not an object file (not allowed in a library)
今天在给客户定制SDK的过程中,出现了下面的一个问题,具有代表性,现在记录下来 问题: Showing All Errors Only : /Applications/Xcode.app/Conten ...
- Daily Scrum 11.18
今日完成任务: 1.在提问问题的时候为问题创建索引 2.解决了修改个人资料后刷新没有更新的问题 3.初步加入了采纳功能(没完善UI设计) 遇到困难:创建索引之后,跳转到主页,需要重新登录,找了半天不知 ...
- 20135234mqy 实验二 Java面向对象程序设计
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1352 姓名:mqy 学号:20135234 成绩: 指导教师: ...
- alpha版postmortem 报告
一.团队开发存在的问题 此次会议我们团队中每个成员都仔细思考并提出了团队在这一阶段存在的问题,主要如下: 1.前期任务规划.分配不合适: 2.个人对认领任务模块完成度.了解度不够: 3.个人学习意识. ...
- Math 类的使用(一小部分)
package com.Date.Math; /* Math 数学类, 主要是提供了很多的数学公式. abs(double a) 获取绝对值 ceil(double a) 向上取整 floor(dou ...