zoukankan      html  css  js  c++  java
  • [LeetCode66]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.
    一个整数按位存在一个数组中array[0]为最高位

    代码:

    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int size = digits.size();
            if (digits[size - 1] != 9)
            {
                digits[size - 1]++;
                return digits;
            }
            else
            {
                for (int i = size - 1; i >= 0; --i)
                {
                    if (++digits[i] > 9)
                    {
                        digits[i] = 0;
                    }
                    else
                        break;
                }
                if (digits[0] == 0)
                {
                    digits[0] = 1;
                    digits.push_back(0);
                }
            }
            return digits;
        }
    };
  • 相关阅读:
    ASP.NET初识4
    属性
    ASP.NET初识4
    ACCP6.0第九章练习
    ASP.NET初识1
    鼠标指针含义
    ASP.NET初识2
    第三部分
    ASP.NET初识3
    ASP.NET初识5
  • 原文地址:https://www.cnblogs.com/zhangbaochong/p/5143427.html
Copyright © 2011-2022 走看看