(leetcode)Plus One
2015-08-02 19:59
330 查看
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.
大数字加法,一开始以为只是简单地整数加法,后来才发现是大数字
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>& digit){ // int len = digit.size(); // long long res = 0; // reverse(digit.begin(),digit.end()); // for(int j = len-1;j >= 0;--j) // { // res = res*10+ digit[j]; // } // long long newres = res+1; // digit.clear(); // while(newres!=0) // { // int a = newres%10; // digit.push_back(a); // newres /= 10; // } // reverse(digit.begin(),digit.end()); // return digit; int carry = 0; //reverse(digit.begin(),digit.end()); vector<int> newvec; int len = digit.size(); for(int i=len-1;i>=0;--i) { int tmp; if(i == len-1) { tmp = digit[i] + 1; } else tmp = digit[i] + carry; carry = 0; if(tmp>=10) { carry = 1; tmp=tmp%10; newvec.push_back(tmp); } else { newvec.push_back(tmp); } if(i == 0 && carry == 1) { newvec.push_back(carry); } } reverse(newvec.begin(),newvec.end()); return newvec; } };
相关文章推荐
- laravel小抄
- HDOJ 1241 Oil Deposits【最大连通块 dfs】
- C语言-VS2010调试tips
- HDOJ 1241 Oil Deposits【最大连通块 dfs】
- 精灵进程的创建
- C#委托(Delegate)学习日记
- PCA + SVM 人脸识别
- hdu 5340 Three Palindromes
- 有向图的欧拉回路及欧拉道路
- java中的I/O流
- Nutch2.2.1 开发环境搭建
- javascript设计模式之Mediator(中介者)模式
- WRL 类库项目模板
- BFS POJ 3126 Prime Path
- SSH学习九 依赖注入及加载Spring配置文件的方法
- 20150802-泛型
- 【转】CAD2012打开自动关闭解决方法
- How to write a good tech blog
- C语言-递归算法以及经典递归(Hanoi)
- 字符串拷贝操作