当前位置:网站首页>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;
}
边栏推荐
- 淘宝焦点图布局实战
- 2.11 simulation summary
- 会员积分营销系统操作的时候怎样提升消费者的积极性?
- Sign SSL certificate as Ca
- 1. Dynamic parameters of function: *args, **kwargs
- Self made CA certificate and SSL certificate using OpenSSL
- A doctor's 22 years in Huawei
- 2022.02.13
- Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
猜你喜欢
Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
Is there a completely independent localization database technology
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
Force buckle 146 LRU cache
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Pure QT version of Chinese chess: realize two-man, man-machine and network games
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
随机推荐
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
PMP practice once a day | don't get lost in the exam -7.5
#PAT#day10
深度解析链动2+1模式,颠覆传统卖货思维?
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
球面透镜与柱面透镜
Apt installation ZABBIX
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
Trends in DDoS Attacks
XSS challenges绕过防护策略进行 XSS 注入
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
Blue Bridge Cup group B provincial preliminaries first question 2013 (Gauss Diary)
Detailed use of dbutils # yyds dry goods inventory #
会员积分营销系统操作的时候怎样提升消费者的积极性?
PMP每日一练 | 考试不迷路-7.5
【若依(ruoyi)】ztree 自定义图标(iconSkin 属性)
[ruoyi] set theme style
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
张丽俊:穿透不确定性要靠四个“不变”