leetcode题目(3)

目录

1.加一

2.二进制求和

3.x的平方根

4.爬楼梯

5.颜色分类

6.二叉树的中序遍历


1.加一

https://leetcode.cn/problems/plus-one/

cpp 复制代码
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int n = digits.size();
        for(int i = n -1;i>=0;--i)
        {
            if(digits[i] != 9)
            {
                ++digits[i];
                for(int j = i + 1;j<n;++j)
                {
                    digits[j] = 0;
                }
                return digits;
            }
        }
        //数组全部为9
        vector<int> ans(n+1);
        ans[0] = 1;
        return ans;
    }
};

2.二进制求和

https://leetcode.cn/problems/add-binary/description/

cpp 复制代码
class Solution {
public:
    string addBinary(string a, string b) {
        int i = a.length() - 1;
        int j = b.length() - 1;
        int carry = 0;
        string builder;
        //循环相加两个字符串相同长度的低位数部分
        while (i >= 0 && j >= 0) {
            int sum = carry;
            std::cout<<"a---"<<a.at(i)-'0'<<std::endl;
            std::cout<<"b---"<<b.at(j)-'0'<<std::endl;
            sum += a.at(i--) - '0';
            sum += b.at(j--) - '0';
            carry = sum / 2; //是否进位
            builder+=to_string((sum % 2));
        }
        // 如果 a 还没遍历完成(a串比b串长),则继续遍历添加 a 的剩余部分
        while (i >= 0) {
            int sum = carry + a.at(i--) - '0';
            carry = sum / 2;
            builder+=to_string(sum % 2);
        }
        // 如果 b 还没遍历完成(b串比a串长),则继续遍历添加 b 的剩余部分
        while (j >= 0) {
            int sum = carry + b.at(j--) - '0';
            carry = sum / 2;
            builder+=to_string(sum % 2);
        }
        //如果 carry 不等于0 还有个进位数没加进去,需要补充
        if (carry == 1) {
            builder+=to_string(carry);
        }
        //反转字符串获得正常结果
        reverse(builder.begin(),builder.end());
        return builder;
    }
};

3.x的平方根

https://leetcode.cn/problems/sqrtx/

cpp 复制代码
class Solution {
public:
    int mySqrt(int x) {
        //使用库中函数
        int sum = sqrt(x);
        return sum;
    }
};

4.爬楼梯

https://leetcode.cn/problems/climbing-stairs/description/

cpp 复制代码
class Solution {
public:
    int climbStairs(int n) {
        //f(x)=f(x-1)+f(x-2);
        int p = 0, q = 0, r = 1;
        for (int i = 1; i <= n; ++i) 
        {
            p = q; 
            q = r; 
            r = p + q;
        }
        return r;
    }
};

5.颜色分类

https://leetcode.cn/problems/sort-colors/description/

cpp 复制代码
class Solution {
public:
    void sortColors(vector<int>& nums) {
        //sort(nums.begin(),nums.end());

        //p0交换0,p1交换1
        int p0 = 0,p1 = 0;
        for(int i = 0;i < nums.size();++i)
        {
            if(nums[i] == 1)
            {
                swap(nums[i],nums[p1]);
                ++p1;
            }
            else if(nums[i] == 0)
            {
                swap(nums[i],nums[p0]);
                if(p0 < p1)
                {
                    swap(nums[i],nums[p1]);
                }
                ++p0;
                ++p1;
            }
        }
    }
};

6.二叉树的中序遍历

https://leetcode.cn/problems/binary-tree-inorder-traversal/description/

cpp 复制代码
class Solution {
public:
    void inorder(TreeNode* root,vector<int>& res)
    {
        if(root == NULL)
            return;
        inorder(root->left,res);
        res.push_back(root->val);
        inorder(root->right,res);
    }

    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        inorder(root,res);
        return res;
    }
};
相关推荐
想变成树袋熊6 分钟前
【自用】NLP算法面经(6)
人工智能·算法·自然语言处理
cccc来财29 分钟前
Java实现大根堆与小根堆详解
数据结构·算法·leetcode
liulilittle1 小时前
C++/CLI与标准C++的语法差异(一)
开发语言·c++·.net·cli·clr·托管·原生
小狄同学呀1 小时前
VS插件报错,g++却完美编译?API调用错因分析
c++
程序员编程指南1 小时前
Qt 数据库连接池实现与管理
c语言·数据库·c++·qt·oracle
Coovally AI模型快速验证1 小时前
数据集分享 | 智慧农业实战数据集精选
人工智能·算法·目标检测·机器学习·计算机视觉·目标跟踪·无人机
小乖兽技术1 小时前
C#与C++交互开发系列(二十四):WinForms 应用中嵌入C++ 原生窗体
c++·c#·交互
墨尘游子1 小时前
目标导向的强化学习:问题定义与 HER 算法详解—强化学习(19)
人工智能·python·算法
张北北.2 小时前
【深入底层】C++开发简历4+4技能描述6
java·开发语言·c++
恣艺2 小时前
LeetCode 854:相似度为 K 的字符串
android·算法·leetcode