leetcode题目(1)

目录

1.回文数

2.盛最多水的容器

3.罗马数字转整数

4.最长公共前缀

5.三数之和

6.最接近的三数之和


1.回文数

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

cpp 复制代码
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/

cpp 复制代码
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/

cpp 复制代码
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/

cpp 复制代码
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/

cpp 复制代码
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/

cpp 复制代码
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;
    }
};
相关推荐
倒头就睡的小比特4 天前
算法竞赛C++常用的STL
c++·算法
weilx12344 天前
C++笔记-文件IO-<fcntl.h>
c++
Smileyqp沛沛4 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车4 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光4 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·4 天前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁4 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven4 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
程序猿编码4 天前
告别改源码适配模型:纯 C++ 可配置 LLM 推理引擎,全格式全结构兼容
c++·大模型·llm·推理引擎
此生决int4 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++