博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 66. Plus One
阅读量:4519 次
发布时间:2019-06-08

本文共 997 字,大约阅读时间需要 3 分钟。

原题链接在这里:

题目:

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

题解:

特殊情况是 全是9时需要新建一个数组。

Time Complexity: O(n). Space: O(n).

AC Java:

1 public class Solution { 2     public int[] plusOne(int[] digits) { 3         if(digits == null || digits.length == 0){ 4             return digits; 5         } 6         int carry = 1; 7         for(int i = digits.length-1; i>=0; i--){ 8             int cur = (digits[i]+carry)%10; 9             carry = (digits[i]+carry)/10;10             digits[i] = cur;11             12             if(carry == 0){13                 return digits;14             }15         }16         17         int [] res = new int[digits.length+1];18         res[0] = 1;19         return res;20     }21 }

跟上.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5020943.html

你可能感兴趣的文章
Lintcode: Fast Power
查看>>
Pocket Gem OA: Log Parser
查看>>
枚举也能直接转换为对应的数值输出
查看>>
angularjs1-7,供应商
查看>>
BitSet
查看>>
Spring常用注解,自动扫描装配Bean
查看>>
(转载)深入理解WeakHashmap
查看>>
JAVA中的数组
查看>>
爬虫—使用Requests
查看>>
scrollIntoView()窗口滚动
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
使用ansible远程管理集群
查看>>
读jQuery源码释疑笔记3
查看>>
手把手教你jmeter压测--适合入门
查看>>
Sequelize+MySQL存储emoji表情
查看>>
RabbitMQ学习之Publish/Subscribe(3)
查看>>
[SCOI2010]生成字符串
查看>>
JLOI2015 城池攻占
查看>>
在 Azure 虚拟机上快速搭建 MongoDB 集群
查看>>
跑步运动软件调研
查看>>