66. 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.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;
	}
        
        
        
    }


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lihaitao000/article/details/51558146
个人分类: LeetCode
上一篇38. Count and Say
下一篇67. Add Binary
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