当前位置:网站首页>LeetCode每日一题——加一
LeetCode每日一题——加一
2022-06-11 14:08:00 【java小豪】
66、加一
【题目描述】
给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
【示例输入】
示例 1:
输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。
示例 2:
输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。
示例 3:
输入:digits = [0]
输出:[1]
【思路】
当我们对数组 digits 加一时,我们只需要关注digits 的末尾出现了多少个 99 即可。我们可以考虑如下的三种情况:
如果digits 的末尾没有 9,例如[1,2,3],那么我们直接将末尾的数加一,得到 [1,2,4] 并返回;
如果digits 的末尾有若干个 9,例如 [1,2,3,9,9],那么我们只需要找出从末尾开始的第一个不为 9 的元素,即 3,将该元素加一,得到[1,2,4,9,9]。随后将末尾的 9 全部置零,得到[1,2,4,0,0] 并返回。
如果 digits 的所有元素都是 99,例如 [9,9,9,9,9],那么答案为 [1,0,0,0,0,0]。我们只需要构造一个长度比 digits 多 1 的新数组,将首元素置为 1,其余元素置为 0 即可。
【解法一】:
class Solution {
public int[] plusOne(int[] digits) {
int len = digits.length;
for (int i = len - 1; i >= 0; i--) {
if (digits[i] != 9) {
++digits[i];
for (int j = i + 1; j < len; j++) {
digits[j] = 0;
}
return digits;
}
}
int[] ans = new int[len + 1];
ans[0] = 1;
return ans;
}
}
【解法二】
class Solution {
public int[] plusOne(int[] digits) {
int len = digits.length;
for (int i = len - 1; i >= 0; i--) {
if (digits[i] == 9) {
digits[i] = 0;
} else {
digits[i] += 1;
return digits;
}
}
int[] ans = new int[len + 1];
ans[0] = 1;
return ans;
}
}
边栏推荐
- Variable parameter expression
- [public class preview]: mxplayer Ott audio and video transcoding practice and optimization
- 小米9线刷ROM
- YOLOv3学习笔记:YOLOv3的模型结构
- Tp6 whoops based exception takeover (beautiful interface)
- d的each与map不一致
- I have something to say about final, finally and finalize
- Single table query of SQL data query
- Learning notes of yolov3: model structure of yolov3
- Distributed file system and enterprise application -- elk enterprise log analysis system
猜你喜欢

AGV机器人RFID传感器CK-G06A与西门子1200PLC应用手册

My struggle: my years in foreign enterprises (1)

Introduction to reverse learning - excellent assembly debugging tool OllyDbg

mysql高级语句

【Flink】Flink CancellationException null DefaultExecutionGraphCache LeaderRetrievalHandler

Live800: several ways for intelligent customer service to improve customer experience

强大的全文本搜索工具——AnyTXT Searcher

Variable parameter expression

2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案

【公开课预告】:MXPlayer OTT音视频转码实践和优化
随机推荐
gensim.models word2vec 参数
In depth research and analysis report on global and Chinese plant extract products market
In depth research and analysis report on global and Chinese content audit market
Explanation of waitgroup usage in go language learning
Distributed file system and enterprise application -- elk enterprise log analysis system
Extracting storage is the best memory method
SQL数据查询之单表查询
Recommended open source scheduling libraries worth learning
Redis uses 10 tips, please accept!
Is the brokerage account given by qiniu business school safe? Do you charge for opening an account
I have something to say about final, finally and finalize
In depth research and analysis report on global and Chinese high purity molybdenum market
In depth research and analysis report on ready to eat meat market for vacuum low temperature cooking in the world and China
Code comparison tool, I use these six
How to quickly compress the size of video?
2022-2028 near infrared (NIR) analyzer Market Status and future development trend in the world and China
SQL: how to use the data of purchase order and sales order to calculate commodity cost by moving weighted average method
Implementation of VGA protocol based on FPGA
Redis configuration and optimization of NoSQL
[Clickhouse column] user initialization of new library role