LeetCode 1002. Find Common Characters (查找常用字符)
题目标签: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 (查找常用字符)的更多相关文章
- 1002. Find Common Characters查找常用字符
参考:https://leetcode.com/problems/find-common-characters/discuss/247573/C%2B%2B-O(n)-or-O(1)-two-vect ...
- Leetcode 1002. Find Common Characters
python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...
- Leetcode 1002. 查找常用字符
1002. 查找常用字符 显示英文描述 我的提交返回竞赛 用户通过次数301 用户尝试次数324 通过次数303 提交次数480 题目难度Easy 给定仅有小写字母组成的字符串数组 A,返回列表 ...
- 【LEETCODE】43、1002. Find Common Characters
package y2019.Algorithm.array; import java.util.*; /** * @ProjectName: cutter-point * @Package: y201 ...
- [Swift]LeetCode1002. 查找常用字符 | Find Common Characters
Given an array A of strings made only from lowercase letters, return a list of all characters that s ...
- 力扣(LeetCode)1002. 查找常用字符
给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表.例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 ...
- 【LeetCode】1002. Find Common Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【leetcode】1002. Find Common Characters
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- 1002. 查找常用字符 leecode
题目: 给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表.例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该 ...
随机推荐
- Android常见问题总结(二)
1.布局文件LinearLayout线性布局添加内容报错. 解决方法: 线性布局LinearLayout中包裹的元素多余1个需要添加android:orientation属性. 2.android 的 ...
- HFS模板开发
痉挛模板, 节, 符号 & 变量帮助需要更多帮助 吗?看看 下面这些链接-模板是模型痉挛用于构建 HTML 页面. -它分为几个部分, 每个部分描述最终 HTML 页面的一部分. -此模板必须 ...
- vue+axios上传文件
单独上传文件: <input class="file" name="file" type="file" accept="im ...
- 【转载】tomcat部署web项目的3中方法
转载自:http://blog.csdn.net/wjx85840948/article/details/6749964/ 1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的 ...
- css3属性之-webkit-margin-before
当没有对浏览器进行css边距初始化时,在web-kit浏览器上会出现下面的浏览器默认边距设置: ul, menu, dir { display: block; list-style-type: dis ...
- 洛谷——P2709 小B的询问
P2709 小B的询问 莫队算法,弄两个指针乱搞即可 这应该是基础莫队了吧 $x^2$可以拆成$((x-1)+1)^2$,也就是$(x-1)^2+1^2+2\times (x-1)$,那么如果一个数字 ...
- CodeForces 【20C】Dijkstra?
解题思路 heap+Dijkstra就能过.注意边是双向边,要用long long. 附上代码 #include <iostream> #include <queue> #in ...
- response对象处理HTTP文件头(禁用缓存、设置页面自动刷新、定时跳转网页)
response对象处理HTTP文件头 制作人:全心全意 禁用缓存 在默认情况下,浏览器将会对显示的网页内容进行缓存.这样,当用户再次访问相关网页时,浏览器会判断网页是否有变化,如果没有变化则直接显示 ...
- vue-cli npm install 失败
1.$ npm install -g vue-cli 2.vue init webpack sell (sell 是项目) 3.然后就是自动下载模板,根据提示输入 4.cd cell => np ...
- 关于图片预览使用base64在chrome上的性能问题解决方法
在开发后台上传图片的功能时候使用base64预览图片,结果在传入大量图片后导致chrome崩溃,代码如下 var img = new Image(); var render = new FileRea ...