66 Plus One
2015-08-18 09:11
295 查看
public class Solution {
public int[] plusOne(int[] digits) {
if(digits==null||digits.length==0) return null;
int len = digits.length;
int f = 1;
int p = len - 1;
StringBuffer str = new StringBuffer();
while(p>=0){
int tmp = digits[p] + f;
str.append(tmp%10);
f = (tmp - tmp%10)/10;
if(f==0){
--p;
while(p>=0){
str.append(digits[p]);
--p;
}
char[] tmpRes = str.toString().toCharArray();
int tr_len = tmpRes.length;
int[] res = new int[tr_len];
int index = 0;
for(int j=tr_len-1;j>=0;--j){
res[index++] = new Integer(tmpRes[j] - '0');
}
return res;
}
--p;
}
if(f!=0){
str.append(f);
}
char[] tmpRes = str.toString().toCharArray();
int tr_len = tmpRes.length;
int[] res = new int[tr_len];
int index = 0;
for(int j=tr_len-1;j>=0;--j){
res[index++] = new Integer(tmpRes[j] - '0');
}
return res;
}
}
public int[] plusOne(int[] digits) {
if(digits==null||digits.length==0) return null;
int len = digits.length;
int f = 1;
int p = len - 1;
StringBuffer str = new StringBuffer();
while(p>=0){
int tmp = digits[p] + f;
str.append(tmp%10);
f = (tmp - tmp%10)/10;
if(f==0){
--p;
while(p>=0){
str.append(digits[p]);
--p;
}
char[] tmpRes = str.toString().toCharArray();
int tr_len = tmpRes.length;
int[] res = new int[tr_len];
int index = 0;
for(int j=tr_len-1;j>=0;--j){
res[index++] = new Integer(tmpRes[j] - '0');
}
return res;
}
--p;
}
if(f!=0){
str.append(f);
}
char[] tmpRes = str.toString().toCharArray();
int tr_len = tmpRes.length;
int[] res = new int[tr_len];
int index = 0;
for(int j=tr_len-1;j>=0;--j){
res[index++] = new Integer(tmpRes[j] - '0');
}
return res;
}
}
相关文章推荐
- UAP如何根据DeviceFamily显示不同的页面
- PostgreSQL 系统表
- poj 2251 Dungeon Master
- OCS Inventory,我又来了
- hdu2647
- java.lang.Thread.State类详解
- iOSQuartz2D-04-手动剪裁图片并保存到相册
- 灰度世界算法(Gray World Algorithm)
- poj 1047 模拟(含大数乘法)循环数
- hdu 5294 Tricks Device (最短路+最大流)
- gson
- Windows 10 响应式设计和设备友好的开发
- HDOJ-2112-HDU Today(最短路)
- JAVA应用当中的数字格式化之科学计数法转数字
- hdoj3791二叉搜索树
- Add Binary
- Android性能优化-内存
- Unity游戏中使用贝塞尔曲线
- 在路演中,CEO长得越帅,那么股价越高?
- hdoj1869 六度分离(dijstra——floyd)