lls

51 阅读1分钟

1 回文字符串

  • isnum() 判断是否为数字
  • isalpha() 判断是否为字母
  • isalnum() 判断是否为数字或字母
    首尾双指针遍历,除掉无用的字符,判断对应的字符是否相同。
class Solution {
public:
    bool isPalindrome(string s) {
        string sgood;
        for (char ch: s) {
            if (isalnum(ch)) {
                sgood += tolower(ch);
            }
        }
        int n = sgood.size();
        int left = 0, right = n - 1;
        while (left < right) {
           if (sgood[left] != sgood[right]) {
                return false;
            }
            ++left;
            --right;
        }
        return true;
    }
};

1 最多删除一个

class Solution {
public:
    bool checkPalindrome(const string& s, int low, int high) {
        for (int i = low, j = high; i < j; ++i, --j) {
            if (s[i] != s[j]) {
                return false;
            }
        }
        return true;
    }

    bool validPalindrome(string s) {
        int low = 0, high = s.size() - 1;
        while (low < high) {
            char c1 = s[low], c2 = s[high];
            if (c1 == c2) {
                ++low;
                --high;
            } else {
                return checkPalindrome(s, low, high - 1) || checkPalindrome(s, low + 1, high);
            }
        }
        return true;
    }
};

2 缺失的第一个正整数

// author:rmokerone
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int firstMissingPositive(vector<int> &nums) {
        for (int i = 0; i < nums.size(); i++) {
            while (nums[i] != i + 1) {
                if (nums[i] <= 0 || nums[i] > nums.size() || nums[i] == nums[nums[i] - 1])
                    break;
                // 将nums[i] 放置到对应位置上[1,2,3...]
                int idx = nums[i] - 1;
                nums[i] = nums[idx];
                nums[idx] = idx + 1;
            }
        }
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] != (i + 1)) {
                return (i + 1);
            }
        }
        return (nums.size() + 1);
    }
};

3 最长回文子序列

class Solution {
public:
    int longestPalindromeSubseq(string s) {
        int n = s.size();
        // dp 数组全部初始化为 0
        vector<vector<int>> dp(n, vector<int>(n, 0));
        // base case
        for (int i = 0; i < n; i++) {
            // 只有一个字符,最长回文子串的长度为 1
            dp[i][i] = 1;
        }
        for (int i = n - 1; i >= 0; i--){
            for (int j = i + 1; j < n; j++){
                if (s[i] == s[j]){
                    dp[i][j] = dp[i + 1][j - 1] + 2;
                }
                else{
                    dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[0][n - 1];
    }
};

4 重排链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        ListNode* slow = head;
        ListNode* fast = head;
        while (fast->next != nullptr && fast->next->next != nullptr) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

    ListNode* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* curr = head;
        while (curr != nullptr) {
            ListNode* tmp = curr->next;
            curr->next = pre;
            pre = curr;
            curr = tmp;
        }
        return pre;
    }

    void mergeList(ListNode* l1, ListNode* l2) {
        ListNode* l1_tmp, *l2_tmp;

        while (l1 != nullptr && l2 != nullptr) {
            l1_tmp = l1->next;
            l2_tmp = l2->next;

            l1->next = l2;
            l1 = l1_tmp;

            l2->next = l1;
            l2 = l2_tmp;
        }
    }
    void reorderList(ListNode* head) {
        // 特判
        if (head == nullptr) {
            return;
        }

        // 求中点
        ListNode* mid = middleNode(head);
        ListNode* l1 = head;
        ListNode* l2 = mid->next;
        mid->next = nullptr;

        // 翻转右侧
        l2 = reverseList(l2);

        // 拼接
        mergeList(l1, l2);
    }
};

5 环形分割

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

long long minimum(vector<int>& a) {
    int left = 0;
    int sum = 0, sum1 = 0;
    for (int i : a) {
        sum += i;
    }
    int avg = sum / 2;
    long long ans = INT_MAX;
    for (int right = 0; right < a.size(); right++) {
        sum1 += a[right];
        ans = std::min(static_cast<long long>(abs(sum - sum1 - sum1)), ans);
        while (sum1 > avg) {
            sum1 -= a[left];
            left++;
            ans = std::min(static_cast<long long>(abs(sum - sum1 - sum1)), ans);
        }
    }
    return ans;
}

int main() {
    vector<vector<int>> testCases = {
        {3, 1, 4, 2, 2, 1},
        {5, 10, 6, 7, 3},
        {1, 2, 3, 4, 5},
    };

    for (int i = 0; i < testCases.size(); i++) {
        cout << "Test case " << (i + 1) << ": ";
        cout << minimum(testCases[i]) << endl;
    }

    return 0;
}