Leetcode [66] (Java): Plus One

Source: Internet
Author: User

title : Array plus one

Difficulty : Easy

topic content :

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits is stored such, the most significant digit was at the head of the list, and each element in the array conta In a single digit.

Assume the integer does not contain any leading zero, except the number 0 itself.

translation :

Given a non-empty array of numbers, represents a nonnegative integer, plus 1 to an integer.

These numbers are stored so that the most important numbers are in the head of the list, and each element in the array contains a number.

You can assume that this integer does not contain any leading zeros, except that the number 0 itself

Example 1:

Input: [1,2,3]output: [1,2,4]explanation:the array represents the integer 123.

Example 2:

Input: [4,3,2,1]output: [4,3,2,2]explanation:the array represents the integer 4321.

My idea : The first thought is that, with StringBuffer, they are taken out one by one, then the int is added, then the int array is returned, and then there are several test cases that are very long .... Cannot convert string to int or long.

So you can only consider traversal, start at the end of the loop, if the current value plus a carry is greater than 9, then the current value is 0, and continue the rounding, or return directly.

And then finally exit the loop when also to determine whether the last one is also a carry, if it is, the first more than the original "1", so must be re-new a int[].

My Code :

1      Public int[] PlusOne (int[] digits) {2         inti = Digits.length-1;3          while(I >-1) {4             if(Digits[i] + 1 > 9) {5Digits[i] = 0;6i--;7}Else {8digits[i]++;9                 returndigits;Ten             } One         } A         int[] ans =New int[Digits.length+1]; -Ans[0] = 1; -          for(intj = 0; J < Digits.length; J + +) { theANS[J+1] =Digits[j]; -         } -         returnans; -}

my complexity : O (n) spatial complexity is also O (n)

problems in the programming process :

No

Answer code :

1  Public int[] PlusOne (int[] digits) {2         3     intn =digits.length;4      for(inti=n-1; i>=0; i--) {5         if(Digits[i] < 9) {6digits[i]++;7             returndigits;8         }9         TenDigits[i] = 0; One     } A      -     int[] Newnumber =New int[N+1]; -Newnumber[0] = 1; the      -     returnNewnumber;

complexity of the answer: O (n) spatial complexity is also O (m*n) Same as mine

The answer : And mine is the same, but also from the end of the loop, but the last time to exit the loop because it is +1, at this time if there is no return, indicating that the digits is all 9999 ..., so the direct return of the first is 1, the other is the array of 0 is good, It is not necessary to assign the value behind the digits (definitely 0) to it.

extension : 67. Add binary (binary addition)

Enter two string, which represents two binary numbers, and returns a string representing both of them.

Idea: As with the subject, calculate from the far right, and calculate the carry symbol at the same time.

The main idea is to use two pointers and a carry variable carry, two any one greater than or equal to 0 to continue to add , while using StringBuffer () a one put in, and finally reversed.

Code:

1      Publicstring Addbinary (String A, string b) {2StringBuilder SB =NewStringBuilder ();3         inti = A.length ()-1, J = b.length ()-1, carry = 0;4          while(I >= 0 | | J >= 0) {5             intsum =carry;6             if(J >= 0) sum + = B.charat (j--)-' 0 ';7             if(i >= 0) sum + = A.charat (i--)-' 0 ';8Sb.append (sum% 2);9carry = SUM/2;Ten         }  One         if(Carry! = 0) Sb.append (carry); A         returnsb.reverse (). toString (); -}

Note that you need to convert char-' 0 ' to an array, and don't forget I--and J--。

Leetcode [66] (Java): Plus One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.