66. Plus One
2016-07-05 19:38
399 查看
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.
Subscribe to see which companies asked this question
代码:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int index=1;
for(int i= digits.size()-1; i>=0;--i)
{
if(index==1)
{
if(digits[i]==9)
{
digits[i]=0;
index=1;
}
else
{
digits[i]+=1;
index=0;
}
}
else
return digits;
}
if(index==1)
digits.insert(digits.begin(),1);
return digits;
}
};
The digits are stored such that the most significant digit is at the head of the list.
Subscribe to see which companies asked this question
代码:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int index=1;
for(int i= digits.size()-1; i>=0;--i)
{
if(index==1)
{
if(digits[i]==9)
{
digits[i]=0;
index=1;
}
else
{
digits[i]+=1;
index=0;
}
}
else
return digits;
}
if(index==1)
digits.insert(digits.begin(),1);
return digits;
}
};
相关文章推荐
- leetcode: Swap Nodes in Pairs
- Lua中assert( )函数的使用
- UVA 11384 正序数排列
- mysql的text的类型注意
- ViewPager的setOnPageChangeListener方法详解
- PHP调试
- how many ones?
- 深入解析HashMap、HashTable
- 嵌入式学习-uboot-lesson11-按键中断
- Objective-C实现一个简单的栈
- Web API集成Azure AD认证
- 图片下载和json下载(sd卡)
- 升级xcode7之后报错 n /Users/dpclove/Library/Developer/Xcode/DerivedData/
- GIT中文手册系列文章说明
- 欢迎使用CSDN-markdown编辑器
- iOS开发系列--App扩展开发
- Java jxl 基于模板导出excel
- TortoiseGit 更新Github 原作者的修改 然后提交到自己的Repository
- Wildcard Matching
- Spark的运行架构分析(二)之运行模式详解