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;
    }
};
相关推荐
GISer_Jing2 分钟前
前端算法实战:大小堆原理与应用详解(React中优先队列实现|求前K个最大数/高频元素)
前端·算法·react.js
DBWYX1 小时前
c++项目 网络聊天服务器 实现;QPS测试
c++
小森77671 小时前
(三)机器学习---线性回归及其Python实现
人工智能·python·算法·机器学习·回归·线性回归
振鹏Dong1 小时前
超大规模数据场景(思路)——面试高频算法题目
算法·面试
uhakadotcom1 小时前
Python 与 ClickHouse Connect 集成:基础知识和实践
算法·面试·github
uhakadotcom1 小时前
Python 量化计算入门:基础库和实用案例
后端·算法·面试
uhakadotcom2 小时前
使用 Python 与 BigQuery 进行交互:基础知识与实践
算法·面试
uhakadotcom2 小时前
使用 Hadoop MapReduce 和 Bigtable 进行单词统计
算法·面试·github
XYY3692 小时前
前缀和 一维差分和二维差分 差分&差分矩阵
数据结构·c++·算法·前缀和·差分
longlong int3 小时前
【每日算法】Day 16-1:跳表(Skip List)——Redis有序集合的核心实现原理(C++手写实现)
数据库·c++·redis·算法·缓存