题目

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.

分析

该题目要求:将一整数按位存储在vector中,对其实现+1操作,返回结果。

是一道简单题目,对存储序列vector中元素,倒序遍历,末位+1,若<10可直接返回,否则,保存进位加之到下一位,循环至最高位。

若此时,进位依然为1,则新建长度增一的vector首位为1,其余为0,返回即可。

AC代码


class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len = digits.size(); if (len == 0)
return digits; int carry = 1;
for (int i = len - 1; i >= 0; --i)
{
digits[i] += carry;
if (digits[i] >= 10)
{
digits[i] -= 10;
carry = 1;
continue;
}
else{
carry = 0;
break;
}
} if (carry == 0)
return digits;
else
{
vector<int> v;
v.push_back(1);
for (int i = 0; i < len; i++)
v.push_back(0);
return v;
}
}
};

GitHub测试程序源码

最新文章

  1. Create Volume 操作(Part II) - 每天5分钟玩转 OpenStack(51)
  2. 炫酷CSS
  3. Atitit.病毒木马的快速扩散机制原理nio&#160;内存映射MappedByteBuffer
  4. oracle安装常见问题
  5. Android Studio如何发布APK
  6. C# RSA和Java RSA互通
  7. 查看某一个点是否在某个多边形内 使用ST_Contains函数
  8. C++ 基本数据结构整理
  9. T-Sql(一)简单语法
  10. SQL SERVER的统计信息
  11. 防火墙上开放Oracle服务端口1521的方法
  12. RabbitMQ消息队列之二:消费者和生产者
  13. java 坑总结
  14. PHP中empty,is_null,isset的区别
  15. IOS开发常用宏定义
  16. django restframework 序列化
  17. 发发牢骚,觉得走c#这条路,不该太浮躁。
  18. iOS pch文件的创建
  19. Flex 布局教程:实例篇【转】
  20. libevent-signal(2)

热门文章

  1. 弹射起步~django
  2. nginx上游模块
  3. archive log full ora-00257
  4. nginx中常见的变量
  5. rhel 6.5--samba
  6. solr 常见异常
  7. 基于Java实现的冒泡排序算法
  8. T4308 数据结构判断
  9. 1   开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。   本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括:    正确的使用数据库MetaData方法    只获取需要的数据    选用最佳性能的功能    管理连
  10. hdu 2192 MagicBuilding