zoukankan      html  css  js  c++  java
  • 【Leetcode】Plus One

    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 d = digits.size();
     5         int sum = 0, carry = 1;
     6         for (int i = d - 1; i >= 0; --i) {
     7             if (carry == 0) {
     8                 break;
     9             } else {
    10                 sum = digits[i] + carry;
    11                 digits[i] = sum % 10;
    12                 carry = sum / 10;
    13             }
    14         }
    15         if (carry == 1) {
    16             digits.insert(digits.begin(), 1);
    17         }
    18         return digits;
    19     }
    20 };
    View Code

    模拟。

  • 相关阅读:
    第九章:switch语句
    第八章:if-else语句
    第七章:运算符及运用
    第六章:名命规范
    事务
    jdbc
    Object
    容器
    Java exception
    Java OO1
  • 原文地址:https://www.cnblogs.com/dengeven/p/3612480.html
Copyright © 2011-2022 走看看