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 缺失的第一个正整数
#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;
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();
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
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 重排链表
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;
}