-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0066-Plus-one.cs
More file actions
46 lines (39 loc) · 1.04 KB
/
0066-Plus-one.cs
File metadata and controls
46 lines (39 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0066.Plus_one
{
public class _0066_Plus_one
{
public int[] PlusOne(int[] digits)
{
List<int> list = new List<int>();
bool carry = false;
int len = digits.Length - 1;
if (digits[len] + 1 == 10)
{
list.Add(0);
carry = true;
}
else list.Add(digits[len] + 1);
for (int i = len - 1; i >= 0; i--)
{
if (carry)
{
if (digits[i] + 1 == 10)
list.Add(0);
else
{
list.Add(digits[i] + 1);
carry = false;
}
}
else list.Add(digits[i]);
}
if (carry) list.Add(1);
var res = list.ToArray();
Array.Reverse(res);
return res;
}
}
}