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返回。

如果直到最高位仍有进位,则在数组头部插入1,结果为100…0

解法一:

inplace但插入操作时会产生数组的移位

class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int carry = ;
for(int i = digits.size()-; i >= ; i --)
{
int sum = digits[i]+carry;
carry = sum / ;
digits[i] = sum % ;
if(carry == )
break;
}
if(carry == )
digits.insert(digits.begin(), );
return digits;
}
};

解法二:

inplace且数组不用移位

class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
for(int i = digits.size()-; i >= ; i --)
{
if(digits[i] <= )
{
digits[i] += ;
return digits;
}
else
{//
if(i != )
digits[i] = ;
else
{
digits[] = ;
digits.push_back();
return digits;
}
}
}
}
};

【LeetCode】66. Plus One (2 solutions)的更多相关文章

  1. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  2. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  3. 【LeetCode】66 & 67- Plus One & Add Binary

    66 - Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...

  4. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  5. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  6. 【LeetCode】66. 加一

    66. 加一 知识点:数组: 题目描述 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 ...

  7. 【LeetCode】66. Plus One 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数九 采用进位 日期 [LeetCode] 题目地址 ...

  8. 【LeetCode】338. Counting Bits (2 solutions)

    Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num  ...

  9. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

随机推荐

  1. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  2. Scala入门:从HelloWorld开始【源码及编译】

    最近在学习Scala语言,虽然还没有完全学通, 但是隐约可以体会到Scala的简洁和强大. 它既能让程序员使用函数式编程, 也提供了全面的面向对象编程. 在刚刚开始读<Scala编程>的时 ...

  3. ES6系列汇总

    汇 总 第一节:什么是ES6?新手该如何理解 第二节:ES6新增了let关键字,干嘛用的? 第三节:ES6中另一个不得不说的关键字const 第四节:教你如何快速让浏览器兼容ES6特性 第五节:一个令 ...

  4. mui 页面滚动解决方案

    默认情况下mui 页面不能滚动,以下为解决方案: 1. mui('.mui-scroll-wrapper').scroll({  deceleration: 0.0005 //flick 减速系数,系 ...

  5. objectiveC【语法】修饰符 static extern const

    const const最好理解,修饰的东西不能被修改 指针类型根据位置的不同可以理解成3种情况: I 常量指针 // 初始化之后不能赋值,指向的对象可以是任意对象,对象可变. NSString * c ...

  6. HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  7. C语言之数组中你所不在意的重要知识

    #include<stdio.h> void simpleArray(); void main() { simpleArray(); } //数组的简单操作 void simpleArra ...

  8. 关于hessian接口类方法顺序及对象序列化的实战研究

    前段时间,提供出去的hessian接口被调用时,调用方出现序列化异常以及其他莫名的异常.同事说hessian接口使用有2个注意事项: 1.对于已经存在的hessian接口,后续增加的方法只能加在文件末 ...

  9. javascript基础编程の变量、对象、数据类型及函数

    在web标准中.网页由结构.表现形式和行为三个部分组成. 结构标准---->XHTML: 表现形式标准----->CSS: 行为标准----->javascript: javascr ...

  10. 有用的iOS网站地址

    王巍 (@onevcat) 是一名 iOS 和 Unity3D 开发者,现旅居日本,正在寻求创意之源.http://swifter.tips/http://onevcat.com/2013/02/xc ...