题目标签: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. Jmeter各组件介绍 及 使用

    本篇主要讲述Jmeter的各个组件及简单使用,其中包括以下内容: 一.线程组二.逻辑控制器三.配置元件四.定时器五.后置处理器六.断言七.监听器 八.参数化 网上大神整理的链接:http://blog ...

  2. android cmd adb命令安装和删除apk应用

    copy自http://blog.csdn.net/xpsharp/article/details/7289910 1. 安装Android应用程序 1) 启动Android模拟器 2) adb in ...

  3. iOS中ARC和非ARC混用

    如果在使用第三方类库的时候,我们可能会遇到一些内存管理的问题   那么如何在一个工程中实现ARC和非ARC混用呢,例如你创建一个ARC的工程,但是你引用的第三方类库是非ARC管理内存的   首先点击工 ...

  4. Xilinx FPGA编程技巧之常用时序约束详解

    1.   基本的约束方法 为了保证成功的设计,所有路径的时序要求必须能够让执行工具获取.最普遍的三种路径为: 输入路径(Input Path),使用输入约束 寄存器到寄存器路径(Register-to ...

  5. Flask框架 之路由

    一.视图函数路由规则 from flask import Flask, redirect, url_for # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录 ...

  6. cstring to utf8

    char* UnicodeToUtf8(CString unicode) { int len; len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)unico ...

  7. 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet(转载)

    若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet 请将 J ...

  8. linux mysql设置远程访问

    >mysql -u root -p 选择进入mysql数据库use `mysql`; 查看所有存在的账号和地址.SELECT `Host`,`User` FROM `user`; 现在决定让ro ...

  9. xmpp获取好友信息和添加删除好友(4)

    原始地址: XMPPFrameWork IOS 开发(五)获取好友信息和添加删除好友 好友列表和好友名片 [_xmppRoster fetchRoster];//获取好友列表 //获取到一个好友节点 ...

  10. Crossword Answers UVA - 232

    题目大意 感觉挺水的一道题.找出左面右面不存在或者是黑色的格子的白各,然后编号输出一横向单词和竖向单词(具体看原题) 解析 ①找出各个格子的编号 ②对每个节点搜索一下 ③输出的时候注意最后一个数据后面 ...