LeetCode日记20200215

LeetCode日记2020.2.15


字符串专题+一道 hard

84 柱状图中的最大矩形(hard)

单调栈应用,比较老的题了。

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        pair<int, int> rect;
        stack<pair<int, int>> st;
        int maxA = 0;
        for(int i=0;i<heights.size(); ++i)
        {
            int idx = i, h = heights[i];
            while(!st.empty() && st.top().first > h)
            {
                int area = st.top().first * (i - st.top().second);
                if(area > maxA)
                    maxA = area;

                idx = st.top().second;
                st.pop();
            }
            st.push(make_pair(h, idx));
        }
        while(!st.empty())
        {
            int area = st.top().first * (heights.size() - st.top().second);
            if(area > maxA)
                maxA = area;
            
            st.pop();
        }
        return maxA;
    }
};

890 查找和替换模式(mid)

class Solution {
public:
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
        string sp = parseP(pattern);
        vector<string> res;
        for(const auto& w: words)
        {
            string wp = parseP(w);
            if(wp == sp)
                res.push_back(w);
        }
        return res;
    }

    string parseP(const string& w)
    {
        string wMap, sp;
        for(auto p: w)
        {
            auto pos = wMap.find(p);
            if(pos == string::npos)
            {
                sp.append(1, char(wMap.size()) + 'a'); 
                wMap.append(1, p);
            }
            else
                sp.append(1, char(pos) + 'a');
        }
        return sp;
    }
};

537 复数乘法(mid)

class Solution {
public:
    string complexNumberMultiply(string a, string b) {
        auto ia = parse(a), ib = parse(b);
        string res;
        res += to_string(ia[0]*ib[0] - ia[1]*ib[1]);
        res.push_back('+');
        res += to_string(ia[1]*ib[0]+ib[1]*ia[0]);
        res.push_back('i');
        return res;
    }

    vector<int> parse(const string& e)
    {
        vector<int> res;
        auto pos = e.find('+');
        res.push_back(stoi(e.substr(0, pos)));
        auto pos2 = e.find('i');
        res.push_back(stoi(e.substr(pos+1, pos2)));
        return res;
    }
};

791 自定义字符串排序(mid)

class Solution {
public:
    string customSortString(string S, string T) {
        int h[26];
        memset(h, 0, sizeof(h));
        for(int i=0;i<S.size(); ++i)
            h[S[i]-'a']=i;

        sort(T.begin(), T.end(), [h](char i1, char i2){
            return h[i1-'a']<h[i2-'a'];
        });
        return T;
    }
};

12 整数转罗马数字(mid)

突发奇想用递归做做。

class Solution {
public:
    string intToRoman(int num) {
        if(num>=1000)
        {
            return string(num/1000, 'M') + intToRoman(num%1000);
        }
        else if(num>=100)
        {
            int Ccnt = num /100;
            if(Ccnt == 9)
                return string("CM") + intToRoman(num-900);
            else if(Ccnt >= 5)
                return string("D") + string(Ccnt-5, 'C') + intToRoman(num-Ccnt*100);
            else if(Ccnt == 4)
                return string("CD") + intToRoman(num-400);
            else
                return string(Ccnt, 'C') + intToRoman(num - Ccnt*100);     
        }
        else if(num>=10)
        {
            int Ccnt = num /10;
            if(Ccnt == 9)
                return string("XC") + intToRoman(num-90);
            else if(Ccnt >= 5)
                return string("L") + string(Ccnt-5, 'X') + intToRoman(num-Ccnt*10);
            else if(Ccnt == 4)
                return string("XL") + intToRoman(num-40);
            else
                return string(Ccnt, 'X') + intToRoman(num - Ccnt*10);   
        }
        else
        {
            if(num == 9)
                return string("IX");
            else if(num >= 5)
                return string("V") + string(num-5, 'I');
            else if(num == 4)
                return string("IV");
            else
                return string(num, 'I');
        }
    }
};

647 回文子串(mid)

找个时间集中训练下动态规划。

class Solution {
public:
    int countSubstrings(string s) {
        vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));
        for(int i=0;i<s.size();++i)
            dp[i][i] = 1;

        int cnt = s.size();
        for(int i=1;i<s.size();++i)
        {
            for(int j=0;j<s.size()-i;++j)
            {
                if(s[j]==s[j+i] && (i==1 || dp[j+1][j+i-1]))
                {
                    ++cnt;
                    dp[j][j+i] = 1;
                }
            }
        }
        return cnt;
    }
};

777 在LR字符串中交换相邻字符(mid)

这题最关键的是要理解到L可以向左移动直到遇到另一个不为X的字符,同理R

class Solution {
public:
    bool canTransform(string start, string end) {
        if(start.size()!=end.size())
            return false;
        
        auto posE = end.find_first_not_of("X");
        auto posS = start.find_first_not_of("X");
        while(posE!=string::npos)
        {
            if(posS == string::npos || start[posS] != end[posE])
                return false;

            if(end[posE] == 'L')
            {
                if(posS<posE)
                    return false;
            }
            else
            {
                if(posE<posS)
                    return false;
            }
            posS = start.find_first_not_of("X", posS+1);
            posE = end.find_first_not_of("X", posE+1);
        }
        if(posS!=string::npos)
            return false;

        return true;
    }
};

1035 不相交的线(mid)

要看到问题的实质——最长子串

class Solution {
public:
    int maxUncrossedLines(vector<int>& A, vector<int>& B) {
        vector<vector<int>> dp(A.size() + 1, vector<int>(B.size() + 1, 0));
        for(int i=0; i<A.size(); ++i)
        {
            for(int j=0;j<B.size();++j)
            {
                if(A[i] == B[j])
                {
                    dp[i+1][j+1]=dp[i][j]+1;
                }
                else
                {
                    dp[i+1][j+1]=max(dp[i][j+1], dp[i+1][j]);
                }
            }
        }
        return dp[A.size()][B.size()];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值