当前位置:网站首页>数组 加一
数组 加一
2022-06-10 20:50:00 【Morris_】
数组 LC 加一
给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
func plusOne(_ digits: [Int]) -> [Int] {
var nums = digits
// 最后一位是否是9
if nums.last == 9 {
// 如果最后一位是9,需要遍历其他位
var index = nums.count - 1
// 从最后一位开始遍历,直至遍历到元素值不是9止
while index >= 0 && nums[index] == 9 {
// 如果元素上的值是9,则将其改为0
nums[index] = 0
index -= 1
}
// 进位
if index < 0 {
nums.insert(1, at: 0)
}
else {
nums[index] += 1
}
}
// 如果最后一位是不是9,则最后一位直接+1
else {
nums[nums.count - 1] += 1
}
return nums
}
边栏推荐
- 报错解决Error parsing Mapper XML
- As a programmer, is it really that important for the underlying principles?
- Standard dual airbags, starting from 48900 for butcher Chang'an Lumin
- Which city should I go to after I graduate from it? Which position has a high salary? Which companies have good treatment?
- In 2021, the average salary will be released, and the IT industry will not be surprised
- Shell implements SSH login and executes commands
- AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
- Notes to entry: do I need to know programming for O & M?
- Abbexa low sample size chicken lysozyme C (Lyz) ELISA Kit
- 【phpstorm】 No data sources are configured to run this SQL and provide advanced c
猜你喜欢

As a programmer, is it really that important for the underlying principles?

What should be paid attention to when designing Multilayer PCB?

【phpstorm】 No data sources are configured to run this SQL and provide advanced c

Abbexa cdan1 siRNA instruction manual

Intelligent robot making progress in imitation learning

JS mobile terminal copy text to clipboard code

2022-06-09 rk817 PMU battery temperature detection
![[nk] 牛客月赛51 F-平均题](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)
[nk] 牛客月赛51 F-平均题

Forward slash "/", backslash "\," escape character "\" and file path separator cannot be clearly remembered

【MySQL】常见数据类型总结
随机推荐
Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)
AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
Abbexa 8-OHdG CLIA kit solution
How to view the occupied space of a table in MySQL database
Will your company choose to develop data center?
01js basic null and undefined difference type conversion = = code block logical operator
Shaping teenagers' comprehension ability with children's programming thinking
ThinkPHP v6.0. X deserialization vulnerability recurrence
Cordova Plugin /JPush PhoneGap 极光推送_本地推送_消息推送
Principle of gravure overprint and factors affecting overprint
Interview Essentials - basic knowledge of synchronized underlying principles
Leetcode advanced road - 169 Most elements
Leetcode advanced path - delete duplicates in the sorting array
Before we learn about high-performance computing, let's take a look at its history
标配双安全气囊,价格屠夫长安Lumin 4.89万起售
Abbexa cdan1 siRNA instruction manual
Leetcode advanced road - 69 Square root of X
Has the samesite cookie problem occurred when using identityserver?
Factory and strategy mode implementation scheme of coupons
MySQL数据库如何查看表占用空间大小