当前位置:网站首页>LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果
LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果
2022-07-25 23:57:00 【冰露可乐】
LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果
提示:本题是系列LeetCode的150道高频题,你未来遇到的互联网大厂的笔试和面试考题,基本都是从这上面改编而来的题目互联网大厂们在公司养了一大批ACM竞赛的大佬们,吃完饭就是设计考题,然后去考应聘人员,你要做的就是学基础树结构与算法,然后打通任督二脉,以应对波云诡谲的大厂笔试面试题!你要是不扎实学习数据结构与算法,好好动手手撕代码,锻炼解题能力,你可能会在笔试面试过程中,连题目都看不懂!比如华为,字节啥的,足够让你读不懂题
题目
给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/plus-one
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一、审题
示例 1:
输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。
示例 2:
输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。
示例 3:
输入:digits = [0]
输出:[1]
提示:
1 <= digits.length <= 100
0 <= digits[i] <= 9
二、解题
极其无聊的一个题
其实你大可不必把数组转int数字,直接在原来的数组上操作
我第一次见到这个题,还把数组变数字了
就是高位数字,不断乘10加低位数字
比如
这样会有问题的
很有可能int数字y会超过int表达的最大范围,溢出了
因此,你要把加法的本质搞懂了
就是低位一位一位地,加,发现有进位放到高位上去就行,我们直接在原来那个数组上操作就行
正常情况下,只有数字9才有必要+1进位得10
因此,你就从数组最低位开始加1,发现进位搞到上面去
其余的数字要是小于9,那压根不需要进位,直接加了1就返回数组本身
如果一直都是需要进位,最后数组还得多申请一个数字来,最高位是1,进位来的
这题绝对是闲得无聊
我们也不需要准备进位c
因为我们代码for循环就从数组的最低位,开始加1,即使你进位了,如果没有进位直接返回了
有进位,我们依然会到进位位置那去加1,这时候没有进位依然就直接返回了
手撕代码就过于容易了
//复习:直接原来数组上操作,从低位开始加1,如果没有进位直接返回,否则把进位1加到高位
public int[] plusOneReview(int[] digits) {
if (digits == null || digits.length == 0) return null;
if (digits.length == 1 && digits[0] == 0) return new int[] {
1};
int N = digits.length;
//直接从数组的低位N-1位置开始,到高位0位置
for (int i = N - 1; i >= 0; i--) {
if (digits[i] < 9){
//不够9,那低位加了1无所谓,没有进位,直接返回了
digits[i] += 1;
return digits;//没有后续
}
//如果数字是9,9+1=10,i位置就变0了,下一次一定要让前面高位加1
digits[i] = 0;
}
//如果不会提前返回,到这的话说明一直遇到了99999,还得进位,我们现在申请一个补上,其余位全是0
int[] ans = new int[N + 1];
ans[0] = 1;//最高位1,其余是0
return ans;
}
测试一把“:
public static void test(){
int[] arr = {
1,2,3,9};
Solution solution = new Solution();
int[] res = solution.plusOneReview(arr);
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] +" ");
}
}
public static void main(String[] args) {
test();
}
1 2 4 0
很OK
LeetCode测试;

反正不需要转数字
懂了进位往前加的道理就行
总结
提示:重要经验:
1)这是史上最简单的算法题了
3)笔试求AC,可以不考虑空间复杂度,但是面试既要考虑时间复杂度最优,也要考虑空间复杂度最优。
边栏推荐
- 行为型模式之责任链模式
- 指针函数的demo
- Leetcode169-多数元素详解
- 十大排序之快速排序
- Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms
- [learning notes] unreal 4 engine introduction (III)
- 抽丝剥茧C语言(高阶)程序环境和预处理
- Article 75: writing skills of academic papers
- The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
- Leetcode 0136. numbers that appear only once: XOR
猜你喜欢
![[learning notes] unreal 4 engine introduction (III)](/img/c2/2e6023c72652c77577f8f21684d07e.png)
[learning notes] unreal 4 engine introduction (III)

行为型模式之迭代器模式

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异

Bubble sort idea and Implementation

死信队列 和消息TTL过期代码

模式之固定与交替顺序执行
![[learning notes] solid works operation record](/img/f7/0535b473755643ce32996f00fa5554.png)
[learning notes] solid works operation record

Practical experience of pair programming
![[day.2] Joseph Ring problem, how to use arrays to replace circular linked lists (detailed explanation)](/img/2b/b354e52a9eb1d53475fa8d0339d33b.jpg)
[day.2] Joseph Ring problem, how to use arrays to replace circular linked lists (detailed explanation)

The GUI interface of yolov3 (simple, image detection)
随机推荐
Nacos offline service times error errcode: 500
C - readonly and const keywords
下一代终端安全管理的关键特征与应用趋势
What is multithreading
Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms
安全文档归档软件
牛客/洛谷——[NOIP2003 普及组]栈
意向不到的Dubug妙招
回溯——17. 电话号码的字母组合
Redis basic data type (string/list/set/hash/zset)
@Autowired注解的底层原理
Promise resolve callback hell, async await modifier
Unexpected dubug tricks
Part 74: overview of machine learning optimization methods and superparameter settings
ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
Program environment and pretreatment
LeetCode 沙胡同系列 -- 63. 不同路径 II
What is parity? How to use C language?
GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object