当前位置:网站首页>LeetCode 进阶之路 - 加一
LeetCode 进阶之路 - 加一
2022-06-10 20:01:00 【Li_XiaoJin】
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
示例 1:
输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。
示例 2:
输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。
核心思想就是最后一位加1,然后处理一些临界值的情况,如9、99、999等 先对其进行加1,再拿加1后的值和10进行取余,不为0则表示该值不是9,无需进位,直接返回; 如果加1后得到值取余后得到0,则需要向前进位,如果每一位都需要进位,最后开辟一个新的数组,数组的长度比之前大1,将数组的第一个值设为1,因为数组不设置的话默认值是0;
public class PlusOne {
public static int[] plusOne(int[] digits) {
for (int i = digits.length-1; i >= 0 ; i--) {
digits[i]++;
digits[i] = digits[i] % 10;
if (digits[i] != 0) return digits;
}
digits = new int[digits.length +1];
digits[0] = 1;
return digits;
}
public static void main(String[] args) {
int[] num1 = new int[]{9,9,9,9};
int[] num2 = new int[]{0};
int[] num3 = new int[]{2,2};
System.out.println(plusOne(num3));
}
}
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/leetcode进阶之路-加一
边栏推荐
- 编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
- ^30H5 Web Worker多线程
- P5723 [deep base 4. example 13] prime number pocket
- Unity analyzes the rendering of built-in terrain and does some interesting things
- What is the difference between localhost and 127.0.0.1?
- LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
- Knowledge map / relationship visualization
- Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
- redis设置密码命令(临时密码)
- LeetCode:1037. Effective boomerang - simple
猜你喜欢

冷酸灵,一个国产品牌IPO的30年曲折史

app測試用例

Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?

pytorch深度学习——神经网络卷积层Conv2d

Test APK exception control netlocation attacker development

mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)

pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)

Canvas advanced functions (Part 1)

Fast Planner - detailed explanation of kinetic astar

自注意力(self-attention)和多头注意力(multi-head attention)
随机推荐
Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
冷酸灵,一个国产品牌IPO的30年曲折史
shell实现ssh登录并执行命令
1、 Vulkan develops theoretical fundamentals
pytorch深度学习——神经网络卷积层Conv2d
Niuke.com: numbers that appear more than half of the times in the array
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
Is Zhongyan futures a regular platform in China? Is it safe to open an account? Want to open a futures account
H5 van-popup全屏弹窗,模拟页面回退效果,支持左上角返回按钮,适用物理返回,侧滑与底部返回键
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
app测试用例
Full Permutation V3 recursion of brute force method /1~n
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
MySQL service startup failed
Self attention and multi head attention
MySQL Basics
Unity analyzes the rendering of built-in terrain and does some interesting things
Cut rope / integer split
Four methods to obtain the position index of the first n values of the maximum and minimum values in the list