1.最长回文子串
题目来源:5.最长回文子串
题解:
class Solution {
public:
pair<int, int> expandAroundCenter(const string& s, int left, int right) {
while (left >= 0 && right < s.size() && s[left] == s[right]) {
--left;
++right;
}
return {left + 1, right - 1};
}
string longestPalindrome(string s) {
int start = 0, end = 0;
for (int i = 0; i < s.size(); ++i) {
auto [left1, right1] = expandAroundCenter(s, i, i);
auto [left2, right2] = expandAroundCenter(s, i, i + 1);
if (right1 - left1 > end - start) {
start = left1;
end = right1;
}
if (right2 - left2 > end - start) {
start = left2;
end = right2;
}
}
return s.substr(start, end - start + 1);
}
};
2.验证回文串
题目来源:
函数接口:
- isalnum():判断一个字符是否是字母或者(十进制)数字,若为字母或者数字,则返回True(非0值),否者返回False(0)
- tolower():把给定的字母转换为小写字母。
- 反向迭代器:sgood_rev(sgood.rbegin(), sgood.rend());
题解1:
class Solution {
public:
bool isPalindrome(string s) {
string sgood;
for (char ch: s) {
if (isalnum(ch)) {
sgood += tolower(ch);
}
}
string sgood_rev(sgood.rbegin(), sgood.rend());
return sgood == sgood_rev;
}
};
题解2:
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;
}
};
题解3:
class Solution {
public:
bool isPalindrome(string s) {
int n = s.size();
int left = 0, right = n - 1;
while (left < right) {
while (left < right && !isalnum(s[left])) {
++left;
}
while (left < right && !isalnum(s[right])) {
--right;
}
if (left < right) {
if (tolower(s[left]) != tolower(s[right])) {
return false;
}
++left;
--right;
}
}
return true;
}
};
3.删除有序数组中的重复项
题目来源:80. 删除有序数组中的重复项 II
题解:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int slow=0,fast=0,count=0;
while(fast<nums.size()){
if(nums[slow]!=nums[fast]){
slow++;
nums[slow]=nums[fast];
}
else if(slow<fast&&count<2){
slow++;
nums[slow]=nums[fast];
}
fast++;
count++;
if(fast<nums.size()&&nums[fast]!=nums[fast-1])
count=0;
}
return slow+1;
}
};
4.回文数
题目来源:https://leetcode.cn/problems/palindrome-number/submissions/
class Solution {
public:
bool isPalindrome(int x) {
if(x<0||(x % 10 == 0 && x != 0))
return false;
int revers=0;
while(x>revers){
revers=revers*10+x%10;
x/=10;
}
return x == revers || x == revers / 10;
}
};
5. 找到k个最接近元素
题目来源:https://leetcode.cn/problems/find-k-closest-elements/solution/zhao-dao-k-ge-zui-jie-jin-de-yuan-su-by-ekwtd/
思路:【二分法】
lower_bound【函数接口】
题解:
class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
int right = lower_bound(arr.begin(), arr.end(), x) - arr.begin();
int left = right - 1;
while (k--) {
if (left < 0) {
right++;
} else if (right >= arr.size()) {
left--;
} else if (x - arr[left] <= arr[right] - x) {
left--;
} else {
right++;
}
}
return vector<int>(arr.begin() + left + 1, arr.begin() + right);
}
};
自解:
class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
int left,right;
if(arr[0]>=x){
left=-1;
right=0;
}
if(arr[arr.size()-1]<x){
left=arr.size()-1;
right=left+1;
}
for(int i=0;i<arr.size()-1;i++){
if(arr[i]<x && arr[i+1]>=x){
left=i;
right=i+1;
//cout<<left<<"--"<<right<<endl;
break;
}
}
for(int i=0;i<k;i++){
if(left<0)
right++;
else if(right>=arr.size() || x-arr[left]<=arr[right]-x)
left--;
else
right++;
}
return vector<int>(arr.begin()+left+1,arr.begin()+right);
}
};
6. 接雨水
题目来源:42.接雨水
思路:
①对于下标 ii,下雨后水能到达的最大高度等于下标 ii 两边的最大高度的最小值,下标 ii 处能接的雨水量等于下标 ii 处的水能到达的最大高度减去 \textit{height}[i]height[i]。
②
class Solution {
public:
int trap(vector<int>& height) {
int n=height.size();
int sum=0;
int leftmax[n],rightmax[n];
leftmax[0]=height[0];
rightmax[n-1]=height[n-1];
for(int i=1;i<=n-1;i++){
leftmax[i]=max(leftmax[i-1],height[i]);
}
for(int i=n-2;i>=0;i--)
{
rightmax[i]=max(rightmax[i+1],height[i]);
}
for(int i=0;i<n;i++){
int tmp=min(leftmax[i],rightmax[i])-height[i];
if(tmp>0)
sum+=tmp;
}
return sum;
}
};
学习参考:
双指针技巧秒杀七道数组题目