1. 程式人生 > >LeetCode --- 66. Plus One

LeetCode --- 66. 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。

簡單的大數加法,遍歷陣列的每位,同時處理進位,如果最後還有進位,則在陣列最前面在插入1即可。

時間複雜度:O(n)

空間複雜度:O(1)

 1 class Solution
2 { 3 public: 4 vector<int> plusOne(vector<int> &digits) 5 { 6 int c = 1; // 令進位標識初始值為1 7 for(int i = digits.size() - 1; i >= 0; -- i) 8 { 9 // 不斷處理進位 10 int a = digits[i] + c; 11 digits[i] = a % 10; 12 c
= a / 10; 13 } 14 // 如果最後還有進位,則在陣列最前面在插入1 15 if(c == 1) 16 digits.insert(digits.begin(), 1); 17 18 return digits; 19 } 20 };

當然,由於這題是比較特殊的大數處理,只有從低位開始全是9,才會不斷產生進位,而且進位後數字為0。當且僅當說有數位全是9的時候,才會多進出1位,此時最高位是1,其他所有位全是0(由於多出1位,因此需要在最後新增1個0)。

 1 class Solution
2 { 3 public: 4 vector<int> plusOne(vector<int> &digits) 5 { 6 // 從後往前,碰到第一個不是9的就對其加1,然後返回即可 7 for(int i = digits.size() - 1; i >= 0; -- i) 8 { 9 if(digits[i] == 9) 10 digits[i] = 0; 11 else 12 { 13 ++ digits[i]; 14 return digits; 15 } 16 } 17 // 最高位改成1,最後再添加個0 18 digits[0] = 1; 19 digits.push_back(0); 20 return digits; 21 } 22 };

相關推薦

LeetCode 66. Plus One(加1)

class 數字 public store res rest self present [0 Given a non-negative integer represented as a non-empty array of digits, plus one to the i

[leetcode] 66. Plus One

num solution 水題 strong can leading ger ica ati Given a non-negative integer represented as a non-empty array of digits, plus one to th

[leetcode] 66. Plus One 解題報告

ati pan can turn color con length empty leet Given a non-negative integer represented as a non-empty array of digits, plus one to the int

leetcode 66. Plus One

insert most tin integer output assume AS bsp else Given a non-empty array of digits representing a non-negative integer, plus one to th

[leetcode][66] Plus One

digits plus one inpu self. represent sum pub The hat 66. Plus One Given a non-empty array of digits representing a non-negative integer,

[leetcode]66.Plus One

nts sin ref .cn 沒有 ica ret n-n out 題目 Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The

#Leetcode# 66. Plus One

https://leetcode.com/problems/plus-one/   Given a non-empty array of digits representing a non-negative integer, plus one to the inte

LeetCode#66: Plus One

class Solution { public int[] plusOne(int[] digits) { int n = digits.length - 1;

leetcode 66 Plus One(加一運算)

題目要求 給定一個非空的數字陣列,該陣列表示一個非負整數,要求對這個非負整數加上1。返回操作後的陣列。 示例 輸入:[1,2,3] 表示123, 輸出[1,2,4] (123+1=124) 解題思路 因為題目已經說明不考慮負數的情況,所以對於任意非負數進行加

LeetCode--66. Plus One

題目連結:https://leetcode.com/problems/plus-one/ 該問題有點類似於連結串列的加一:2. Add Two Numbers和二進位制表達的字串加1的問題:67. Add Binary,比較簡單,就是設定一個邏輯型變數的進位標誌,程式碼如下:

[leetcode]66. Plus One

MY DUMMY SOLUTION class Solution { public int[] plusOne(int[] digits) { int c=0; digits[digits.length-1]=digits[digits.lengt

Leetcode 66. Plus One-陣列表示的數值+1,返回新的陣列

Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the

LeetCode --- 66. 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 t

Leetcode 66. Plus One 加一! 解題報告

1 解題思想 這道題,就是做一個加法,加1的操作而已。。 要點: 臨時陣列需要開一個比輸入長1的,防止最高位進位。 低位加一,然後處理進位,如有進位,那麼高一位執行+1操作,如此往復迭代。 2 原題 Given a non-negative numb

LeetCode(Python版)——66. Plus One

Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most

LeetCode OJ 系列之66 Plus One --Python

Problem: Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the m

66. Plus One

push ber exce urn sign cto plus one sent dig Given a non-negative integer represented as a non-empty array of digits, plus one to the int

66. Plus One(python+cpp)

題目: Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the mo

66. Plus One - Easy

Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most si

LeetCode Day53 plus-one

class Solution { public: vector<int> plusOne(vector<int>& digits) { int carry=1; for(int i=digits.size()-1;i&g