[LeetCode-66] Plus One
来源:互联网 发布:c语言strstr函数用法 编辑:程序博客网 时间:2024/06/04 23:25
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.
【分析】:题意:一个整数按位存储于一个int数组中,排列顺序为:最高位在array[0] ,最低位在[n-1],例如:98,存储为:array[0]=9; array[1]=8;
解题思路,从数组的最后一位开始加1,需要考虑进位,如果到[0]位之后仍然有进位存在,需要新开一个长度为(length + 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){if(!digits||!returnSize||digitsSize<=0) return NULL;/*至多比原先函数多一位*/int *returndigits = (int *)malloc(sizeof(int)*(digitsSize+1));if(!returndigits)return NULL;int i = 0;*returnSize = digitsSize;for(i = digitsSize-1;i>=0;i--) {if(digits[i] != 9) {digits[i] = digits[i] + 1;free(returndigits);return digits;} else {digits[i] = 0;}}/*全为9的情况*/for(i = 1;i < digitsSize;i++) {returndigits[i] = digits[i];}*returnSize = digitsSize + 1;returndigits[0] = 1;return returndigits;}
1 0
- leetcode 66 Plus One
- [leetcode 66] Plus One
- [LeetCode 66]Plus One
- leetcode ||66、 Plus One
- leetcode-66 plus one
- LeetCode 66 Plus One
- Plus One - LeetCode 66
- 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
- [LeetCode]66-Plus One
- Leetcode#66||Plus One
- [Leetcode]#66 Plus One
- linux开发遇到的一些问题
- Libev源码分析04:Libev中的相对时间定时器
- shadow map + vsm 总结
- TCP四次挥手过程
- vmware tools安装以及分辨率更改
- [LeetCode-66] Plus One
- mingw下vs编译的ffmpeg如何支持x264
- Ruby版本管理(RVM)
- asp.net上传文件使用fileupload控件,判断文件类型和大小,取得文件路径时报错【System.IO.FileNotFoundException:未能找到文件】的解决办法
- bzoj1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
- MySQL卸载
- 多线程同步方法<一>数据库悲观锁(for update)
- 分页查询
- QT5程序发布--打包可执行文件和动态链接库