Skip to content

Commit d70fe72

Browse files
66. Plus One #110
1 parent 2e0fdcf commit d70fe72

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

  • Algorithms/LeetCode.Algorithms/LeetCode.Algorithms/P
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Numerics;
2+
3+
namespace LeetCode.Algorithms;
4+
5+
public class Plus_One
6+
{
7+
8+
public int[] PlusOne(int[] digits)
9+
{
10+
if (digits.Length == 0)
11+
return new int[0];
12+
13+
List<int> rDigits = digits.Reverse().ToList();
14+
15+
var hand = 1;
16+
for (var i = 0; i < rDigits.Count; i++)
17+
{
18+
rDigits[i] = hand + rDigits[i];
19+
hand = 0;
20+
if (rDigits[i] >= 10)
21+
{
22+
hand = rDigits[i] / 10;
23+
rDigits[i] = rDigits[i] % 10;
24+
}
25+
else
26+
break;
27+
}
28+
if (hand > 0)
29+
rDigits.Add(hand);
30+
31+
int[] arr = new int[rDigits.Count];
32+
33+
var j = 0;
34+
for (var i = rDigits.Count - 1; i >= 0; i--)
35+
{
36+
arr[j] = rDigits[i];
37+
j++;
38+
}
39+
40+
return arr;
41+
}
42+
}

0 commit comments

Comments
 (0)