题目标签:Array, Hash Table

题目给了我们一个string array A,让我们找到common characters。

建立一个26 size 的int common array,作为common characters 的出现次数。

然后遍历每一个 string, 都建立一个 int[26] 的 temp array,来数每个char 出现的次数,当完成了这个str 之后,

把这个temp array 更新到 common 里面去,这里要挑最小的值存入。当完成所有string 之后,common array 里面剩下的,

就是我们要找的常用字符。

具体看code。

Java Solution:

Runtime beats 58.42%

完成日期:03/08/2019

关键点:对于每一个新的string,把common array 里面的次数进行更新,取最小的次数,排除不是 common 的 字符。

class Solution
{
public List<String> commonChars(String[] A)
{
List<String> result = new ArrayList<>();
int [] commonCharsCount = new int[26];
Arrays.fill(commonCharsCount, Integer.MAX_VALUE); // iterate each string in A
for(String str : A)
{
int [] tempCharsCount = new int[26]; // count char for this string
for(char c : str.toCharArray())
tempCharsCount[c - 'a']++; // update the commonCharsCount
for(int i=0; i<commonCharsCount.length; i++)
commonCharsCount[i] = Math.min(commonCharsCount[i], tempCharsCount[i]);
} // iterate commonCharsCount to add each char
for(int i=0; i<commonCharsCount.length; i++)
{
while(commonCharsCount[i] > 0)
{
result.add("" + (char)('a' + i));
commonCharsCount[i]--;
}
} return result;
} }

参考资料:https://leetcode.com/problems/find-common-characters/discuss/?currentPage=1&orderBy=recent_activity&query=

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 1002. Find Common Characters (查找常用字符)的更多相关文章

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

    参考:https://leetcode.com/problems/find-common-characters/discuss/247573/C%2B%2B-O(n)-or-O(1)-two-vect ...

  2. Leetcode 1002. Find Common Characters

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

  3. Leetcode 1002. 查找常用字符

    1002. 查找常用字符  显示英文描述 我的提交返回竞赛   用户通过次数301 用户尝试次数324 通过次数303 提交次数480 题目难度Easy 给定仅有小写字母组成的字符串数组 A,返回列表 ...

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

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

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

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

  6. 力扣(LeetCode)1002. 查找常用字符

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

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

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

  8. 【leetcode】1002. Find Common Characters

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

  9. 1002. 查找常用字符 leecode

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

随机推荐

  1. mongo 3.4分片集群系列之八:分片管理

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  2. Windows下80端口被进程System&PID=4占用的解决方法

    我的占用原因是 SQL Server Reporting Services,停止掉这个服务并设置其为手动启动即可 如果你并没有安装 SQL Server,请参考下文解决 =============== ...

  3. NVIDIA各个领域芯片现阶段的性能和适应范围

    NVIDIA作为老牌显卡厂商,在AI领域深耕多年.功夫不负有心人,一朝AI火,NVIDIA大爆发,NVIDIA每年送给科研院所和高校的大量显卡,大力推广Physix和CUDA,终于钓了产业的大鱼. 由 ...

  4. 搭建jQuery开发环境

    每个版本又分为一下两种:开发版和生产版: 版本 大小/KB 描述jquery-1.x.js 约288 开发版,完整无压缩,多用于学习,开发和测试jquery-2.x.js 约251 开发版,完整无压缩 ...

  5. docker时区

    docker cp /etc/localtime <id/name>:/etc/localtime

  6. 使用SELECT语句检索数据

    使用SELECT语句检索数据select指令适用于SQL数据库SELECT 语句用于从数据库中选取数据.(指令不分大小写,选择的值除名字和一些有特殊意义的字符可不分大小写,from结束时一定要加;) ...

  7. 用 Systemtap 统计 TCP 连接

    转自: https://mp.weixin.qq.com/s?__biz=MzIxMjAzMDA1MQ==&mid=2648946009&idx=1&sn=3a0be2fe4f ...

  8. <MyBatis>入门四 传入的参数处理

    1.单个参数 传入单个参数时,mapper文件中 #{}里可以写任意值 /** * 传入单个参数 */ Employee getEmpById(Integer id); <!--单个参数 #{} ...

  9. 如何在 CentOS 7 上安装 Nginx

    本文首发:开发指南:如何在 CentOS 7 上安装 Nginx Nginx 读作 engine x, 是一个免费的.开源的.高性能的 HTTP 和反向代理服务,主要负责负载一些访问量比较大的站点. ...

  10. vim基础(二)

    上一篇提到了插入与删除.定位.复制与粘贴以及退出与保存几项基本操作,这篇继续整理其他常用命令. 撤销与替换 首先是我们在输入过程中不小心打错了,或者误删了,怎么恢复?在word里我们有ctrl+Z,v ...