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.

Have you met this question in a real interview?

Yes
Example

Given [1,2,3] which represents 123, return[1,2,4].

Given [9,9,9] which represents 999, return[1,0,0,0].

LeetCode上的原题,请参见我之前的博客Plus One

解法一:

class Solution {
public:
/**
* @param digits a number represented as an array of digits
* @return the result
*/
vector<int> plusOne(vector<int>& digits) {
vector<int> res;
int carry = , n = digits.size();
for (int i = n - ; i >= ; --i) {
int sum = digits[i] + carry;
res.insert(res.begin(), sum % );
carry = sum / ;
}
if (carry == ) res.insert(res.begin(), );
return res;
}
};

解法二:

class Solution {
public:
/**
* @param digits a number represented as an array of digits
* @return the result
*/
vector<int> plusOne(vector<int>& digits) {
for (int i = digits.size() - ; i >= ; --i) {
if (digits[i] < ) {
digits[i] += ;
return digits;
}
digits[i] = ;
}
digits.insert(digits.begin(), );
return digits;
}
};

[LintCode] Plus One 加一运算的更多相关文章

  1. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  2. 一个线程加一运算,一个线程做减一运算,多个线程同时交替运行--synchronized

    使用synchronized package com.pb.thread.demo5; /**使用synchronized * 一个线程加一运算,一个线程做减法运算,多个线程同时交替运行 * * @a ...

  3. velocity加减运算注意格式 ,加减号的左右都要有空格

    velocity加减运算注意格式 ,加减号的左右都要有空格 #set( $left= $!biz.value - $vMUtils.getReturnMoney($!biz.billBuy) )

  4. [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  5. C语言中指针变量的加减运算

    1.指针变量中存放的是地址值,也就是一个数字地址,例如某指针变量中的值是0x20000000,表示表示此指针变量存放的是内存中位于0x20000000地方的内存地址.指针变量可以加减,但是只能与整型数 ...

  6. 大整数加减运算的C语言实现

    目录 大整数加减运算的C语言实现 一. 问题提出 二. 代码实现 三. 效果验证 大整数加减运算的C语言实现 标签: 大整数加减 C 一. 问题提出 培训老师给出一个题目:用C语言实现一个大整数计算器 ...

  7. Linux中日期的加减运算

    Linux中日期的加减运算 目录 在显示方面 在设定时间方面 时间的加减 正文 date命令本身提供了日期的加减运算. date 可以用来显示或设定系统的日期与时间. 回到顶部 在显示方面 使用者可以 ...

  8. void *指针的加减运算

    1.手工写了一个程序验证void *指针加减运算移动几个字节: //本程序验证空类型指针减1移动几个字节 #include <stdio.h> int main(int argc, cha ...

  9. Leetcode 592.分数加减运算

    分数加减运算 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分 ...

随机推荐

  1. C字符数组赋值(转)

    举例如下: char a[10];1.定义的时候直接用字符串赋值char a[10]="hello";注意:不能先定义再给它赋值,如 char a[10]; a[10]=" ...

  2. C# 对象转换为byte[] ,byte[]还原对象

    /// <summary>  /// 将一个object对象序列化,返回一个byte[]          /// </summary>  /// <param name ...

  3. 什么时候使用CountDownLatch

    正如每个Java文档所描述的那样,CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行.在Java并发中,countdownlatch的概念是一 ...

  4. js基础知识总结(2016.11.1)

    js基础知识点总结 如何在一个网站或者一个页面,去书写你的js代码:1.js的分层(功能):jquery(tool) 组件(ui) 应用(app),mvc(backboneJs)2.js的规划():避 ...

  5. 将公司的主要项目从eclipse迁移到android studio for mac环境(1)

    上星期,我决定要解决这个问题.理由如下: 3个月之前,我已经投入一段时间要做好这个迁移工作,直到最后,我发现能够安装了,但是运行不了,这个过程也看不到bugly上传,在找不到原因的情况下,我放弃了. ...

  6. BZOJ1946 : [Ceoi2006]ANTENNA

    首先通过随机增量法求出最小覆盖圆,作为答案的上界. 然后二分答案,检验的时候枚举每个点作为原点,求出其他每个点被包括在圆内的角度区间,然后扫描线即可. 时间复杂度$O(Tn^2\log n)$. #i ...

  7. (转)distcp从ftp到hdfs拷贝文件

    link :http://blog.csdn.net/sptoor/article/details/11523469 distcp从ftp到hdfs拷贝文件: hadoop distcp ftp:// ...

  8. FastDFS原理

    转自:http://blog.chinaunix.net/uid-20196318-id-4058561.html 开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器 ...

  9. Hadoop核心组件

    1.Hadoop生态系统 2.HDFS(Hadoop分布式文件系统) 源自于Google的GFS论文,发表于2003年10月,HDFS是GFS克隆版. 是Hadoop体系中数据存储管理的基础.它是一个 ...

  10. Android Acitivity 生命周期

    Activity的生命周期: (1)启动Activity:系统会先调用onCreate方法,然后调用onStart方法,最后调用onResume,Activity进入运行状态. (2)当前Activi ...