Bootstrap

leetcode题目(1)

目录

1.回文数

2.盛最多水的容器

3.罗马数字转整数

4.最长公共前缀

5.三数之和

6.最接近的三数之和


1.回文数

https://leetcode.cn/problems/palindrome-number/description/

class Solution {
public:
    bool isPalindrome(int x) {
        //当x<0时肯定不为回文数,最后一位为0,若要回文只有0满足
        if(x < 0 || x % 10 == 0 && x != 0)
            return false;

        int ReverteNumber = 0;
        while(x > ReverteNumber)
        {
            ReverteNumber = ReverteNumber * 10 + x % 10;
            x /= 10;
        }

        return x == ReverteNumber || x == ReverteNumber / 10;
    }
};

2.盛最多水的容器

https://leetcode.cn/problems/container-with-most-water/description/

class Solution {
public:
    int maxArea(vector<int>& height) {
        //双指针,left左指针,right右指针
        int left = 0,right = height.size()-1;
        int ans=0;
        
        while(left<right)
        {
            //面积为左右指针值小的*指针距离
            int area = min(height[left],height[right])*(right-left);
            //更新ans的值,存储面积最大的
            ans=max(ans,area);
            //每次移动值较小的指针
            if(height[left]<=height[right])
                ++left;
            else
                --right;
        }
        return ans;
    }
};

3.罗马数字转整数

https://leetcode.cn/problems/roman-to-integer/

int romanToInt(char* s) {
    int symbolValues[26];
    //用ASII值相减,symbolValues[8]用来表示I,即数字1
    symbolValues['I' - 'A'] = 1;
    symbolValues['V' - 'A'] = 5;
    symbolValues['X' - 'A'] = 10;
    symbolValues['L' - 'A'] = 50;
    symbolValues['C' - 'A'] = 100;
    symbolValues['D' - 'A'] = 500;
    symbolValues['M' - 'A'] = 1000;

    int ans = 0;
    int n = strlen(s);
    for(int i = 0;i<n;++i)
    {
        int value = symbolValues[s[i]-'A'];
        if(i < n-1 && value < symbolValues[s[i+1] - 'A'])
        {
            ans -= value; 
        }
        else
        {
            ans += value;
        }
    }
    return ans;
}

4.最长公共前缀

https://leetcode.cn/problems/longest-common-prefix/

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.size() == 0)
            return "";
        int length = strs[0].size();
        int count = strs.size();
        for(int i = 0;i<length;++i)//遍历第一个字符串
        {
            char c = strs[0][i];
            for(int j = 1;j<count;++j)
            {
                if(i == strs[j].size() || strs[j][i] != c)
                {
                    //substr从某一位置提取一定字符
                    return strs[0].substr(0,i);
                }
            }
        }
        return strs[0];
    }
};

5.三数之和

https://leetcode.cn/problems/3sum/description/

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        // ranges::sort(nums);
        //数组排序
        sort(nums.begin(),nums.end());
        vector<vector<int>> ans;
        int n = nums.size();
        // 遍历数组,将每个元素作为可能的三元组中的第一个元素
        for (int i = 0; i < n - 2; i++) {
            int x = nums[i];// 当前元素
            if (i && x == nums[i - 1]) continue; // 跳过重复数字
            if (x + nums[i + 1] + nums[i + 2] > 0) break; // 当前元素与后两个之和大于0,结束循环
            if (x + nums[n - 2] + nums[n - 1] < 0) continue; // 当前元素与后两个之和小于0,跳过当前循环
            int j = i + 1, k = n - 1;// 双指针找剩余两个数,i的后一位和数组的最后一位
            while (j < k) {
                int s = x + nums[j] + nums[k];
                if (s > 0) {
                    k--;
                } else if (s < 0) {
                    j++;
                } else {
                    ans.push_back({x, nums[j], nums[k]});// 找到符合条件的加入列表
                    for (j++; j < k && nums[j] == nums[j - 1]; j++); // 跳过重复数字
                    for (k--; k > j && nums[k] == nums[k + 1]; k--); // 跳过重复数字
                }
            }
        }
        return ans;
    }
};

6.最接近的三数之和

https://leetcode.cn/problems/3sum-closest/description/

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());
        int ans,n = nums.size();
        int min_diff =  INT_MAX;// 最大正整数
        for(int i = 0; i < n - 2; i++){
            int x = nums[i];
            if(i > 0 && x == nums[i - 1]) continue;

            int s = x + nums[i + 1] + nums[i + 2];
            if(s > target){
                if(s - target < min_diff){
                    ans = s;
                }
                break;
            }

            s = x + nums[n - 2] + nums[n - 1];
            if(s < target){
                if(target - s < min_diff){
                    min_diff = target - s;
                    ans = s;
                }
                continue;
            }

            int j = i + 1, k = n - 1;
            while(j < k){
                s = x + nums[j] + nums[k];
                if(s == target){
                    return target;
                }
                if(s > target){
                    if(s - target < min_diff){
                        min_diff = s - target;
                        ans = s;
                    }
                    k--;
                }
                else{
                    if(target - s < min_diff){
                        min_diff = target - s;
                        ans = s;
                    }
                    j++;
                }
            }
        }
        return ans;
    }
};

;