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.

  • 先找到所有相同的出现的次数
  • 不同的,我们建立一个map,来记录出现的次数,这个map必须遍历完一次,完整了才可以!!
  • 注意一边+1,还要把字典中刚比较的那个擦去-1
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;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值