LeetCode Online Judge 题目C# 练习 - Plus One

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

 1         public static List<int> PlusOne(List<int> digits)

 2         {

 3             int carry = 1;

 4             for (int i = digits.Count - 1; i >= 0; i--)

 5             {

 6                 digits[i] = digits[i] + carry;

 7                 carry = digits[i] / 10;

 8                 digits[i] %= 10;

 9             }

10 

11             if (carry > 0)

12             {

13                 digits.Insert(0, 1);

14             }

15 

16             return digits;

17         }

代码分析:

  0难度,记住最后一个carry就行了。

  加个笔记,vector.insert(vector.begin(), 1); C++ vector insert,要用iterator.

你可能感兴趣的:(LeetCode)