其他分享
首页 > 其他分享> > 2021-05-18Leetcode205/299/385/136

2021-05-18Leetcode205/299/385/136

作者:互联网

 

为什么只有再i+1时才能实现呢? i+3都行??

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int ss[126] = {0};
        int tt[126] = {0};
        if(s.size() != t.size()) return false;
        for(int i=0; i<s.size(); i++){
            ss[s[i]] ++;
            tt[t[i]] ++;
            if(ss[s[i]] != tt[t[i]]) return false;
        }
        return true;
    }
};

//只能通过31、29?
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int ss[126] = {0};
        int tt[126] = {0};
        if(s.size() != t.size()) return false;
        for(int i=0; i<s.size(); i++){
            ss[s[i]] += i;
            tt[t[i]] += i;
            if(ss[s[i]] != tt[t[i]]) return false;
        }
        return true;
    }
};

//34/39?

好思路如下
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        vector<int> sd(256,0), td(256, 0);
        if(s.length() != t.length()) return false;
        for(int i = 0; i<s.length(); i++){
            if(sd[s[i]] != td[t[i]]){
                return false;
            }
            sd[s[i]] = td[t[i]] = i+1;
        }
        return true;
    }
};

290.如何分割?

        vector<string>word;
        string t;
        for(auto c:s){
            if(c==' '){
                word.push_back(t);
                t.clear();
            }else
                t+=c;
        }
        if(!t.empty())word.push_back(t);
class Solution {
public:
    bool wordPattern(string pattern, string s) {
        vector<string> vs;
        string word;
        for(auto a: s){
            if(a==' '){
                vs.push_back(word);
                word.clear();
            } 
            else word += a;
        }
        if(word.size() != 0) vs.push_back(word);
        if(vs.size() != pattern.size()) return false;

        map<string, int> smap;
        map<char, int> pmap;
        for(int i=0; i<pattern.size(); i++){
            if(smap[vs[i]] != pmap[pattern[i]]) return false;
            smap[vs[i]] = pmap[pattern[i]] = i+1;
        }
        return true;
    }
};

299.

class Solution {
public:
    string getHint(string secret, string guess) {
        int A =0, B=0;
        unordered_map<char, int> cows;
        string rest;
        for(int i=0; i< secret.size(); i++){
            if(secret[i] == guess[i]) A++;
            else{
                cows[secret[i]] ++;
                rest += guess[i];
            }
        }
        for(auto a: rest){
            if(cows[a]){
                B++;
                cows[a]--;
            }
        }
        return to_string(A)+'A'+to_string(B)+'B';
    }
};

385

class Solution {
public:
    char findTheDifference(string s, string t) {
        int ss[26] = {0};
        int tt[26] = {0};
        char ans;
        for(auto a: s) ss[a-'a']++;
        for(auto a: t) tt[a-'a']++;
        for(int i=0; i<26; i++){
            if(ss[i] != tt[i]){
                ans = ('a'+i);
            }
        }
        return ans;
    }
};

136

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int ans = 0;
        for(auto a: nums){
            ans ^= a;
        }
        return ans;
    }
};

 

标签:word,string,05,int,auto,18Leetcode205,++,385,size
来源: https://blog.csdn.net/weixin_51081223/article/details/117002813