LeetCode.1002-寻找共有字符(Find Common Characters)
这是悦乐书的第375次更新,第402篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002)。给定仅由小写字母组成的字符串A,返回列表中所有字符串都有显示的字符的列表(包括重复字符)。例如,如果一个字符在所有字符串中出现3次但不是4次,则需要在最终答案中包含该字符三次。
你可以按任何顺序返回答案。例如:
输入:["bella","label","roller"]
输出:["e","l","l"]
输入:["cool","lock","cook"]
输出:["c","o"]
注意:
1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j]是一个小写字母。
02 解题
题目的意思是找A中所有字符串共有的字符,即所有字符串的字符交集。比如示例中第一个字符串数组["bella","label","roller"],"bella"和"label"的字符交集是{'a','b','e','l','l'},"label"和"roller"的字符交集是{'e','l','l'},而{'a','b','e','l','l'}和{'e','l','l'}的交集是{'e','l','l'},所以最后的结果是["e","l","l"]。
思路:使用一个26个长度的int数组count,初始值设为整型最大值,循环处理A中的字符串,借助26个长度的int数组tem,将每个字符串中的字符出现次数记数,然后比较count和tem中对应位的元素值大小(字符出现次数),取较小(求交集)的一个重新赋值给count中的对应位,最后将count数组中大于0的数转成字符串添加到List中去。
public List<String> commonChars(String[] A) {
List<String> result = new ArrayList<String>();
int[] count = new int[26];
for (int i=0; i<26; i++) {
count[i] = Integer.MAX_VALUE;
}
for (String str : A) {
int[] tem = new int[26];
for (int j=0; j<str.length(); j++) {
tem[str.charAt(j)-'a']++;
}
for (int k=0; k<26; k++) {
count[k] = Math.min(count[k], tem[k]);
}
}
for (int i=0; i<26; i++) {
while (count[i]-- > 0) {
result.add((char)(i+'a')+"");
}
}
return result;
}
03 小结
算法专题目前已连续日更超过七个月,算法题文章242+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.1002-寻找共有字符(Find Common Characters)的更多相关文章
- Leetcode 1002. 查找常用字符
1002. 查找常用字符 显示英文描述 我的提交返回竞赛 用户通过次数301 用户尝试次数324 通过次数303 提交次数480 题目难度Easy 给定仅有小写字母组成的字符串数组 A,返回列表 ...
- [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题解寻找最短字符路径
1.题目描述 2.分析 最简单的方案,对每一个字符,向两边寻找. 3.代码 vector<int> shortestToChar(string S, char C) { vector< ...
- 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 ...
- Find Common Characters - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find Common Characters - LeetCode 注意点 不能单纯的以字母出现的次数来判断是否是公共的字母 解法 解法一:将第一个字符串 ...
- 乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters
乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters 一.前言 在算法之中出现最多的就是字符串方面的问题了,关于字符串的 ...
- C#版(击败100.00%的提交) - Leetcode 744. 寻找比目标字母大的最小字母 - 题解
C#版 - Leetcode 744. 寻找比目标字母大的最小字母 - 题解 744.Find Smallest Letter Greater Than Target 在线提交: https://le ...
- leetcode笔记 动态规划在字符串匹配中的应用
目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...
随机推荐
- Python 3标准库 第十四章 应用构建模块
Python 3标准库 The Python3 Standard Library by Example -----------------------------------------第十四章 ...
- 【leetcode】1248. Count Number of Nice Subarrays
题目如下: Given an array of integers nums and an integer k. A subarray is called nice if there are k odd ...
- .net core 下载文件 其他格式
app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions { //FileProvider = new PhysicalFilePr ...
- jquery keypress() 方法 语法
jquery keypress() 方法 语法 作用:keypress 事件与 keydown 事件类似.当按钮被按下时,会发生该事件.它发生在当前获得焦点的元素上.不过,与 keydown 事件不同 ...
- AngularJS基础语法
1.ng-app 决定了angularjs的作用域范围,你可以如下使用: <html ng-app> … </html> 来让angularjs渲染整个页面,也可以使用 < ...
- 哈密尔顿环x
欧拉回路是指不重复地走过所有路径的回路,而哈密尔顿环是指不重复地走过所有的点,并且最后还能回到起点的回路. 代码如下: #include<iostream> #include<cs ...
- JS框架_(Laydate.js)简单实现日期日历
百度云盘 传送门 密码:71hf JavaScript日期与时间组件_____laydate.js 日期日历效果: <!DOCTYPE html> <html> <hea ...
- Nginx配置记录【例3】
C服务器,例: [root@82_www_db_2 conf.d]# egrep -v "^#|^$" /etc/nginx/nginx.conf user nginx; work ...
- java 多线程为何会出现无法捕获异常的现象?
提出问题: 很多Java初学者在初学java 多线程的时候可能会看到如下代码: public class ExceptionThread implements Runnable{ @Override ...
- 第九周学习总结&实验报告七
实验报告: 实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. 实验 ...