LeetCode 6-10 题解

6 中等

Soluion

  • 模拟规律题
  • 找同一行相邻两个字符再原字符串中的间隔

Sample Code (map)

class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows == 1) return s;
        string res = "";
        int len = s.length();
        for(int i = 1; i <= numRows; ++i){
            for(int j = i; j <= len; j += 2 * numRows - 2){
                res += s[j - 1];
                if(i == 1 || i == numRows) continue;
                if(j + 2 * numRows - 2 * i <= len)
                    res += s[j + 2 * numRows - 2 * i - 1];
            }
        }
        return res;
    }
};

7 简单

Soluion

  • 取符号+取正数反转
  • 判断范围

Sample Code

class Solution {
public:
    int reverse(int x) {
        bool flag = false; 
        long long xx = (long long)x;
        if(x < 0) {
            xx = -xx;
            flag = true;
        }
        long long reverse_xx = 0;
        while(xx){
            int bit = xx % 10;
            reverse_xx = reverse_xx * 10 + bit;
            xx /= 10;
        }
        //0x7fffffff, 0x80000000
        long long up = (long long)1 << 31;
        if(reverse_xx  > up) return 0;
        if(reverse_xx == up && flag == false) return 0;
        return (flag ? -1 : 1) * reverse_xx;
    }
    int
};

8 中等

Soluion

  • 设定不同字符的读入状态
  • 特判符号
  • 模拟

Sample Code (map)

class Solution {
public:
    int getState(char s){
        if(s >= '0' && s <= '9') return 2;
        if(s == ' ') return 0;
        if(s == '-' || s == '+') return 1;
        return -1;
    }
    int myAtoi(string s) {
        int state = 0; long long res = 0; int flag = 1;
        for(int i = 0; s[i]; ++i){
            int curState = getState(s[i]);
            if(curState < state) return flag * res;
            state = curState;
            if(curState == 1){
                flag = s[i] == '-' ? -1 : 1;
                ++state;
            }else{
                if(curState == 2){
                    res = 10 * res + (s[i] - '0');
                    if(res > (long long)0x7fffffff && flag == 1) return 0x7fffffff;
                    if(res > (long long)0x80000000 && flag == -1) return 0x80000000;
                }
            }
        }
        return flag * res;
    }
};
/*
state = 0; 前导空格状态
state = 1; 检查符号状态
state = 2; 读入数字状态
*/

9 简单

Soluion

  • 反转即可
  • 特判符号

Sample Code (map)

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false;
        long long xx = (long long)x;
        long long t = 0;
        while(xx){
            int bit = xx % 10;
            xx /= 10;
            t = 10 * t + bit;
        }
        return (long long)x == t;
    }
};

10 困难

Soluion

  • dp
  • 预处理*的匹配

Sample Code (map)

class Solution {
public:
    int dp[1010][1010];
    bool charMatch(char a, char b){
        return a == b || b == '.';
    }
    bool isMatch(string s, string p) {
        int len1 = s.length();
        int len2 = p.length();
        dp[0][0] = 1;
        int cnt = 0;
        for(int i = 0; i < len2; ++i){
            if(p[i] == '*') ++cnt;
            else --cnt;
            if(cnt == 0) dp[0][i + 1] = 1;
            else dp[0][i + 1] = 0;
        }
        for(int i = 0; i < len1; ++i)
            for(int j = 0; j < len2; ++j){
                if(charMatch(s[i], p[j])) dp[i + 1][j + 1] = dp[i][j];
                else {
                    if(p[j] == '*'){
                        int k;
                        for(k = i; k >= 0 && charMatch(s[k], p[j - 1]); --k)
                            dp[i + 1][j + 1] |= dp[k][j - 1];
                        // if(k >= 0)
                        dp[i + 1][j + 1] |= dp[i + 1][j - 1];
                    }
                    else dp[i + 1][j + 1] = 0;
                }
            }
        return dp[len1][len2] == 1;
    }
};

posted @ 2021-09-15 10:03  ADRUI  阅读(37)  评论(0编辑  收藏  举报