关闭

66. Plus One

104人阅读 评论(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.math.BigInteger;

public class Solution {
    public int[] plusOne(int[] digits) {
        
      		String str = "";
		
		String[] strs = new String[digits.length];
		
		for(int i=0;i<digits.length;i++){
			
			strs[i] = String.valueOf(digits[i]);
			
		}
		
		for(int i=0;i<digits.length;i++){
			
			str = str + strs[i];
			
		}
		
		BigInteger bigNumber = new BigInteger("1");
		
		BigInteger bigNumber2 = new BigInteger(str);
		
		BigInteger bigNumber3 = bigNumber.add(bigNumber2);
		
		//System.out.println(str.length());
		
		//int number2 = number + 1;
		
		String str2 = String.valueOf(bigNumber3);
		
		int[] digits2 = new int[str2.length()];
		
		for(int i=0;i<str2.length();i++){
			
			digits2[i] = Integer.parseInt(str2.substring(i, i+1));
			
		}
		
		return digits2;
	}
        
        
        
    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:78548次
    • 积分:1546
    • 等级:
    • 排名:千里之外
    • 原创:79篇
    • 转载:9篇
    • 译文:0篇
    • 评论:17条
    最新评论