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 ...
随机推荐
- appium+python+unittest 测试用例的几种加载执行方式
利用python进行测试时,测试用例的加载方式有2种: 一种是通过unittest.main()来启动所需测试的测试模块: 一种是添加到testsuite集合中再加载所有的被测试对象,而testsu ...
- 详细介绍redis的集群功能,带你了解真正意义上的分布式
Redis 集群是一个分布式(distributed).容错(fault-tolerant)的 Redis 实现, 集群可以使用的功能是普通单机 Redis 所能使用的功能的一个子集(subset). ...
- 基础的 sparkSQL操作
spark连接mysql操作 数据库jdbc 连接封装 package test.com import org.apache.spark.sql.{DataFrame, SparkSession} / ...
- 20180711-统计PDB中的蛋白质种类、膜蛋白文件个数及信息等
20180710完成这份工作.简单,但是完成了还是很开心.在我尝试如何使用pickle保存数据后,尝试保存PDB文件中“HEADER”中的信息.文件均保存于实验室服务器(97.73.198.168)/ ...
- web12 使用map型的request、session、application
电影网站:www.aikan66.com 项目网站:www.aikan66.com 游戏网站:www.aikan66.com 图片网站:www.aikan66.com 书籍网站:www.aikan66 ...
- KMP算法之next数组的求解思路
2.next数组的求解思路 本部分内容转自:http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algo ...
- MYSQL-不能创建表
Can't create table '.\ticket\user_role.frm' (errno: 121) 语法是对的,但显示上面的错误 原因有三种 1.表名重复 2.以该名字命名的表之前创建过 ...
- Internet History, Technology and Security (Week8)
Week 8 This week we start two weeks of Internet Security. It is a little technical but don't worry - ...
- ns3 模拟无线网络通信
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * This program is fre ...
- 删除一个数字之后数列gcd最大
★实验任务 顾名思义,互质序列是满足序列元素的 gcd 为 1 的序列.比如[1,2,3], [4,7,8],都是互质序列.[3,6,9]不是互质序列.现在并不要求你找出一个互质 序列,那样太简单了! ...