leetcode--Plus One
2016-02-20 12:20
309 查看
题目:难度(Easy)
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Tags:Array Math
Similar Problems:(M) Multiply Strings (E) Add Binary
分析:用数组存数的各个数位,数组的靠前的位置数位越大,如[1,2,3]表示123,加1编程[1,2,4]
代码实现:
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Tags:Array Math
Similar Problems:(M) Multiply Strings (E) Add Binary
分析:用数组存数的各个数位,数组的靠前的位置数位越大,如[1,2,3]表示123,加1编程[1,2,4]
代码实现:
class Solution(object): def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ digits.reverse() #进行加1操作 digits[0]+=1 #update digits数组,divmod函数返回(商,余数) for i in range(len(digits)-1): carry, digits[i] = divmod(digits[i], 10) digits[i+1] += carry if digits[len(digits)-1] > 9: digits[len(digits)-1] %= 10 digits.append(1) digits.reverse() return digits
相关文章推荐
- leetcode笔记--Excel Sheet Column Title
- leetcode笔记--Add Digits
- 服务器端学习(一) 如何把自己的电脑作为网站服务器
- linux防火墙开对指定ip段开放端口
- 浅谈Java中的序列化
- 数据结构(6)--栈的应用之中缀表达式求值
- 数据库_1: MySQL:基础
- jQuery
- animationWithKeyPath
- 进程与线程
- jQuery实现div拖拽效果实例分析
- 2015-2016年第一学期第一周协会活动 二分搜索
- hive启动报错:hive1.1.1和0.13的jline的差别
- Tizen开发常用网址_Tizen入门教程_泰泽
- android时差8小时
- Maven 的使用
- [置顶] 中星微USB摄像头驱动分析及视频捕获
- 《大话设计模式》读书笔记6 工厂方法模式
- 面试题目:有序数组建立二叉排序树
- C#学习——字符串