【leetcode】1160. 拼写单词( JAVA + 内存地址共享)

该代码片段展示了一个Java方法,用于计算给定单词数组中,仅使用特定字符集可以完整构成的单词数量。关键在于使用HashMap存储字符计数,并通过克隆来避免修改原始映射。在遍历每个单词时,检查字符是否存在于克隆的映射中,若不存在或计数为0,则停止检查当前单词。
摘要由CSDN通过智能技术生成

在这里插入图片描述

class Solution {
    public int countCharacters(String[] words, String chars){
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        for(int i = 0; i < chars.length(); i++){
            char c = chars.charAt(i);
            if(!map.containsKey(c)){
                map.put(c, 1);
            }else{
                Integer count = map.get(c);
                map.put(c, ++count);
            }
        }

        int ans = 0;
        for(String word : words){
            HashMap<Character, Integer> map_clone =  (HashMap<Character, Integer>) map.clone();

            for(int i = 0; i < word.length(); i++){
                if(!map_clone.containsKey(word.charAt(i))){ // 不包含该字符
                    break;
                }else{
                    int num = map_clone.get(word.charAt(i));
                    if(num == 0){
                        break;
                    }else{
                        map_clone.put(word.charAt(i), --num);
                    }
                }
                if(i == word.length()-1){
                    ans += word.length();
                }
            }
        }
        return ans;
    }
}

本地测试

package leetcode;

import java.net.Inet4Address;
import java.util.HashMap;
import org.openjdk.jol.vm.VM;

public class lc_1160_countCharacters {
    public static void main(String[] args) {

        String[] words = {"cat","bt","hat","tree"};
        String chars = "atach";

//        String[] words = {"hello","world","leetcode"};
//        String chars = "welldonehoneyr";

        lc_1160_countCharacters obj = new lc_1160_countCharacters();
        int ans = obj.countCharacters(words, chars);
        System.out.println(ans);

    }

    public int countCharacters(String[] words, String chars){
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        for(int i = 0; i < chars.length(); i++){
            char c = chars.charAt(i);
            if(!map.containsKey(c)){
                map.put(c, 1);
            }else{
                Integer count = map.get(c);
                map.put(c, ++count);
            }
        }

        int ans = 0;
        for(String word : words){
        	// HashMap<Character, Integer> map_clone =  new HashMap<Character, Integer>(); // 也可以,map_clone和map内存地址不共享
            HashMap<Character, Integer> map_clone =  (HashMap<Character, Integer>) map.clone(); //map_clone和map内存地址不共享
            // 注意不能写 HashMap<Character, Integer> map_clone = map,此时,map_clone和map共享一块内存地址
//            System.out.println(VM.current().addressOf(map));
//            System.out.println(VM.current().addressOf(map_clone));

//            System.out.println(map.equals(map_clone));

            for(int i = 0; i < word.length(); i++){
                if(!map_clone.containsKey(word.charAt(i))){ // 不包含该字符
                    break;
                }else{
                    int num = map_clone.get(word.charAt(i));
                    if(num == 0){
                        break;
                    }else{
                        map_clone.put(word.charAt(i), --num);
                    }
                }
                if(i == word.length()-1){
                    ans += word.length();
                }
            }
        }
        return ans;
    }
}

注意

内存地址共享问题,详见代码注释
好文推荐:Java中equals与“==”的区别详解

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值