0 0
Read Time:1 Minute, 6 Second

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0‘s.

Increment the large integer by one and return the resulting array of digits.

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].

C#

public class Solution {
    public int[] PlusOne(int[] digits) {
        int AddItem=1;
        List<int> inR1= new List<int>();
        List<string> inR= new List<string>();
        for( int z=digits.Length-1; z>=0;z--){
             int s= digits[z]+AddItem;
                if(z==digits.Length-1 && s<10){
                    digits[z]=s;
                    return digits;
                }
               
                if(z==0 && s>=10){
                    inR.Add(s.ToString()[1].ToString());
                    inR.Add(s.ToString()[0].ToString());
                    break;
                }else if(s>=10){
                    inR.Add(s.ToString()[1].ToString());
                    AddItem=1;
                }                
                else {
                    inR.Add(s.ToString());
                     AddItem=0;
                }
                
        }

        for(int i=inR.Count-1;i>=0;i--){
        inR1.Add(int.Parse(inR[i]));
        }
         return inR1.ToArray();

    }
}
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *