LeetCode 2016 423,412,409,89,29,166,91,79

来源:互联网 发布:软件mt4 编辑:程序博客网 时间:2024/06/05 06:00

423 Reconstruct Original Digits from English

class Solution{public:    void makeSch(vector<pair<int, int> > &sch)    {        sch.push_back(make_pair('g'-'a',8));        sch.push_back(make_pair('u'-'a',4));        sch.push_back(make_pair('w'-'a',2));        sch.push_back(make_pair('x'-'a',6));        sch.push_back(make_pair('z'-'a',0));        sch.push_back(make_pair('f'-'a',5));        sch.push_back(make_pair('h'-'a',3));        sch.push_back(make_pair('i'-'a',9));        sch.push_back(make_pair('s'-'a',7));        sch.push_back(make_pair('n'-'a',1));        return ;    }    void makeMap(vector<string>& mapping,vector<string>& itostr)    {        mapping.push_back("zero");        mapping.push_back("one");        mapping.push_back("two");        mapping.push_back("three");        mapping.push_back("four");        mapping.push_back("five");        mapping.push_back("six");        mapping.push_back("seven");        mapping.push_back("eight");        mapping.push_back("nine");        itostr.push_back("0");        itostr.push_back("1");        itostr.push_back("2");        itostr.push_back("3");        itostr.push_back("4");        itostr.push_back("5");        itostr.push_back("6");        itostr.push_back("7");        itostr.push_back("8");        itostr.push_back("9");        return ;    }    string originalDigits(string s)    {        string ans="";        vector<int> tmpans;        vector<string> mapping;        vector<string> itostr;        makeMap(mapping,itostr);        int nums[30]={0};        int ls=s.size();        for(int i=0;i<ls;i++)        {            nums[s[i]-'a']++;        }        vector<pair<int, int> >sch;        makeSch(sch);        int lsch=sch.size();        for(int i=0;i<lsch;i++)        {            while (nums[sch[i].first]>0)            {                string str=mapping[sch[i].second];                int lstr=str.size();                for(int j=0;j<lstr;j++)                {                    nums[str[j]-'a']--;                }                tmpans.push_back(sch[i].second);            }        }        sort(tmpans.begin(),tmpans.end());        int ltmp=tmpans.size();        for(int j=0;j<ltmp;j++)        {            ans=ans+itostr[tmpans[j]];        }        return ans;    }};


412 Fizz Buzz

class Solution {public:    vector<string> fizzBuzz(int n)    {        vector<string> ans;        string tmp;        for(int i=1;i<=n;i++)        {            if (i%3==0 && i%5==0)                tmp="FizzBuzz";            else            if (i%3==0) tmp="Fizz";            else            if (i%5==0) tmp="Buzz";            else            {                stringstream ss;                ss<<i;                tmp=ss.str();            }            ans.push_back(tmp);        }        return ans;    }};

409 Longest Palindrome

class Solution {public:    int longestPalindrome(string s)    {        bool flag=false;        int ans=0;        int cnt[300]={0};        int ls=s.size();        for(int i=0;i<ls;i++)        {            cnt[s[i]]++;        }        for(char i='a';i<='z';i++)        {            if (cnt[i]%2==0)                ans+=cnt[i];            else            {                ans+=cnt[i]-1;                flag=true;            }        }        for(char i='A';i<='Z';i++)        {            if (cnt[i]%2==0)                ans+=cnt[i];            else            {                ans+=cnt[i]-1;                flag=true;            }        }        if (flag) ans++;        return ans;    }};

89 Gray Code

class Solution{public:    vector<int> grayCode(int n)    {        vector<int> ans;        ans.push_back(0);        if (n==0) return ans;        ans.push_back(1);        if (n==1) return ans;        for(int i=2;i<=n;i++)        {            int len=ans.size();            for(int j=len-1;j>=0;j--)            {                ans.push_back(ans[j]+ (1<<(i-1)) );            }        }        return ans;    }};


