LeetCode刷题分类

以下代码都来自该博客:

博客链接:https://www.cnblogs.com/grandyang/p/4606334.html

一、回文串相关

//验证是否为回文字符串(125)
class Solution {
public:
    bool isPalindrome(string s) {
        int left = 0, right = s.size() - 1 ;
        while (left < right) {
            if (!isAlphaNum(s[left])) ++left;
            else if (!isAlphaNum(s[right])) --right;
            else if ((s[left] + 32 - 'a') %32 != (s[right] + 32 - 'a') % 32) return false;
            else {
                ++left; --right;
            }
        }
        return true;
    }
    bool isAlphaNum(char &ch) {
        if (ch >= 'a' && ch <= 'z') return true;
        if (ch >= 'A' && ch <= 'Z') return true;
        if (ch >= '0' && ch <= '9') return true;
        return false;
    }
};
//求最长回文子串(5)
class Solution {
public:
    string longestPalindrome(string s) {
        string t ="$#";
        for (int i = 0; i < s.size(); ++i) {
            t += s[i];
            t += '#';
        }
        int p[t.size()] = {0}, id = 0, mx = 0, resId = 0, resMx = 0;
        for (int i = 1; i < t.size(); ++i) {
            p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
            while (t[i + p[i]] == t[i - p[i]]) ++p[i];
            if (mx < i + p[i]) {
                mx = i + p[i];
                id = i;
            }
            if (resMx < p[i]) {
                resMx = p[i];
                resId = i;
            }
        }
        return s.substr((resId - resMx) / 2, resMx - 1);
    }
};

二、动态规划问题相关

//爬楼梯问题(70)
class Solution {
public:
    int climbStairs(int n) {
        int dp[n] = {0};
        if(n==1) return 1;
        if(n==2) return 2;
        dp[0] = 1;
        dp[1] = 2;
        for(int i=2;i
//不同的路径(62)
class Solution {
public:
    int uniquePaths(int m, int n) {
        vector> dp(m+1,vector(n+1,0));
        dp[0][1] = 1;
        for(int i =1;i<=m;i++){
            for(int j = 1;j<=n;j++){
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[m][n];
    }
};
//不同的路径II-有障碍物(63)
class Solution {
public:
    int uniquePathsWithObstacles(vector>& obstacleGrid) {
        if (obstacleGrid.empty() || obstacleGrid[0].empty() || obstacleGrid[0][0] == 1) return 0;
        int m = obstacleGrid.size(), n = obstacleGrid[0].size();
        vector> dp(m + 1, vector(n + 1, 0));
        dp[0][1] = 1;
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (obstacleGrid[i - 1][j - 1] != 0) continue;
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m][n];
    }
};

//最小路径和(64)
class Solution {
public:
    int minPathSum(vector>& grid) {
        int m = grid.size(),n = grid[0].size();
        vector> dp(m,vector(n));
        dp[0][0]  = grid[0][0];
        for(int i=1;i

三、删除重复元素

//删除排序数组中的重复项(26)
class Solution {
public:
    int removeDuplicates(vector& nums) {
        if(nums.empty()) return 0;
        int slow = 0, quick = 0, n = nums.size();
        while(quick
//删除排序数组中的重复项II(80)
class Solution {
public:
    int removeDuplicates(vector& nums) {
        if(nums.size() <= 2) return nums.size();
        int end = 2, n = nums.size();
        for(int i = 2;i
//删除链表中的重复元素(83)
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *cur = head;
        while(cur && cur->next){
            if(cur->val == cur->next->val) cur->next = cur->next->next;
            else cur = cur->next;
        }
        return head;
    }
};
// 删除排序链表中的重复元素 II(82)
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if (!head || !head->next) return head;
        ListNode *dummy = new ListNode(-1), *pre = dummy;
        dummy->next = head;
        while (pre->next) {
            ListNode *cur = pre->next;
            while (cur->next && cur->next->val == cur->val) {
                cur = cur->next;
            }
            if (cur != pre->next) pre->next = cur->next;
            else pre = pre->next;
        }
        return dummy->next;
    }
};

四、二叉树相关内容

//判断是否为相同的树(100)
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(!p && !q) return true;
        if((p && !q) || (!p && q) || (p->val != q->val)) return false;
        return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
    }
};
//判断是否为对称二叉树(101)
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if (!root) return true;
        return symmetric(root->left, root->right);
    }
    bool symmetric(TreeNode *left, TreeNode *right) {
        if (!left && !right) return true;
        if (left && !right || !left && right || left->val != right->val) return false;
        return symmetric(left->left, right->right) && symmetric(left->right, right->left);
    }
    
};

你可能感兴趣的:(C++学习)