"Leetcode with Python" Plus One__python

Source: Internet
Author: User
Blog Domain name: Http://www.xnerv.wang
Original title page: https://oj.leetcode.com/problems/plus-one/
Topic Type: Analog numerical operations
Difficulty Evaluation: ★
This article address: http://blog.csdn.net/nerv3x3/article/details/37336603

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such this most significant is in the head of the list.


An int array represents a numeric value, the leftmost is the highest, and now requires 1, and returns an array of int to represent the results of the calculation.
Very simple analog addition, note the carry problem, if it is 99,999 this, and finally add 1 after the array to the left to insert an element 1.
Space complexity O (1). Time complexity O (N).


class Solution: # @param digits, a list of integer digits # @return A list of integers digits
            def plusone (self, digits): len_s = len (digits) Carry = 1 for I in range (len_s-1,-1,-1): Total = digits[i] + carry digit = Int (total%) carry = Int (TOTAL/10) dig Its[i] = Digit if 1 = = Carry:digits.insert (0, 1) 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.