【LEETCODE】43、1002. Find Common Characters
package y2019.Algorithm.array; import java.util.*; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: CommonChars
* @Author: xiaof
* @Description: 1002. Find Common Characters
* Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings
* within the list (including duplicates).
* For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times
* in the final answer.
* You may return the answer in any order.
*
* Input: ["bella","label","roller"]
* Output: ["e","l","l"]
*
* @Date: 2019/7/4 11:07
* @Version: 1.0
*/
public class CommonChars { public List<String> solution(String[] A) {
List<String> result = new ArrayList<>();
int[] nums = new int[26]; //英文一共26个字符,并且是小写的
Arrays.fill(nums, Integer.MAX_VALUE);
//遍历所有的字符,根据小标放入集合中
for(String aTemp : A) {
//依次遍历所有字符
char tempC[] = aTemp.toCharArray();
int[] cnt = new int[26];
//第一次统计每个单词中出现的次数
for(int j = 0; j < tempC.length; ++j) {
cnt[tempC[j] - 'a']++;
}
//第二次我们过滤掉,每二个单词中出现的最小次数,比如第一个单词出现10次,但是第二个单词出现1次,那么都出现的次数也就是1次
for(int i = 0; i < nums.length; ++i) {
nums[i] = Math.min(nums[i], cnt[i]);
}
} //最后统计结果
for(int i = 0; i < nums.length; ++i) {
for(int j = 0; j < nums[i]; ++j) {
//如果出现多处,那么放入多处
result.add("" + (char) ('a' + i));
}
} //如果每个字符中都出现过,那么必须是3的倍数次
return result;
} public List<String> commonChars(String[] A) {
List<String> ans = new ArrayList<>();
int[] count = new int[26];
Arrays.fill(count, Integer.MAX_VALUE);
for (String str : A) {
int[] cnt = new int[26];
for (int i = 0; i < str.length(); ++i) { ++cnt[str.charAt(i) - 'a']; } // count each char's frequency in string str.
for (int i = 0; i < 26; ++i) { count[i] = Math.min(cnt[i], count[i]); } // update minimum frequency.
}
for (char c = 'a'; c <= 'z'; ++c) {
while (count[c - 'a']-- > 0) { ans.add("" + c); }
}
return ans;
} public static void main(String args[]) {
String as[] = {"bella","label","roller"};
CommonChars fuc = new CommonChars();
System.out.println(fuc.solution(as));
}
}
【LEETCODE】43、1002. Find Common Characters的更多相关文章
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 【LEETCODE】73、根据身高重建队列 第406题
说实话,这道题我没想出来,但是看解题报告题解比较让人觉得眼前一亮,这里记录下来 package y2019.Algorithm.greedy.medium; import java.util.Arra ...
随机推荐
- 关于新版本cube.js 集成preosto 的一个说明
实际上cubejs cli 是可以使用的,只是官方文档对于dbtype 说明的问题 使用如下命令我们可以创建prestodb 的基本项目 cubejs create -d prestodb myd ...
- 在nodejs中操作数据库(MongoDB和MySQL为例)
一.使用nodejs操作MongoDB数据库 ①使用官方的mongodb包来操作 ②使用第三方的mongoose包来操作(比较常用) // 首先必须使MongoDB数据库保持开启状态 // npm下载 ...
- vscode vue文件格式化没效果
在vscode 中 格式化vue文件没效果 解决办法: 点击头部文件 >首选项>设置 在右侧加入这两句 "vetur.format.defaultFormatter.js&q ...
- 洛谷 P1629 邮递员送信 题解
P1629 邮递员送信 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要 ...
- GoCN每日新闻(2019-10-15)
GoCN每日新闻(2019-10-15) GoCN每日新闻(2019-10-15) 1. Go Module 存在的意义与解决的问题 https://www.ardanlabs.com/blog/20 ...
- mysql 设置字段是否可以为null
//不允许为null alter table table1 change id id ) not null; //允许为null alter table table1 change id id ) n ...
- Spring AOP的实现记录操作日志
适用场景: 记录接口方法的执行情况,记录相关状态到日志中. 注解类:LogTag.java package com.lichmama.spring.annotation; import java.la ...
- [学习笔记] kd-tree
本文参考这位dalao的题解 前置技能:二叉查找树 其实kd-tree很简单的啦 和BST都差不多的啦 就是在划分的时候把每一维都比较一下就行啦 (\(dalao\)的kd-tree教程) 然而本蒟蒻 ...
- Markdown 打出上下标
1. 打上标,下标 2<sup>10</sup> <!--下标--> 2.同时打上下标 $x^p_ {ij}$ <!--上标为p,下标为ij,{}是用来组合i ...
- vscode插件开发之如何玩转vscode命令
这里以插件开发为例,VsCode之所以那么强大是因为它背后有千千万万的开发者们为其开发大量功能插件,WordPress同理. 那么如何玩转VsCode命令呢(以插件开发为例)? 官方文档必不可少 ht ...