Leetcode Plus One Java edition problem solving report

Source: Internet
Author: User

https://oj.leetcode.com/problems/plus-one/

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;
The idea of solving problems, starting with the last digit of the array and adding 1, you need to consider 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.

public class Solution {public    int[] PlusOne (int[] digits) {        int flag = 1;int i = 0;for (i=digits.length-1;i>=0; i--) {Digits[i] = Digits[i] + flag;if (digits[i]>9) {            <span style= "white-space:pre" ></span>flag = 1;< C3/><span style= "White-space:pre" ></span>digits[i] = 0;         <span style= "White-space:pre" ></SPAN>} else {            <span style= "White-space:pre" ></span> return digits;         <span style= "White-space:pre" ></SPAN>}         }if (i==-1&&flag==1) {<span style= "White-space: Pre "></span>int[] newdigits = new Int[digits.length+1];newdigits[0] = 1;for (i=1;i<=digits.length;i++) { Newdigits[i] = digits[i-1];} return newdigits;} else {return digits;}}    }




Leetcode Plus One Java edition problem solving report

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.