Skip to content

Commit

Permalink
66. Plus One #110
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedKhalil777 committed Jul 27, 2024
1 parent 2e0fdcf commit d70fe72
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Algorithms/LeetCode.Algorithms/LeetCode.Algorithms/P/Plus One.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Numerics;

namespace LeetCode.Algorithms;

public class Plus_One
{

public int[] PlusOne(int[] digits)
{
if (digits.Length == 0)
return new int[0];

List<int> rDigits = digits.Reverse().ToList();

var hand = 1;
for (var i = 0; i < rDigits.Count; i++)
{
rDigits[i] = hand + rDigits[i];
hand = 0;
if (rDigits[i] >= 10)
{
hand = rDigits[i] / 10;
rDigits[i] = rDigits[i] % 10;
}
else
break;
}
if (hand > 0)
rDigits.Add(hand);

int[] arr = new int[rDigits.Count];

var j = 0;
for (var i = rDigits.Count - 1; i >= 0; i--)
{
arr[j] = rDigits[i];
j++;
}

return arr;
}
}

0 comments on commit d70fe72

Please sign in to comment.