LeetCode Plus One

Source: Internet
Author: User

LeetCode Plus One
LeetCode solving Plus One

Original question

Add a non-negative integer that consists of a list of numbers.

Note:

The number before the list indicates that the highest bit may also be carried.

Example:

Input: [1, 2, 3, 4, 9]

Output: [1, 2, 3, 5, 0]

Solutions

From the low position to the high position. If the last digit has a forward position, add this digit. Otherwise, exit the loop. If the highest bit is also carried, insert one before the list.

AC Source Code
Class Solution (object): def plusOne (self, digits): "": type digits: List [int]: rtype: list [int] "" carry = 1 for I in range (len (digits)-1,-1,-1 ): digits [I] + = carry if digits [I] <10: carry = 0 break else: digits [I]-= 10 if carry = 1: digits. insert (0, 1) return digitsif _ name _ = "_ main _": assert Solution (). plusOne ([1, 2, 3, 4, 9]) = [1, 2, 3, 5, 0] assert Solution (). plusOne ([9]) = [1, 0]

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.