leetcode 1,[LeetCode]Plus One

 2023-10-18 阅读 28 评论 0

摘要:題目描述:(鏈接) Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 解題思路: 上代碼 1 class Solution { 2 public: 3 vector<

題目描述:(鏈接)

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

解題思路:

上代碼

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int>& digits) {
 4         int carry = 1;
 5         for (int i = digits.size() - 1; i >= 0; --i) {
 6             int tmp = digits[i] + carry;
 7             digits[i] = tmp % 10;
 8             carry = tmp / 10;
 9         }
10         
11         if (carry) {
12             digits.insert(digits.begin(), carry);
13         }
14         
15         return digits;
16     }
17 };

?

轉載于:https://www.cnblogs.com/skycore/p/4872275.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/147281.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息