(Leetcode) Plus one---plus a

Source: Internet
Author: User

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.

Subscribe to see which companies asked this question


Problem Solving Analysis:

First see the topic, did not understand the reason, here to explain how to understand test instructions.

Think of a list as a number, say [9, 9]

This is considered to be 99, then add one, or 100, which is list, [1, 0, 0]

The way to do this here is to start with the last one and then make a judgment on each array to see if it needs rounding.

A for if there is a carry, add one, no carry words plus 0.

#-*-Coding:utf-8-*-__author__ = ' Jiuzhang ' class solution (object):    def plusone (self, digits):        plus = 1        for I in xrange (len (digits)-1,-1,-1):            digits[i] + = plus            if digits[i] >=:                digits[i]-= ten                plus = 1
   else:                plus = 0                break        if i = = 0 and plus = = 1:            digits.insert (0, 1)        return digits




(Leetcode) Plus one---plus a

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.