[C语言] leetcode 66. Plus One
来源:互联网 发布:淘宝店铺设置流程 编辑:程序博客网 时间:2024/06/05 02:29
题目:
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
题目解释:
先解释一下题目意思:给定一个非负整数,然后用一个数组保存这个数字,例如数字100,则数组为[1,0,0];
然后给定数字加1之后再用一个数组保存,并返回这个数组。
答案代码:
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */int* plusOne(int* digits, int digitsSize, int* returnSize) { int* returnarray=(int*)malloc(digitsSize*sizeof(int)); int flag=1; //由于给定数字需要加1,所以flag初始化值设为1 for(int i=digitsSize-1;i>=0;i--) { returnarray[i]=(digits[i]+flag)%10; //数字最低位时加1(flag=1),其他情况根据前一位进位决定 flag=(digits[i]+flag)/10; } if(flag==0) { *returnSize=digitsSize; } else { *returnSize=digitsSize+1; (int*)realloc(returnarray,(digitsSize+1)*sizeof(int)); for(int i=digitsSize;i>0;i--) { returnarray[i]=returnarray[i-1]; } returnarray[0]=1; } return returnarray;}
0 0
- [C语言] leetcode 66. Plus One
- [leetcode]Plus One C语言
- [C语言][LeetCode][66]Plus One
- [LeetCode]66.Plus One
- LeetCode --- 66. Plus One
- [Leetcode] 66. Plus One
- [leetcode] 66.Plus One
- [leetcode] 66. Plus One
- Leetcode-66. Plus One
- 【leetcode】66. Plus One
- 66. Plus One LeetCode
- 【LeetCode】66. Plus One
- [LeetCode]66. Plus One
- 【LeetCode】66. Plus One
- LeetCode 66. Plus One
- LeetCode *** 66. Plus One
- leetcode 66. Plus One
- 【leetcode】66. Plus One
- Logstash使用详解
- Android系统Recovery工作原理之使用update.zip升级过程分析(二)---update.zip差分包问题的解决
- 数正方形
- input number 标签
- RequireJs的使用和快速理解
- [C语言] leetcode 66. Plus One
- 开发基于全志V3的LinuxOS
- ios 10 开发-让我们一次性解决导航栏的所有问题
- wireshark 还原语音包 RTP
- Ubuntu 开机自动启动roscore命令 自动打开终端执行脚本命令
- Ajax作用、及Ajax函数的编写
- 入门python--类与面向对象(3)
- DataTable 转换为 List ?
- linux 常用命令 su