[LeetCode]66. Plus One
2016-04-16 23:00
666 查看
66. Plus One
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.
分析
一个非负整数的每位数保存在数组中,最高位在数组头部,现在需要算出这个数加1后的结果。这个从后往前遍历即可。源码
class Solution { public: vector<int> plusOne(vector<int>& digits) { int carry = 1; int sum = 0; for(int i = digits.size() - 1; i >= 0; i--){ sum = digits[i] + carry; carry = sum / 10; digits[i] = sum % 10; } //最后还有进位的话需要添加一位如,99+1 = 100 if(carry > 0) digits.insert(digits.begin(),carry); return digits; } };
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Undelete Plus v2.55 恢复误删除的文件 多国语言版下载
- Protector Plus Antivirus 2007 v8.0.A03 下载附注册机(keygen)
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- Windows SP2 免激活安装版 - DeepinXP PLUS V2 测试版 下载
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例