当前位置:网站首页>leetcode:66. add one-tenth
leetcode:66. add one-tenth
2022-06-11 18:54:00 【uncle_ ll】
66. Add one
source : Power button (LeetCode)
link : https://leetcode.cn/problems/plus-one/
Given a by Integers Composed of Non empty The nonnegative integer represented by an array , Add one to the number .
The highest digit is placed at the top of the array , Each element in the array stores only a single number .
You can assume that except for integers 0 outside , This integer will not start with zero .
Example 1:
Input :digits = [1,2,3]
Output :[1,2,4]
explain : Input array represents number 123.
Example 2:
Input :digits = [4,3,2,1]
Output :[4,3,2,2]
explain : Input array represents number 4321.
Example 3:
Input :digits = [0]
Output :[1]
Tips :
1 <= digits.length <= 100
0 <= digits[i] <= 9
solution
- The new array holds the calculated elements : Use the new storage space to store the transformed elements of the original array , Notice if the header has a carry ;
- From the back to the front, find the first not for 9 The elements of : Find the first one not for 9 The elements of , Add to this bit 1, And set the following elements to 0 that will do ; If it's all 9, Then create a new array , The head is 1, The back is full of 0;
Code implementation
New array assignment
python Realization
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
n = len(digits)
addFlag = 0 # Carry mark
res = []
count = 0 # First time of calculation , At the end of +1, The rest is only added to the carry
while digits:
digit = digits.pop() # Look for elements from the end
if count == 0:
newDigit = digit + addFlag + 1
count += 1
else:
newDigit = digit + addFlag
if newDigit >= 10:
newDigit = newDigit % 10
addFlag = 1
else:
addFlag = 0
res.insert(0, newDigit) # Insert a new element from the header
if addFlag == 1:
res.insert(0, addFlag)
return res
c++ Realization
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int n = digits.size();
vector<int> res;
int addFlag = 0;
int currentVal = 0;
for (int i=n-1; i>=0; i--) {
if (i == n-1) {
currentVal = digits[i] + 1 + addFlag;
}
else {
currentVal = digits[i] + addFlag;
}
if (currentVal >= 10) {
currentVal = currentVal % 10;
addFlag = 1;
}
else
addFlag = 0;
res.insert(res.begin(), currentVal);
}
if (addFlag == 1)
res.insert(res.begin(), addFlag);
return res;
}
};
Complexity analysis
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( n ) O(n) O(n)
Look for the first non from the back 9 Number of numbers
python Realization
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
n = len(digits)
for i in range(n-1, -1, -1):
if digits[i] != 9:
digits[i] += 1
for j in range(i+1, n):
digits[j] = 0
return digits
return [1] + [0] * n
c++ Realization
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int n = digits.size();
for (int i=n-1; i>=0; i--) {
if (digits[i] != 9) {
digits[i]++;
for (int j=i+1; j<n; j++)
digits[j] = 0;
return digits;
}
}
vector<int> res(n+1);
res[0] = 1;
return res;
}
};
Complexity analysis
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( 1 ) O(1) O(1) , The new return array is not considered , All for 9 This is still rare
边栏推荐
- Make a static tank
- Combination sum of 39 questions
- cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】
- Realize that you can continue to play
- 非递归实现二叉树的前、中、后序遍历
- Why is ti's GPMC parallel port more often used to connect FPGA and ADC? I give three reasons
- E-commerce (njupt)
- Visual slam lecture notes-10-2
- WWDC22 开发者需要关注的重点内容
- 让我们的坦克欢快的动起来吧
猜你喜欢

Uni app Muke hot search project (I) production of tabbar
![Cf:f. shifting string [string rearrangement in specified order + grouping into rings (cutting connected components) + minimum same moving cycle of each group + minimum common multiple]](/img/54/028f186883e54bcf0d741cf31b94fd.png)
Cf:f. shifting string [string rearrangement in specified order + grouping into rings (cutting connected components) + minimum same moving cycle of each group + minimum common multiple]

Financial bank_ Introduction to collection system

【图像去噪】基于绝对差分中值滤波、加权中值滤波法、改进加权中值滤波实现脉冲噪声图像去噪附matlab代码

全志科技T3开发板(4核ARM Cortex-A7)——MQTT通信协议案例

Today's sleep quality record is 60 points

力扣刷题——二叉树的层序遍历Ⅱ

Balanced search binary tree -- AVL tree

Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots

为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由
随机推荐
非递归实现二叉树的前、中、后序遍历
MySQL in-depth and complete learning - stage 1 - overview of learning
SAP UI5 里 XML 视图根节点运行时实例化的分析
2022成年礼,致每一位高考学子
全志T3开发板(4核ARM Cortex-A7)——系统启动阶段LOGO显示详解
电子商务(njupt)
Let our tanks move happily
Download the code and compile the environment
Specific methods for JS to realize full screen display
给你一个项目,你将如何开展性能测试工作?
Uni app Muke hot search project (I) production of tabbar
「案例分享」基于 AM57x+ Artix-7 FPGA开发板——PRU开发手册详解
让我们的坦克欢快的动起来吧
Quanzhi technology T3 development board (4-core arm cortex-a7) - video development case
uni-app 慕客热搜项目实战(一)tabBar的制作
In 2023, the MPAcc of School of management of Xi'an Jiaotong University approved the interview online in advance
KMP! You deserve it!!! Run directly!
Overall process of software development
leetcode:926. Flip the string to monotonically increasing [prefix and + analog analysis]
今天睡眠质量记录60分