您的位置:首页 > 编程语言 > C#

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

2012-10-10 04:47 369 查看
Given a number represented as an array of digits, plus one to the number.

public static List<int> PlusOne(List<int> digits)
{
int carry = 1;
for (int i = digits.Count - 1; i >= 0; i--)
{
digits[i] = digits[i] + carry;
carry = digits[i] / 10;
digits[i] %= 10;
}

if (carry > 0)
{
digits.Insert(0, 1);
}

return digits;
}


代码分析:

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

  加个笔记,vector.insert(vector.begin(), 1); C++ vector insert,要用iterator.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: