首页 > 代码库 > [leetcode]Plus One
[leetcode]Plus 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.
代码:
import java.util.Vector; //java public class PlusOne { public int[] plusOne(int[] digits) { Vector<Integer> vec = new Vector<Integer>(); int r = 1; int tmp = 0; int i = digits.length-1; while(i>=0 && r!=0){ tmp = r + digits[i--]; if(tmp >= 10){ vec.add(tmp%10); r = 1; } else{ vec.add(tmp); r = 0; } } if(r==1){ int[] result = new int[digits.length+1]; result[0] = 1; return result; } else{ int [] result = new int[digits.length]; int pos = 0; for(i=0; i<digits.length-vec.size();){ result[pos++] = digits[i++]; } for(i = vec.size()-1; i>=0; i--) result[pos++] = vec.get(i); return result; } } }
[leetcode]Plus One
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。