Plus One @ LeetCode

Source: Internet
Author: User

Import java. util. arrays;/*** Plus One ** Given a number represented as an array of digits, plus one to the number. */public class S66 {public static void main (String [] args) {int [] digits = {9, 9}; // int [] digits = {0 }; system. out. println (Arrays. toString (plusOne (digits);} public static int [] plusOne (int [] digits) {int I = digits. length-1; int overflow = 0; // used to indicate whether overflow exists. // Add while (I> = 0) from the end to the end) {if (digits [I] + 1> 9) {// digits [I] = 0; overflow = 1; I --;} else {// when the number is less than 10, digits [I] = digits [I] + 1; return digits ;}// in this case, the current number of digits is insufficient, you must open a new array, // process the first if (overflow> 0) {int [] newDigits = new int [digits. length + 1]; System. arraycopy (digits, 0, newDigits, 1, digits. length); newDigits [0] = 1; newDigits [1] = 0; return newDigits;} return digits ;}}

 

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.