LeetCode#66 Plus One
2015-07-23 21:13
232 查看
Problem Definition:
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.
Solution:
1) A very traditional thought:
2) And a faster one:
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.
Solution:
1) A very traditional thought:
def plusOne(digits): if digits==None or digits==[]: return digits index=len(digits)-1 c=1 r=[] while index>=0 or c==1: a=digits[index]+c if index>=0 else c i=a if a<10 else 0 c=a/10 r+=i, index-=1 return r[::-1]
2) And a faster one:
def plusOne(digits): if digits==None or digits==[]: return digits index=len(digits)-1 while index>=0: if digits[index]==9: digits[index]=0 index-=1 else: #可提前结束 digits[index]+=1 return digits #走到这,数组里将全是0 digits[0]=1 digits+=1, return digits
相关文章推荐
- 反射setAccessible()方法
- 字典
- 基于Linux webRTC 音语对讲之三 -- webrtc voip 实例展现
- cf559B字符串的最小表示法和递归的写法
- 解决java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
- hdoj 1106 排序
- 205 Isomorphic Strings
- undefined reference to `log4cplus::Logger::Logger(log4cplus::Logger&&)'
- iOS 监听文本框的改变 代码片段
- 7.23多校——5305DFS——Friends
- Parencodings
- SQL*loader实验
- C语言部分知识
- Android之——监听手机开机事件
- hdu 3635 Dragon Balls
- Hdu 2899 - Strange fuction 二分/三分求函数极值点
- 享元模式
- hdu5305
- CUDA快速傅里叶变换(cuFFT)阅读笔记(一)
- 03 JAVA 数组补充 拷贝、序列化