【LeetCode】 066. Plus One
2016-08-02 11:32
330 查看
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.
public class Solution {
public int[] plusOne(int[] digits) {
boolean flag = false;
digits[digits.length - 1]++;
for (int i = digits.length - 1; i >= 0; i--) {
digits[i] += flag ? 1 : 0;
if (digits[i] > 9) {
flag = true;
digits[i] -= 10;
} else {
flag = false;
break;
}
}
if (flag) {
int[] res = new int[digits.length + 1];
System.arraycopy(digits, 0, res, 1, digits.length);
res[0] = 1;
return res;
}
return digits;
}
}
The digits are stored such that the most significant digit is at the head of the list.
public class Solution {
public int[] plusOne(int[] digits) {
boolean flag = false;
digits[digits.length - 1]++;
for (int i = digits.length - 1; i >= 0; i--) {
digits[i] += flag ? 1 : 0;
if (digits[i] > 9) {
flag = true;
digits[i] -= 10;
} else {
flag = false;
break;
}
}
if (flag) {
int[] res = new int[digits.length + 1];
System.arraycopy(digits, 0, res, 1, digits.length);
res[0] = 1;
return res;
}
return digits;
}
}
相关文章推荐
- Android Storage Access Framework(存储访问框架)
- java网络编程(一)使用TCP协议完成客户端与服务端的数据传递
- 守护进程编程惯例
- Spark 概念学习系列之Spark生态(十四)
- Spark生态之SparkR
- MVC模型绑定
- c++实现二维动态数组
- PAT1087
- Android实现机制(三)——View事件分发机制
- 浅谈struts
- Spark生态之Spark BlinkDB
- Could not load file or assembly 'Microsoft.AnalysisServices.SharePoint.Integration'
- HAProxy 基本翻译
- 配置泛微OA支持修改AD密码
- zookeeper详解(三)-- paxos算法以及zookeeper中的实现
- AUI中的tapmode属性详解
- jQuery源码研究分析学习笔记-jQuery.buildFragment()(六)
- 用 Python 绘制音乐图谱
- 如何加载全国8000G离线谷歌卫星地图并进行标注
- IOS动画中的枚举UIViewAnimationOptions