zoukankan      html  css  js  c++  java
  • LeetCode 28. 拼写单词 HashMap赋值给另一个HashMap

    题目描述

    给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。

    假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。

    注意:每次拼写时,chars 中的每个字母都只能用一次。

    返回词汇表 words 中你掌握的所有单词的 长度之和。

    示例 1:

    输入:words = ["cat","bt","hat","tree"], chars = "atach"
    输出:6
    解释:
    可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。


    示例 2:

    输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
    输出:10
    解释:
    可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。
     

    提示:

    1 <= words.length <= 1000
    1 <= words[i].length, chars.length <= 100
    所有字符串中都仅包含小写英文字母

    解题思路

    用一个hashmap来维持chars 里面的各个字符,如下代码所示,为tmpMap

    for (int i = 0; i < chars.length(); i++) {
                if (tmpMap.get(chars.charAt(i))==null) {
                    tmpMap.put(chars.charAt(i), 1);
                }else {
                    tmpMap.put(chars.charAt(i), tmpMap.get(chars.charAt(i))+1);
                }
            }

    由于每次遍历完数组中的一个字符串,都要将map更新为初始的tmpMap,这就要考虑到HashMap赋值给另一个HashMap的问题了

    这时是不能使用map==tmpMap的,因为实际上这里是指向同一个地址的,如map改变了,tmpMap也跟着改变了,这就不能维持一个初始的tmpMap了

    1。建议clone()方法创建新的testMap

    2. 将第一个map的值通过遍历的方式赋值给第二个map,这样你操作任意一个map, 
    另一个map都不会改变。 
    3. 用putAll方法赋值,本质也是 2 中的方法。

    代码如下

    package leetcode;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class CountCharacters {
         public int countCharacters(String[] words, String chars) {
            
            if (words.length==0||chars==null) {
                return 0;
            }
            Map<Character, Integer> tmpMap=new HashMap<Character, Integer>();
            for (int i = 0; i < chars.length(); i++) {
                if (tmpMap.get(chars.charAt(i))==null) {
                    tmpMap.put(chars.charAt(i), 1);
                }else {
                    tmpMap.put(chars.charAt(i), tmpMap.get(chars.charAt(i))+1);
                }
            }
            Map<Character, Integer> map=new HashMap<Character,Integer>();
            //这里是不能用map==tmpMap的
            map.putAll(tmpMap);
            int sumLength=0;
            for (int i = 0; i < words.length; i++) {
                
                boolean flag=true;
                for (int j = 0; j < words[i].length(); j++) {
                    if (map.get(words[i].charAt(j))==null) {
                        flag=false;
                        
                    }else if(map.get(words[i].charAt(j))==0){
                        flag=false;
                        
                    }else {
                        map.put(words[i].charAt(j), map.get(words[i].charAt(j))-1);
                    }
                    
                }
                //这里是不能用map==tmpMap的
                map.putAll(tmpMap);
                if (flag) {
                    sumLength+=words[i].length();
                }
                
            }
             return sumLength;
    
            }
         
         public static void main(String[] args) {
            CountCharacters characters=new CountCharacters();
            System.out.println(characters.countCharacters(new String[] {"cat","bt","hat","tree"}, "atach"));
        }
    }
  • 相关阅读:
    关于SNS网络
    面向对象中的IS A和HAS A 问题
    项目进度检查
    【php】php Apache 怎么创建虚拟目录
    【IE6】iframe在IE6下出现横向滚动条的解决方案
    【javascript基础】Javascript的getYear、getFullYear、getUTCFullYear异同
    【java】JAVA环境配置
    UTF8 GBK UTF8 GB2312 之间的区别和关系
    【转发】10 个技巧助你写出卓越的jQuery插件
    【前端优化】你想不到的压缩方法:将javascript文件压缩成PNG图像存储
  • 原文地址:https://www.cnblogs.com/Transkai/p/12509864.html
Copyright © 2011-2022 走看看