29 Divide Two Integers

class Solution {public:    int divide(int dividend, int divisor)    {        int flag = 1;        int ans = 0;        if (dividend>0 && divisor<0) flag=-1;        if (dividend<0 && divisor>0) flag=-1;        if (divisor==0) return INT_MAX;        if (dividend==INT_MIN && divisor==-1) return INT_MAX;        long long up=labs(dividend);        long long down=labs(divisor);        while (up >= down)        {            long long multi=1;            long long tmp=down;            while (up >= (tmp<<1))            {                tmp=tmp<<1;                multi=multi<<1;            }            ans+=multi;            up-=tmp;        }        if (flag>0) return ans;else return -ans;    }};


166 Fraction to Recurring Decimal

class Solution {public:    string fractionToDecimal(int numerator, int denominator)    {        string ans="";        string tmpans="";        long long up=numerator;        long long down=denominator;        if (up*down<0) ans="-";        up=abs(up);down=abs(down);        long long tmp;        stringstream ss;        ss<<(up/down);        ans=ans+ss.str();        up%=down;        if (up==0)        {            return ans;        }        else        {            map<long long,bool> flag;            vector<long long> remainds;            flag[0]=true;            ans+=".";            while ((up>0) && (flag.find(up)==flag.end()))            {                flag[up]=true;                remainds.push_back(up);                up*=10;                tmp=up/down;                stringstream s;                s<<tmp;                tmpans+=s.str();                up=up-tmp*down;            }            if (up==0)            {                ans=ans+tmpans;            }            else            {                int i,lr=remainds.size();                for(i=0;i<lr;i++)                    if (remainds[i]==up) break;                ans=ans+tmpans.substr(0,i)+"("+tmpans.substr(i)+")";            }        }        return ans;    }};


91 Decode Ways

class Solution {public:    int numDecodings(string s)    {        int ls=s.size();        if (ls==0) return 0;        int ans;        int f[ls+5]={0};        f[0]=1;        if (s[0]!='0') f[1]=1;        for(int i=1;i<ls;i++)        {            if (s[i]!='0') f[i+1]=f[i];            int tmp=((s[i-1]-'0')*10+s[i]-'0');            if (tmp>=10 && tmp<=26) f[i+1]+=f[i-1];        }        return f[ls];    }};


79 Word Search

class Solution {public:    string str;    int targetk,row,col;    int dx[4]={-1,0,1,0};    int dy[4]={0,-1,0,1};    char used='#';    vector< vector<char> > grid;    bool flag;    bool exist(vector< vector<char> >& board, string word)    {        row=board.size();        if (row==0) return flag;        col=board[0].size();        flag=false;        str=word;        targetk=str.size();        grid.resize(row+2);        for(int i=0;i<=col+1;i++)            grid[0].push_back(used);        for(int i=0;i<row;i++)        {            grid[i+1].push_back(used);            for(int j=0;j<col;j++)                grid[i+1].push_back(board[i][j]);            grid[i+1].push_back(used);        }        for(int i=0;i<=col+1;i++)            grid[row+1].push_back(used);        col+=2;row+=2;        for(int i=0;i<row;i++)        {            for(int j=0;j<col;j++)            {                if (grid[i][j]==str[0])                {                    char saved = grid[i][j];                    grid[i][j]=used;                    depthSearsh(i,j,1);                    grid[i][j]=saved;                    if (flag) break;                }            }            if (flag) break;        }        return flag;    }    void depthSearsh(int x,int y,int k)    {        if (k==targetk)        {            flag=true;            return ;        }        for(int i=0;i<4;i++)        {            char saved;            int tmpx=x+dx[i],tmpy=y+dy[i];            if (grid[tmpx][tmpy]==str[k])            {                saved=grid[tmpx][tmpy];                grid[tmpx][tmpy]=used;                depthSearsh(tmpx,tmpy,k+1);                if (flag) return ;                grid[tmpx][tmpy]=saved;            }        }        return ;    }};





0 0