leetcode-66 Plus One (value in array of +1 rounding operations, array expansion)

Source: Internet
Author: User

Plus one accepted:48227 total submissions:157869 My submissions Question Solution

Given a non-negative number represented as an array of digits, plus one to the number.

The digits is stored such, the most significant digit was at the head of the list.

Hide Tags Array Math

Test instructions: An integer is stored in an int array in the order of the highest bit in array[0], the lowest bit in [n-1], for example: 98, stored as: array[0]=9; Array[1]=8, then perform +1 operations, i.e. 98+1, to operate on the array

Problem Solving Ideas: This is a relatively simple topic, an array to add one operation. But don't underestimate the problem, which is called "Google's favorite topic" because it appears very often in Google interviews. starting with the last digit of the array and adding 1, you need to consider the rounding, if there is still a carry after the [0] bit, you need to open an array of length (N.length + 1) and copy the original array.

The code is as follows:

public static int[] PlusOne (int []digits) {//Set carry ID, initial value 1, meaning array last +1 operation int flag=1;int i=0;if (digits==null| | digits.length==0) {return digits;} for (i = digits.length-1; I >=0; i--) {digits[i]=digits[i]+flag;//If the value after +1 is greater than 9, the value on that bit is set to 0 and the carry indicator is placed 1if (digits[i]>9) { flag=1;digits[i]=0;} Else{return digits;}} After iterating through the array, if the Carry ID is 1, you need to expand the array size and set the first upper//value of the new array to 1, and the rest of the upper values using traversal to copy the IF (i==-1&&flag==1) {int []newdigits=new Int[digits.length+1];newdigits[0]=1;for (i = 1; i < digits.length; i++) {newdigits[i]=digits[i];} return newdigits;} Else{return digits;}}

I think the reason why Google likes is that the follow-up can ask some of the more basic extension issues, such as the ability to extend this to two arrays, or ask some oo design, suppose now to design a BigInteger class,
So what constructors are needed, and what data structures are used, and what are the advantages and disadvantages of using arrays and lists?

Leetcode-66 Plus One (value in array of +1 rounding operations, array expansion)

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.