首页 > 其他 > 详细

[leetcode]Plus One

时间:2014-10-28 17:47:40      阅读:119      评论:0      收藏:0      [点我收藏+]

问题描述:

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

原文:http://blog.csdn.net/chenlei0630/article/details/40543129

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!