Plus One
2015-09-13 17:28
323 查看
【题目描述】
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 len=digits.size(); if(digits[len-1]!=9){ digits[len-1]=digits[len-1]+1; } else{ for(int i=len-1;i>=0;i--){ if(digits[i]==9){ digits[i]=0; } else{ digits[i]+=1; break; } } } if(digits[0]==0){ digits.insert(digits.begin(),1); } return digits; } };
相关文章推荐
- 2015调整数组的顺序使奇数位于偶数的前面(不用考虑排序)
- Java网络编程 UDP发送与接收数据
- ArrayList底层实现(JAVA)
- 如何创建一个控制器
- 《恶意代码分析实战》学习笔记(1)
- HashMap
- Great programmers are born, not made
- 汉诺塔
- 经济--互联网金融的远景
- 关于spring整合hibernate 懒加载异常解决方案
- git 查看远程分支、本地分支、创建分支、把分支推到远程repository、删除本地分支
- iOS设计模式——Category
- 常用几种设计模式
- GitHub常用命令总结
- CATransition转场动画
- MySQL数据库查询步骤和缓存原理
- 良好的编程习惯(一)
- Autoencoder
- Activity的生命周期
- 《剑指offer》表示数值的字符串