博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1002. Find Common Characters
阅读量:6703 次
发布时间:2019-06-25

本文共 1669 字,大约阅读时间需要 5 分钟。

/** * 1002. Find Common Characters * https://leetcode.com/problems/find-common-characters/submissions/ * 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.Example 1:Input: ["bella","label","roller"]Output: ["e","l","l"] * */class Solution {    fun commonChars(A: Array
): List
{ val result = ArrayList
() val map = HashMap
() for (i in 0..(A.size - 1)) { for (c in A[i]) { if (!map.containsKey(c)) { map.put(c, IntArray(A.size)) } //array数组保存为:出现过的char在每一个A的子数组中出现的次数,顺序对应着A的子数组 //如a在"bella","label","roller",对应为[1,1,0] val array = map.get(c)!! array[i]++ map.put(c, array) } } map.forEach { key, charCountArr -> var canInsertFlag = 0 for (v in charCountArr) { canInsertFlag += v } var canInsert = canInsertFlag >= A.size if (canInsert) { charCountArr.sort() var insertCount = charCountArr[0] for (j in 0..(insertCount - 1)) { result.add(key.toString()) } } } return result }}

 

转载于:https://www.cnblogs.com/johnnyzhao/p/10604957.html

你可能感兴趣的文章
java 常用工具类的使用<一>
查看>>
A Neural Algorithm of Artistic Style
查看>>
Camera Calibration 相机标定:原理简介(三)
查看>>
ArrayList的使用方法【转载】
查看>>
String.Format格式说明
查看>>
Linux的proc文件系统详解
查看>>
Beta 冲刺(6/7)
查看>>
Kubernetes集群(概念篇)
查看>>
微软Hololens学院教程- Holograms 101: Introduction with Device【微软教程已经更新,本文是老版本】...
查看>>
Frequent Pattern 挖掘之二(FP Growth算法)(转)
查看>>
int * const 和 const int *
查看>>
requests模块相关用法
查看>>
linux
查看>>
vue菜单切换
查看>>
Mac下的Jenkins安装
查看>>
LeetCode(53):Maximum Subarray
查看>>
Android直接用手机打包apk!
查看>>
安排与愿想
查看>>
SQL Server 错误18456
查看>>
thinkCMF----导航高亮显示
查看>>