查找表, 242,202,290,205,451

这道题已知字符串只有小写字母。可以使用map把字符和对应的次数联系起来。若在s[i]中的字符counts++; 若在t[i]中的字符counts--。

最后来遍历counts判断每个字符的键值,若为0说明t也有s中的对应字符,否则没有返回false。

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length()!=t.length()) return false;
        unordered_map<char, int> counts;
        for(int i=0;i<s.length();i++){
            counts[s[i]]++;
            counts[t[i]]--;
        }
        for(auto count:counts){
            if(count.second) return false;  //若键值不为0说明counts里面有s的字符但不在t里
        }
        return true;
    }
};

class Solution {
public:
    bool isHappy(int n) {
        //n为输入值
        set<int> s;
        while(n!=1){
            int t = 0;
            while(n){
                //计算n的各个位上的平方和
                t += (n%10) * (n%10);   //平方
                n /= 10;
            }
            n = t;  
            if(s.count(n)) break;   //count() 用来查找set中某个某个键值出现的次数,若出现过跳出当前循环
            else s.insert(n);   //若不存在,加入到s中
        }
        return n==1;  //判断n若为1返回true
    }
};

 思路:1)若可以在m中找到在pattern里面的字符,它所对应的值与word不相等,返回false;

2)否则,没有在m中找到pattern里面的字符,再遍历一遍m,若能找到对应的word值,说明键对应错了,返回false;

3)否则,将pattern和对应的word插入;

4)检查pattern和str长度是否相同。

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        unordered_map<char, string> m;
        istringstream is(str);  
        int i = 0;
        for(string word; is >> word; i++){
           //将str按空格分隔
            if(m.find(pattern[i]) != m.end()){
                //在m中找到了pattern对应的字符
                if(m[pattern[i]] != word) 
                    //当pattern对应字符在m中对应的值不等于word 时
                    return false;
            }
            else{
                for(unordered_map<char, string> ::iterator it = m.begin();it!=m.end();it++){
                    if(it->second == word) return false;  //str在m中对应到了其他元素
                }
                m[pattern[i]] = word;
            }
        }
        return i == pattern.size();
    }
};

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        unordered_map<char, char> m;
        for(int i=0;i<s.length();i++){
            if(m.find(s[i]) != m.end()){
                if(m[s[i]] != t[i]) 
                    return false;
            }
            else{
                for(unordered_map<char, char> :: iterator it = m.begin(); it!=m.end();it++){
                    if(it->second == t[i]) return false;
                }
                m[s[i]] = t[i];
            }
        }
        return true;
    }
};

思路:先统计出每个字符出现的个数,再利用优先队列的自动排序的特点,把个数和字符组成的pair放到优先队列里排好序后,再取出来组成结果res即可。

class Solution {
public:
    string frequencySort(string s) {
        string res = "";
        priority_queue<pair<int, char>> q;   //个数和字符组成的pair
        unordered_map<char, int> m;
        for(char c:s) m[c]++;  //统计s中出现的字符个数
        for(auto a:m) q.push({a.second, a.first});
        while(!q.empty()){
            auto t = q.top();
            q.pop();
            res.append(t.first, t.second);   //将t从first到second之间的数添加到res后
        }
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/Bella2017/p/10161141.html
今日推荐