Plus One
Aug 5, 2017
时隔一年,重新写起博客,以前的东西没有存,随它去吧,重新开始。
去年年初接触到 LeetCode,这是一个Online Judge网站,很有趣,从这篇博客开始,争取把所有题目都做完。
0x00 题目
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
0x01 分析
根据字面意思简单的理解就是给一个正整数组成的数组,期望将数组中的数字看成一个整型数,对其进行加一操作。
eg:
[1] => [2]
[9] => [1, 0]
[1, 9, 9] => [2, 0, 0]
0x02 编码
- python 版本123456789101112class Solution(object):def plusOne(self, digits):if len(digits) == 0:return digitsif digits[0] == 9 and len(digits) == 1:return [1,0]if digits[-1] != 9:digits[-1] += 1return digitstmp = self.plusOne(digits[0:-1])tmp.append(0)return tmp
0x03 运行结果
语言 | 运行时间 |
---|---|
Python | 56ms |