溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++如何實(shí)現(xiàn)加一運(yùn)算

發(fā)布時(shí)間:2022-03-28 13:39:21 來(lái)源:億速云 閱讀:266 作者:iii 欄目:大數(shù)據(jù)

這篇“C++如何實(shí)現(xiàn)加一運(yùn)算”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“C++如何實(shí)現(xiàn)加一運(yùn)算”文章吧。

Plus One 加一運(yùn)算

Example 1:

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

Example 3:

Input: digits = [0]
Output: [1]

Constraints:

  • 1 <= digits.length <= 100

  • 0 <= digits[i] <= 9

將一個(gè)數(shù)字的每個(gè)位上的數(shù)字分別存到一個(gè)一維向量中,最高位在最開頭,我們需要給這個(gè)數(shù)字加一,即在末尾數(shù)字加一,如果末尾數(shù)字是9,那么則會(huì)有進(jìn)位問題,而如果前面位上的數(shù)字仍為9,則需要繼續(xù)向前進(jìn)位。具體算法如下:首先判斷最后一位是否為9,若不是,直接加一返回,若是,則該位賦0,再繼續(xù)查前一位,同樣的方法,知道查完第一位。如果第一位原本為9,加一后會(huì)產(chǎn)生新的一位,那么最后要做的是,查運(yùn)算完的第一位是否為0,如果是,則在最前頭加一個(gè)1。代碼如下:

C++ 解法一:

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] = 0;
            else {
                digits[i] += 1;
                return digits;
            }
        }
        if (digits.front() == 0) digits.insert(digits.begin(), 1);
        return digits;
    }
};

Java 解法一:

public class Solution {
    public int[] plusOne(int[] digits) {
        int n = digits.length;
        for (int i = digits.length - 1; i >= 0; --i) {
            if (digits[i] < 9) {
                ++digits[i];
                return digits;
            }
            digits[i] = 0;
        }
        int[] res = new int[n + 1];
        res[0] = 1;
        return res;
    }
}

我們也可以使用跟之前那道 Add Binary 類似的做法,將 carry 初始化為1,然后相當(dāng)于 digits 加了一個(gè)0,處理方法跟之前那道題一樣,參見代碼如下:

C++ 解法二 :

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        if (digits.empty()) return digits;
        int carry = 1, n = digits.size();
        for (int i = n - 1; i >= 0; --i) {
            if (carry == 0) return digits;
            int sum = digits[i] + carry;
            digits[i] = sum % 10;
            carry = sum / 10;
        }
        if (carry == 1) digits.insert(digits.begin(), 1);
        return digits;
    }
};

Java 解法二 :

public class Solution {
    public int[] plusOne(int[] digits) {
        if (digits.length == 0) return digits;
        int carry = 1, n = digits.length;
        for (int i = digits.length - 1; i >= 0; --i) {
            if (carry == 0) return digits;
            int sum = digits[i] + carry;
            digits[i] = sum % 10;
            carry = sum / 10;
        }
        int[] res = new int[n + 1];
        res[0] = 1;
        return carry == 0 ? digits : res;
    }
}

以上就是關(guān)于“C++如何實(shí)現(xiàn)加一運(yùn)算”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI