【2023/2/18~2/19 Leetcode】数组练习集锦

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;
    }
};

学习参考:
双指针技巧秒杀七道数组题目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值