Java | Leetcode Java题解之第66题加一

题目:

题解:

java 复制代码
class Solution {
    public int[] plusOne(int[] digits) {
        int n = digits.length;
        for (int i = n - 1; i >= 0; --i) {
            if (digits[i] != 9) {
                ++digits[i];
                for (int j = i + 1; j < n; ++j) {
                    digits[j] = 0;
                }
                return digits;
            }
        }

        // digits 中所有的元素均为 9
        int[] ans = new int[n + 1];
        ans[0] = 1;
        return ans;
    }
}
相关推荐
七夜zippoe2 分钟前
Java性能调优工具篇:JMH基准测试与Profiler(JProfiler/Async-Profiler)使用指南
java·开发语言·jprofiler·jmh·async-profiler
從南走到北7 分钟前
JAVA国际版二手车交易二手车市场系统源码支持Android+IOS+H5+APP
android·java·ios
Kuo-Teng16 分钟前
LeetCode 19: Remove Nth Node From End of List
java·数据结构·算法·leetcode·链表·职场和发展·list
北i18 分钟前
TiDB 关联子查询去关联优化实战案例与原理深度解析
java·数据库·sql·tidb
Kuo-Teng19 分钟前
LeetCode 21: Merge Two Sorted Lists
java·算法·leetcode·链表·职场和发展
我命由我1234524 分钟前
Java 开发 - 粘包处理器 - 基于消息头 + 消息体(魔数验证、长度验证)
java·网络·后端·网络协议·java-ee·intellij-idea·intellij idea
2301_8003997230 分钟前
stm32 printf重定向到USART
java·stm32·算法
bagadesu34 分钟前
15.<Spring Boot 日志>
java·后端
laplace012344 分钟前
Maven
java·maven
wdfk_prog1 小时前
Xshell终端连接Ubuntu/Debian无颜色的解决方案
java·ubuntu·debian