LeetCode PlusOne (附带解题思路)

给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。

最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。

示例 2:

输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。

注意:不要忽略末尾位为9,和全是9的特殊数组。

思路:从后向前看,无非三种情况:

1.正常数组(末尾位小于9): 末尾 + 1 返回

2.末尾位为9,但是不全为9 :从后向前看,为9的位置置零(不是林志玲),最后一个不为9的位置 + 1 返回

3.全部为9: 判断位数,返回 位数 + 1 大小的一个数组,首位为1。

代码地址:https://github.com/maronghe/ODOP/blob/master/src/com/logan/leetcode/plusone/PlusOneTester.java

package com.logan.leetcode.plusone;

/**
 * 
 * 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。
 * 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。
 * 你可以假设除了整数 0 之外,这个整数不会以零开头。
 * 
 * 示例 1:
 * 	输入: [1,2,3]
 * 	输出: [1,2,4]
 * 	解释: 输入数组表示数字 123。
 * 示例 2:
 * 	输入: [4,3,2,1]
 * 	输出: [4,3,2,2]
 * 	解释: 输入数组表示数字 4321。
 * 
 * @author Logan
 *
 */
public class PlusOneTester {
	
	public static void main(String[] args) {
		
		int [] arr = {9,9,0,9};
		for(int num : plusOne(arr)) {
			System.out.println(num);
		}
	}

	/**
	 * plus one 
	 * @param digits
	 * @return arr
	 */
    public static int[] plusOne(int[] digits) {
    	int size = digits.length;
    	for (int i = size - 1; i >= 0; i --) {
    		//judge the number from the last one to the first  
			if(digits[i] < 9) {
				digits[i] ++ ;
				return digits;
			}
			digits[i] = 0;
		}
    	
    	// all equals 9 and return size + 1 's array
    	int[] arr = new int[size + 1];
    	arr[0] = 1;
    	return arr;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34516081/article/details/81264432