-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e0fdcf
commit d70fe72
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
Algorithms/LeetCode.Algorithms/LeetCode.Algorithms/P/Plus One.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |