# 26. 加一

java

```java
/**
 * 66. 加一.
 * 给定一个非负整数组成的非空数组，在该数的基础上加一，返回一个新的数组。
 *
 * 最高位数字存放在数组的首位， 数组中每个元素只存储一个数字。
 *
 * 你可以假设除了整数 0 之外，这个整数不会以零开头。
 *
 * 示例 1:
 * 输入: [1,2,3]
 * 输出: [1,2,4]
 * 解释: 输入数组表示数字 123。
 *
 * 示例 2:
 * 输入: [4,3,2,1]
 * 输出: [4,3,2,2]
 * 解释: 输入数组表示数字 4321。
 */
public class Solution66 {

    /**
     * 从后往前计算，如果该位小于9，直接加1，停止
     * 如果等于9，置0，继续向前计算.
     * 注意边界值，如果超出，需要扩容，高位补1
     *
     * @param digits
     * @return
     */
    public int[] plusOne(int[] digits) {
        int i=digits.length-1;
        for(;i>=0;i--){
            if(digits[i]<9){
                digits[i]+=1;
                break;
            }else {
                digits[i]=0;
            }
        }
        if(i<0){
            int[] r=new int[digits.length+1];
            r[0]=1;
            for(int m=1;m<r.length;m++){
                r[m]=digits[m-1];
            }
            return r;
        }
        return digits;
    }

    public static void main(String[] args){
        int[] digits={
                9,9,9,9
        };
        int[] r=new Solution66().plusOne(digits);
        for (int i=0;i<r.length;i++){
            System.out.print(r[i]+" ");
        }
    }
}
```

python

```
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/zhi-shang-lei/26-jia-yi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
