题目标签: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. git删除本地分支失败,报错error: branch 'test219' not found.

    错误: 删除本地分支报错,操作如下: git branch -d test219 操作失败,错误信息:error: branch 'test219' not found git branch -D t ...

  2. 廖雪峰 Git教程学习笔记 原文 http://www.liaoxuefeng.com/

    一 .集中式与分布式        先说集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推 ...

  3. 【转】jvm收集器

    HotSpot JVM收集器 上面有7中收集器,分为两块,上面为新生代收集器,下面是老年代收集器.如果两个收集器之间存在连线,就说明它们可以搭配使用. Serial(串行GC)收集器 Serial收集 ...

  4. Maven对不同的测试环境用不同的参数进行打包

    通过mvn package -P ${env} 加载不同配置文件 1.pom.xml中的配置 filter-dev.properties jdbc.properties

  5. Sonar 规则

    bug类型: 1.".equals()" should not be used to test the values of "Atomic" classes. ...

  6. HTML `capture` 属性

    file 类型的 <input> 除了调起系统的文件选择框外,还可通过指定 capture 属性来现场拍照或录制.配合 accept 属性,可实现更加便捷的文件获取. 比如想要录制一段视频 ...

  7. A4. JVM 内存分配及回收策略

    [概述] Java 技术体系中所提倡的自动内存管理最终可以归结为自动化地解决两个问题:给对象分配内存以及回收分配给对象的内存. 对象的内存分配,往大方向讲,就是在堆上分配,对象主要分配在新生代的 Ed ...

  8. 编译器:gcc, clang, llvm

    clang Clang是LLVM的前端,可以用来编译C,C++,ObjectiveC等语言.传统的编译器通常分为三个部分,前端(frontEnd),优化器(Optimizer)和后端(backEnd) ...

  9. mysql服务无法启动(1067错误)时数据备份的经验

    mysql服务无法启动(1067错误)时数据备份的经验 背景 方法 背景 在已安装MySQL5.5的情况下,再次安装 MySQL5.7时,因为MySQL5.7是压缩文件安装的方式,复制MySQL5.5 ...

  10. 解决idea控制台打印乱码问题

    idea控制台打印乱码,用起来总别扭,也是在网上搜索了一番,靠一点猜测解决了. 首先打开你自己的idea的安装目录下(即右键桌面图标,点击打开文件所在位置),然后找到idea.exe.vmoption ...