题解 | #给数组加一#

给数组加一

https://www.nowcoder.com/practice/e20d6e18e75941b6a5b7b33ffa7b8d4d

class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型vector
     * @return int整型vector
     */
    vector<int> plusOne(vector<int>& nums) {
        int add = 1;
        vector<int> res;
        int n = nums.size();
        // 从低位开始累加
        for (int i = n - 1; i >= 0; --i) {
            int cur = nums[i] + add;
            int x = cur % 10;
            add = cur / 10;
            res.emplace_back(x);
        }
        if (add) res.emplace_back(add);
        reverse(res.begin(), res.end());
        return res;
    }
};

全部评论

相关推荐

05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
在喝茶的牛油很喜欢吃...:今天oc了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务