week4

class Solution {
public:
    string countAndSay(int n) {
        string s="1";
        for(int i=2;i<=n;i++)
        {
            string ns;
            for(int k=0;k<s.length();)
            {
                int j=k+1;
                while(s[j]==s[k]&&j<s.length())
                    j++;
                ns+=to_string(j-k)+s[k];
                k=j;
            }
            s=ns;
        }
        return s;
    }
};
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>> hash;
        for(auto str:strs)
        {
            string key=str;
            sort(key.begin(),key.end());
            hash[key].push_back(str);
        }

        vector<vector<string>> ans;
        for(auto item:hash)
            ans.push_back(item.second);

        return ans;
    }
};
class Solution {
public:
    string reverseWords(string s) {
        int k=0;
        for(int i=0;i<s.size();i++)
        {
            while(i<s.size() && s[i]==' ') i++;
            if(i==s.size()) break;
            int j=i;
            while(j<s.size() && s[j]!=' ')j++;
            reverse(s.begin()+i,s.begin()+j);
            if(k) s[k++]=' ';
            while(i<j) s[k++]=s[i++];
        }
        s.erase(s.begin()+k,s.end());
        reverse(s.begin(),s.end());
        return s;
    }
    
};
class Solution {
public:
    int compareVersion(string s1, string s2) {
        int i=0,j=0;
        while(i<s1.size() || j<s2.size())
        {
            int x=i,y=j;
            while(x<s1.size() && s1[x]!='.') x++;
            while(y<s2.size() && s2[y]!='.') y++;
            int a= x==i?0:atoi(s1.substr(i,x-i).c_str());
            int b= y==j?0:atoi(s2.substr(j,y-j).c_str());
            if(a>b) return 1;
            if(a<b) return -1;
            i=x+1,j=y+1;
        }
        return 0;
    }
};
class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        unordered_set<string> hash;
        for(auto email:emails)
        {
            string name;
            int at=email.find('@');
            for(auto c:email.substr(0,at))
            {
                if(c=='+') break;
                if(c!='.') name+=c; 
            }
            string domain=email.substr(at+1);
            hash.insert(name+'@'+domain);
        }
        return hash.size();
    }
};
class Solution {
public:
    string longestPalindrome(string s) {
        string res;
        for(int i=0;i<s.size();i++)
        {
            for(int j=i,k=i;j>=0 && k<s.size()&&s[j]==s[k];j--,k++)
                if(res.size()<k-j+1)
                    res=s.substr(j,k-j+1);
            for(int j=i,k=i+1;j>=0 && k<s.size()&&s[j]==s[k];j--,k++)
                if(res.size()<k-j+1)
                    res=s.substr(j,k-j+1);
        }
        return res;
    }
};
class Solution {
public:
    string convert(string s, int n) {
       if(n==1) return s;
       string res;
       for(int i=0;i<n;i++)
       {
           if(!i || i==n-1)
           {
               for(int k=i;k<s.size();k+=2*(n-1))
                res+=s[k];
           }else
           {
               for(int j=i,k=2*(n-1)-i;j<s.size()||k<s.size();j+=2*(n-1),k+=2*(n-1))
               {
                   if(j<s.size()) res+=s[j];
                   if(k<s.size()) res+=s[k];
               }
           }
       }
       return res;

    }
};
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char,int> hash;
        int res=0;
        for(int i=0,j=0;i<s.size();i++)
        {
            hash[s[i]]++;
            while(hash[s[i]]>1) hash[s[j++]]--;
            res=max(res,i-j+1);
        }
        return res;
    }
};
class Trie {
public:
    struct Node
    {
        bool is_end;
        Node* son[26];
        Node()
        {
            is_end=false;
            for(int i=0;i<26;i++) son[i]=NULL;
        }
    }*root;

    /** Initialize your data structure here. */
    Trie() {
        root=new Node();
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        auto p=root;
        for(auto c:word)
        {
            int n=c-'a';
            if(p->son[n]==NULL) p->son[n]=new Node();
            p=p->son[n];
        }
        p->is_end=true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        auto p=root;
        for(auto c:word)
        {
            int n=c-'a';
            if(p->son[n]==NULL) return false;
            p=p->son[n];
        }
        return p->is_end;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        auto p=root;
        for(auto c:prefix)
        {
            int n=c-'a';
            if(p->son[n]==NULL) return false;
            p=p->son[n];
        }
        return true;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值