LeetCode66 Plus One
来源:互联网 发布:如何卸载mac第三方软件 编辑:程序博客网 时间:2024/06/02 03:13
LeetCode66 Plus One
问题描述
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.
简单分析
在网上看到这道题是谷歌很喜欢的面试题,不是很理解。这道题就是对数组的每一个元素进行加1,跟之前的
LeetCode43 Multiply Strings
有十分相似的地方,就是进位。
其实很多想加题目主要考虑的就是进位的问题。
这里因为数组的长度是固定的,如果在最高位发生进位的话,也就是比之前多了一位,那么就需要重新声明一个新的数组。
代码如下
public int[] plusOne(int[] digits) { if(digits == null || digits.length==0){ return digits; } int help = 1; for (int i = digits.length-1; i >= 0; i--) { int digit = (digits[i] + help) % 10; help = (digits[i] + help) / 10; digits[i] = digit; if (help == 0) return digits; } int[] result = new int[digits.length + 1]; result[0] = 1; return result;}
LeetCode学习笔记持续更新
GitHub地址 https://github.com/yanqinghe/leetcode
CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678
阅读全文
0 0
- LeetCode66: Plus One
- leetcode66.[Array] Plus One
- leetcode66: Plus One
- leetcode66~Plus One
- LeetCode66. Plus One
- LeetCode66. Plus One
- LeetCode66 Plus One
- leetcode66-Plus One
- LeetCode66——Plus One
- LeetCode66——Plus One
- leetcode66-Plus One(加1问题)
- LeetCode66/169/79 Plus One/Majority Element /Word Search
- LeetCode66——Plus One(一个整数用数组存储,然后在末尾加1)
- 字符串可以按位获得和列表生成式-【leetcode66-plus one】
- Plus One
- Plus One
- Plus One
- Plus One
- AndroidStudio Libs文件夹
- C语言-语句
- 34.Oracle深度学习笔记——12C的AWR初步解读
- MapReduce表连接操作之Map端join
- 《数据结构》实验二(顺序表)
- LeetCode66 Plus One
- (M)Dynamic Programming:576. Out of Boundary Paths
- 开始
- 嵌入式Linux学习笔记(3)——linux系统常用工具
- bzoj 2788(差分约束+tarjan)
- java安全框架
- 10月8日 c语言 数组元素作函数实参 输入10个数,要求输出其中值最大的元素和该数是第几个数
- LaTeX使用经验
- 种树,什么时间最合适?