0317-2020-LEETCODE-1160-言葉を綴ります

かかわらず、サイクルの26倍(サブマップ[2つの逐次比較マップ、単語; ++ - アルファベットマップの辞書26文字の出現数を格納するために非常に良いアイデア、使用int型[26]である[「A」C] J] == 0)は比較にならないでしょう。

public int countCharacters1(String[] word,String chars){
        int[] map = new int[26];
        for (char c : chars.toCharArray()){
            map[c - 'a']++;
        }
        int res = 0;
        for (int i = 0; i < word.length; i++) {
            int[] subMap = new int[26];
            for (char c : word[i].toCharArray()) {
                subMap[c - 'a']++;
            }
            for (int j = 0; j < 26; j++) {
                if (map[j] < subMap[j]){
                    break;
                }
                if (j == 25){
                    res += word[i].length();
                }
            }
        }
        return res;
    }

あなた自身の時間と労力を書く、保持リストで、呪文の終了後の各時間は、現在のリストの元のリストを復元します。各スペルの必要性が存在するかどうか分析し、は直接ブレークはありません。手紙は存在しますが、比較サイクルが続きます。

public int countCharacters(String[] words, String chars) {
        if (chars == null || chars.length() == 0){
            return 0;
        }
        char[] array = chars.toCharArray();
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            list.add(array[i]);
        }
        ArrayList<Character> subList = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < words.length; i++) {
            subList.clear();
            subList.addAll(list);
            for (int j = 0; j < words[i].length(); j++) {
                if (subList.contains(words[i].charAt(j))){
                    subList.remove((Character) words[i].charAt(j));
                } else {
                    break;
                }
                if (j == words[i].length() - 1){
                    count += words[i].length();
                }
            }
        }
        return count;
    }
公開された98元の記事 ウォンの賞賛0 ビュー2194

おすすめ

転載: blog.csdn.net/weixin_43221993/article/details/104915209