Leetcode_389_找不同_水题

12/18

hashmap速度是真的慢,下次这种固定长度的题目直接用数组存就完事了
class Solution {
    
    
    public char findTheDifference(String s, String t) {
    
    
        Map<Character,Integer>m=new HashMap<>();
        for(int i=0;i<s.length();i++){
    
    
            m.put(s.charAt(i),m.getOrDefault(s.charAt(i),0)+1);
        }
        for(int i=0;i<t.length();i++){
    
    
            int n=m.get(t.charAt(i));
            if(n==0){
    
    
                return t.charAt(i);
            }else{
    
    
                m.put(t.charAt(i),n-1);
            }
        }
        return 'a';
    }
}

猜你喜欢

转载自blog.csdn.net/HDUCheater/article/details/111352513