• 1002. Find Common Characters


    /**
     * 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<String>): List<String> {
            val result = ArrayList<String>()
            val map = HashMap<Char, IntArray>()
            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
        }
    }
  • 相关阅读:
    BZOJ 1191 HNOI2006 超级英雄hero
    BZOJ 2442 Usaco2011 Open 修建草坪
    BZOJ 1812 IOI 2005 riv
    OJ 1159 holiday
    BZOJ 1491 NOI 2007 社交网络
    NOIP2014 D1 T3
    BZOJ 2423 HAOI 2010 最长公共子序列
    LCA模板
    NOIP 2015 D1T2信息传递
    数据结构
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/10604957.html
Copyright © 2020-2023  润新知