首页 > 其他 > 详细

leetcode 205. Isomorphic Strings(哈希表)

时间:2016-04-25 06:40:25      阅读:207      评论:0      收藏:0      [点我收藏+]

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg""add", return true.

Given "foo""bar", return false.

Given "paper""title", return true.

题解:显然是哈希查找,但是其实没必要用map,一共就255个字符,每次只记录它现在的位置就好(因为之前的位置都比较过了)。

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int hs[256]={0};
        int ht[256]={0};
        int ls=s.length(),lt=t.length();
        if(ls!=lt) return false;
        for(int i=0;i<ls;i++){
            if(hs[s[i]]!=ht[t[i]]){
                return false;
            }
            hs[s[i]]=i+1;
            ht[t[i]]=i+1;
        }
        return true;
    }
};

 

leetcode 205. Isomorphic Strings(哈希表)

原文:http://www.cnblogs.com/zywscq/p/5429131.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!