Lintcode Python Simple Class topic 407. Add a

Source: Internet
Author: User
Tags lintcode

Title Description:

Given a non-negative number, represents a numeric array, on the basis of that number +1, returns a new array.

The number is arranged by size, and the largest number is at the front of the list.

Have you ever encountered this problem in a real interview? Yes Sample Example

Given [1,2,3] a representation of 123, returns [1,2,4] .

Given [9,9,9] a representation of 999, returns [1,0,0,0] .

labelArray of Google

Topic Analysis:

The end [-1] plus 1, and then reverse the loop, determine whether the element is 10, and then processing;

Special handling, [0] element is 10, need to insert a 1 in the first place.

Source:

class Solution: # @param {int[]} digits a number represented as an array of Digi        TS # @return {int[]} The result def plusone (self, digits): # Write Your code here n = len (digits)                DIGITS[-1] + = 1 for i in Range ( -1,-n,-1): if digits[i] = = 10:digits[i] = 0 DIGITS[I-1] + = 1 if digits[0] = = 10:digits[0] = 0 Digits.insert (0,1) return Digi        TS def _plusone (self, digits): # Write Your code here Digits.reverse () n = len (digits) Digits[0] + = 1 for i in range (n): if digits[i] = = 10:digits[i] = 0 if i = = n-1: Digits.append (1) else:digits[i+1] + = 1 if digits[-1] = = 10:digits[-1] = 0 digits.append (1) digits.reverse () return digits 

Lintcode Python Simple Class topic 407. Add one

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.