找不同

此博客链接:

找不同

题目链接:https://leetcode-cn.com/problems/find-the-difference/

题目

给定两个字符串 s 和 t,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

 

示例 1:

输入:s = "abcd", t = "abcde"
输出:"e"
解释:'e' 是那个被添加的字母。
示例 2:

输入:s = "", t = "y"
输出:"y"
示例 3:

输入:s = "a", t = "aa"
输出:"a"
示例 4:

输入:s = "ae", t = "aea"
输出:"a"

 

题解

使用两个哈希表,把字符串分别存到两个哈希表中,判断哈希表中相同字符的个数是否相同。

不相同则说明多的是不相同的字符。不相同有三种不相同。

1.一个字符串为空,另外一个字符串只含有一个字符,则这个字符就是多的字符。

2.两个哈希表中的字符是一样的,但是个数不一样,则说明这个字符就是多的字符。

3.两个哈希表中,一个哈希表存在某个字符,另外一个哈希表不存在这个字符,则说明这个字符是多的字符。

代码

class Solution {
    public char findTheDifference(String s, String t) {
        if(s==null)
        {
            return t.charAt(0);
        }
        Map<String,Integer> map1=new HashMap();
        Map<String,Integer> map2=new HashMap();
        char reslut='0';
        for(int i=0;i<s.length();i++){
            Integer count1=map1.get(s.charAt(i));
            if(count1==null)
            {
                map1.put(s.charAt(i)+"",1);
            }
            else
                map1.put(s.charAt(i)+"",count1++);
        }
        for(int i=0;i<t.length();i++){
            Integer count2=map2.get(t.charAt(i));
            if(count2==null)
            {
                map2.put(t.charAt(i)+"",1);
            }
            else
                map2.put(t.charAt(i)+"",count2++);
        }
        for(String temp1:map1.keySet()){
           for(String temp2:map2.keySet()){
                 if(map1.get(temp1)==map2.get(temp2))
                     break;
                 if(map1.get(temp1)<map2.get(temp2)){
                 
                   
                         reslut=temp1.charAt(0);
                     
                 }
                 if(map1.get(temp2)==null&&map2.get(temp2)!=null)
                 { 
                      reslut=temp2.charAt(0);
                 }
              } 
          
        }
    return reslut;
    }
}

结果

但是第一个结果就不对,今天没有心情,睡觉吧。

 修改代码

以上代码出现两个致命问题

问题1 在判断字母长度时,没有对是否是相等字符做判断。

问题2在双重遍历哈希表时,外层循环应该是写字母多的哈希表,内层循环应该写字母少的哈希表。

正确代码

class Solution {
    public char findTheDifference(String s, String t) {
         if(s.length()==0)
        {
            return t.toCharArray()[0];
        }
        Map<Character,Integer> map1=new HashMap();
        Map<Character,Integer> map2=new HashMap();
        char reslut='0';
        for(int i=0;i<s.length();i++){
            Integer count1=map1.get(s.charAt(i));
            if(count1==null)
            {
                map1.put(s.charAt(i),1);
            }
            else
                map1.put(s.charAt(i),++count1);
        }
        for(int i=0;i<t.length();i++){
            Integer count2=map2.get(t.charAt(i));
            if(count2==null)
            {
                map2.put(t.charAt(i),1);
            }
            else
                map2.put(t.charAt(i),++count2);
        }
        System.out.println(map1);
        System.out.println(map2);
        System.out.println(map1.get('e'));
        System.out.println(map2.get('e'));
        for(Character temp2:map2.keySet()){
           for(Character temp1:map1.keySet()){
                 if(map1.get(temp1)==map2.get(temp2)&&temp1==temp2)
                     break;
                 if(map1.get(temp1)<map2.get(temp2)&&temp1==temp2){
                         reslut=temp1;    
                 }
                 if(map1.get(temp2)==null&&map2.get(temp2)!=null)
                 { 
                      reslut=temp2;
                 }
              } 
          
        }
    return reslut;
    
    }
}

结果

 

posted @ 2020-12-19 00:26  萍2樱释  阅读(250)  评论(8编辑  收藏  举报