leetcode 66 Plus One
来源:互联网 发布:郑州师范学院网络教育 编辑:程序博客网 时间:2024/06/04 18:24
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.
#include<iostream>#include<cstdio>#include<vector>using namespace std;vector<int> plusOne(vector<int> &digits){ int len=digits.size(); vector<int> res(digits); int carrybit=1,i; for(i=len-1;i>=0;i--) { if(carrybit==0)//当没有进位时break break; else { res[i]+=carrybit; if(res[i]>=10) { carrybit=1; res[i]-=10; }else carrybit=0; } } if(i<0&&carrybit==1)//最高位有进位 res.insert(res.begin(),carrybit); return res;}int main(int argc,char *argv[]){ freopen("input.txt","r",stdin); int num; vector<int> v,res; cin>>num; while(num--) { int tmp; cin>>tmp; v.push_back(tmp);//注意这里每个数占一位,v[i]应该是个个位数 } res=plusOne(v);//对数组v表示的数加1 for(int i=0;i<res.size();i++) cout<<res[i]<<" "; cout<<endl; return 0;}
0 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
- 计算随机数边产生平方,平方根,自然对数的小程序
- liunx执行 *.sh 出现"目录或文件不存在"
- Princeton Algorithms: Part 1 [week 4: Binary Search Trees]
- jquery中的.on()
- Win32应用程序开发:完整的开发流程
- leetcode 66 Plus One
- ubuntu14.04安装之后输入法,鼠标闪烁,QQ,字体等问题解决方案
- java 日期加一天减一天
- 丑数
- 没时间写
- http://localhost:8080/不显示
- AVL树插入、删除的分析与实现
- jquery与google map api结合使用 控件,监听器
- XML数据结构解析