美文网首页
2019-01-30第五天

2019-01-30第五天

作者: 织雾呀 | 来源:发表于2019-01-30 14:09 被阅读0次

昨天在高铁上坐了一天,好累。。。
今天做的leetcode 的题是
题目:

单词模式

这道题还是比较简单的嘛,我很快就做出来了

    public static boolean wordPattern(String pattern, String str) {
        String [] strStrs = str.split(" ");
        char [] patternChars = pattern.toCharArray();
        if(strStrs.length!=patternChars.length) {
            return false;
        }
        Map<Character,String> tempMap = new HashMap<Character, String>();
        for (int i = 0; i < strStrs.length; i++) {
            if(tempMap.get(patternChars[i])==null) {
                if(tempMap.containsValue(strStrs[i])) {
                    return false;
                }
                tempMap.put(patternChars[i], strStrs[i]);
                
            }else {
                if (!(tempMap.get(patternChars[i])+"").equals(strStrs[i])) {
                    return false;
                }
            }
        }
        return true;
    }

美中不足的就是运行时间不如人意啊

image.png

相关文章

网友评论

      本文标题:2019-01-30第五天

      本文链接:https://www.haomeiwen.com/subject/qoxcsqtx.html