407-加一

给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。

该数字按照大小进行排列,最大的数在列表的最前面。

样例

给定 [1,2,3] 表示 123, 返回 [1,2,4].

给定 [9,9,9] 表示 999, 返回 [1,0,0,0].

标签

数组 谷歌

思路

模拟加法,从个位(数组最后一位)+1,若此位产生进位,继续向前一位加进位,若是最高位(数组第一位)还有进位,则插入新的最高位

code

class Solution {
public: /*
* @param digits: a number represented as an array of digits
* @return: the result
*/
vector<int> plusOne(vector<int> digits) {
// write your code here
int size = digits.size();
if (size <= 0) {
return vector<int>();
}
int carry = 0;
digits[size - 1]++;
if (digits[size - 1] < 10) {
return digits;
}
carry = 1;
digits[size - 1] = 0;
for (int i = size - 2; i >= 0; i--) {
digits[i] = digits[i] + carry;
if (digits[i] > 9) {
carry = 1;
digits[i] = 0;
}
else {
carry = 0;
break;
}
}
if (carry == 1) {
digits.insert(digits.begin(), 1);
}
return digits;
}
};

lintcode-407-加一的更多相关文章

  1. LintCode 407: Plus One

    LintCode 407: Plus One 题目描述 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组. 该数字按照位权大小进行排列,位权最大的数在列表的最前面. 样例 给定 ...

  2. LintCode之加一

    题目描述: 分析:由样例可以知道,当数组的每一个数字都是9时,加一会产生一个最高位的数字1,所以先判断这个数组的每一位是否都是9,如果是,那么新数组的大小是原数组大小加一,否则新数组的大小等于原数组的 ...

  3. lintcode:Plus One 加一

    题目: 加一 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组. 该数字按照大小进行排列,最大的数在列表的最前面. 样例 给定 [1,2,3] 表示 123, 返回 [1,2,4 ...

  4. [LintCode] Plus One 加一运算

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  5. 407. Plus One【LintCode java】

    Description Given a non-negative number represented as an array of digits, plus one to the number. T ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  8. [LintCode] Mini Twitter 迷你推特

    Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text). Post a twe ...

  9. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  10. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

随机推荐

  1. HTML5之拖拽

    HTML5拖放 拖放(Drag和drop)是H5标准的组成部分 此处需具备js基础知识及其H5拖拽部分相关方法 在拖动目标上触发事件 (源元素): ondragstart - 用户开始拖动元素时触发 ...

  2. Delphi开发Android的几个注意

    Delphi在Android开发中还不是很完善,也有一些修改,需要注意: 1.不要用IXMLDocument的SaveToStream, XML.Text等,用了OXML,QXML,VerySimpl ...

  3. centos6.9安装虚拟机kvm

    说明 以下所有操作都基于centos6.9 查看系统是否支持虚拟化 结果有vmx|svm才支持虚拟化 egrep '(vmx|svm)' --color=always /proc/cpuinfo 系统 ...

  4. 基于opencv和qt的人脸检测小系统

    摘要:利用opencv读取视频.图片并检测人脸,利用QT显示窗口,功能选择等 环境:Ubuntu18.04.OpenCV3.4.0.QT5.10.1 效果图: 代码如下(比较简单没什么注释): mai ...

  5. Python3中高阶函数lambda,filter,map,reduce,zip的详细用法

    在Python里有五大高阶函数,他们分别是lambda()匿名函数,filter()筛选函数,map()函数,reduce()函数,zip()函数.下面就让我们来详细的了解一下这五种函数的具体用法吧. ...

  6. Linux使用scp命令进行文件远程拷贝详解

    前言 scp是 secure copy的缩写, scp是Linux系统下基于ssh登陆进行安全的远程文件拷贝命令.Linux的scp命令可以在Linux服务器之间复制文件和目录. 使用语法: scp  ...

  7. DP_最长公共子序列/动规入门

    学自:https://open.163.com/movie/2010/12/L/4/M6UTT5U0I_M6V2U1HL4.html 最长公共子序列:(本文先谈如何求出最长公共子序列的长度,求出最长公 ...

  8. 【洛谷】 3264 [JLOI2015] 管道连接

    前言:     如果还不知道斯坦纳树的童鞋可以看这两篇博客: 我的:https://blog.csdn.net/jerry_wang119/article/details/80001711 我一开始学 ...

  9. # 2017-2018-1 20155232《信息安全技术》实验二——Windows口令破解

    2017-2018-1 20155232<信息安全技术>实验二--Windows口令破解 [实验目的] 了解Windows口令破解原理 对信息安全有直观感性认识 能够运用工具实现口令破解 ...

  10. 二分查找的C#实现

    二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列. 查找过程 首先,假设表中元素是按升序排列, ...