[Leetcode]66.plus One

Source: Internet
Author: User

Topic

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.

Example 1:

Input: [[+]
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.

A thought of solution

The idea is simple, if the last element of the array is less than 9, the last element is added directly to 1, and then the modified array is returned. If the last element of the array equals 9, it is judged whether the element before it is equal to 9, if it is equal, then continue to find, until the top 9 position pos, at this time is divided into two cases, if the POS is not 0, then the length of the array does not need to change, directly pos-1 the position of the element plus a can, If POS is 0, create a new array of length plus 1, and set the first element to 1 (the int array default initial value is 0).

Code
class Solution {    public int[] plusOne(int[] digits) {        int pos = digits.length - 1;        int length = pos;        int[] des = new int[length+2];        if(digits[pos] < 9) {            ++digits[pos];            return digits;        } else {            digits[pos] = 0;            while(pos-1 >= 0 && digits[pos-1] == 9 && pos-1 >= 0) {                digits[pos-1] = 0;                pos--;            }            if(pos > 0){                digits[pos-1]++;                return digits;            }            else{                des[0] = 1;                return des;            }                          }    }}

The above code is self-written, not elegant enough, the following code ideas are the same, but more elegant

class Solution {    public int[] plusOne(int[] digits) {       for(int i = digits.length-1; i >= 0; i--) {           if(digits[i] < 9) {               digits[i]++;               return digits;           }           digits[i] = 0;       }        int[] res = new int[digits.length+1];        res[0] = 1;        return res;    }}
Two ways to solve the solution

Carry may be used to record no carry. This kind of thinking and [leetcode]67.add binary this problem thinking very similar.

Code
class Solution {    public int[] plusOne(int[] digits) {        if (digits.length == 0) return digits;        int carry = 1, n = digits.length;        for (int i = digits.length - 1; i >= 0; --i) {            int sum = digits[i] + carry;            digits[i] = sum % 10;            carry = sum / 10;            if (carry == 0) return digits;        }        int[] res = new int[n + 1];        res[0] = 1;        return res;    }}

[Leetcode]66.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.