当前位置:网站首页>Add one to non negative integers in the array
Add one to non negative integers in the array
2022-07-06 02:50:00 【Xiao Qi,.】
Give an array of integers to represent nonnegative integers , Add one to the number , And each array element only stores a single number , Divide integer 0 outside , This integer will not be expressed in 0 start .
for example :
Input :digits=[ 1,1,9 ]
Output :[ 1,2,0 ]
Input :digits=[ 9,9 ]
Output :[ 1,0,0 ]
Ideas : There are three situations
1. Not at the end 9 ,,, Add one directly to the end , Returns an array of
2. At the end is 9 ,,, Zero at the end , Traversal forward , Find out if 9 The elements of , Add one operation , Returns an array of
3. All the elements are 9,,, Reallocate the space of the array ( Add a space of shaping variables to the original space ), The first element is 1, All other elements belong to 0
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* plusOne(int* digits, int digitsSize, int* returnSize){
if(digits[digitsSize-1]!=9){ // Not at the end 9
digits[digitsSize-1]++;
*returnSize=digitsSize;
return digits;
}
for(int i=digitsSize-1;i>=0;i--){ // At the end is 9
if(digits[i]!=9){ // Find out if 9 The characters of , Add one operation
*returnSize=digitsSize;
digits[i]++;
return digits;
}
digits[i]=0; // If the character is 9, All become 0
}
*returnSize=digitsSize+1;// The characters in the array are all 9, Array needs to add one int Space occupied by type variables
digits=(int *)realloc(digits,*returnSize*sizeof(int));// Reallocate space
digits[digitsSize]=0;//
digits[0]=1;
return digits;
}
边栏推荐
- Day 50 - install vsftpd on ceontos6.8
- [ruoyi] ztree custom icon (iconskin attribute)
- 一个复制也能玩出花来
- How to read excel, PDF and JSON files in R language?
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
- RobotFramework入门(二)appUI自动化之app启动
- Rust language -- iterators and closures
- MySQL winter vacation self-study 2022 11 (5)
- How to accurately identify master data?
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
猜你喜欢
What is the investment value of iFLYTEK, which does not make money?
Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
C language - Blue Bridge Cup - promised score
Introduction to robotframework (II) app startup of appui automation
High number_ Vector algebra_ Unit vector_ Angle between vector and coordinate axis
【若依(ruoyi)】启用迷你导航栏
Apt installation ZABBIX
2022.02.13
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
随机推荐
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
Sign SSL certificate as Ca
Day 50 - install vsftpd on ceontos6.8
Thinking on Architecture Design (under continuous updating)
Pat 1084 broken keyboard (20 points) string find
Zhang Lijun: penetrating uncertainty depends on four "invariants"
Shell script updates stored procedure to database
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
XSS challenges绕过防护策略进行 XSS 注入
PMP practice once a day | don't get lost in the exam -7.5
CobaltStrike-4.4-K8修改版安装使用教程
【若依(ruoyi)】启用迷你导航栏
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
主数据管理理论与实践
Fault analysis | analysis of an example of MySQL running out of host memory
MySQL advanced notes
Dachang image library
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
CSP date calculation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7