leetcode66-Plus One(加1问题)
2016-04-17 22:20
387 查看
问题描述:
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.
给定一个以一系列数字表示的非负数,将其加一并转换成数字。
数字存储的最高位在列的最前面。
问题求解:
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.
给定一个以一系列数字表示的非负数,将其加一并转换成数字。
数字存储的最高位在列的最前面。
问题求解:
class Solution { public: vector<int> plusOne(vector<int>& digits) { int n=digits.size(); digits[n-1] += 1;//(1)最低位+1 //int carry=0; for(int i=n-1;i>0;i--) { if(digits[i]>=10) {//(2)诸位比较,如果大于10,则需处理进位 digits[i-1] += digits[i]/10; digits[i] = digits[i]%10; } } //(3)判断最高位是否大于10 if(digits[0]>=10) {//若大于10,则开辟一个长度大1的数组存储最后得数 vector<int> d(n+1); d[0] = digits[0]/10; digits[0] %= 10; int k=1, j=0; while(j<n) { d[k++]=digits[j++]; } return d; } return digits; } };
相关文章推荐
- 红黑树rbtree原理及linux内核中红黑树实现详解
- 两种方法求解 正数数组中 两个数相减 的最大值
- 构建之法阅读笔记02
- C++ sizeof 使用规则及陷阱分析
- OpenGL备忘7---Camera
- jQuery的deferred对象详解
- Dubbo架构设计详解
- UFLDL 笔记 03 梯度检验与高级优化算法介绍
- Hadoop:Found a swap file by the name "/etc/.hosts.swp"
- masonry框架的使用
- MCV后端框架的实例(一)
- POI模板导出word
- 20145335郝昊《java程序设计》第7周学习总结
- 触碰jQuery:AJAX异步详解
- Servlet文件上传
- modelsim仿真MCB DDR3 IP核时的报错
- joomla对象注入漏洞分析
- PHP文件的上传与下载
- Linux(CentOS 6.5) 上安装redis
- a820救砖记录