题目:

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.

提示:

此题其实是模拟我们在小学时候学习的加法进位的操作。难度不大。

代码:

空间复杂度是O(n)的方法:

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

空间复杂度是O(1)的方法:

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

【LeetCode】66. Plus One的更多相关文章

  1. 【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. ...

  2. 【LeetCode】66. 加一

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

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

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

  4. 【LeetCode】66. Plus One (2 solutions)

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

  5. 【LEETCODE】66、字符串分类,hard级别,题目:32,72,76

    package y2019.Algorithm.str.hard; import java.util.Stack; /** * @ProjectName: cutter-point * @Packag ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. SUSE 11下安装DPDK

    SUSE下安装与centos下安装有稍许不同: # cd  dpdk-1.8.0 # grep -rn Werror . |grep -iE "Makefile|mk" |awk ...

  2. call,apply和bind,其实很简单

    call和apply call和aplly作用完全一样,都是在特定的上下文中调用函数,或者说改变函数内部的this指向:区别仅在于接收参数的方式不同. var dog = { name: " ...

  3. 【2017-05-19】WebForm复合控件

    自动提交的属性: AutoPostBack="True" 1.RadioButtonList     单选集合 -属性:RepeatDirection:Vertical (垂直排布 ...

  4. Vmware报错:此主机支持IntelVTx 但IntelVTx处于禁用状态

    "此主机支持IntelVTx 但IntelVTx处于禁用状态",报错原因:电脑未开启虚拟化 解决方案: 电脑关机(是关机不是重启)--开机,进BIOS --选择 configura ...

  5. css样式自动换行/强制换行

    写样式时遇到的英文字符超出容器问题,度娘后了解下列知识,与大家分享,同时以便自己日后回顾. 一.自动换行问题 正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大. 下面介绍的是CSS如何 ...

  6. 外部无法捕捉Realm的doGetAuthenticationInfo方法抛出的异常

    shiro权限框架,用户登录方法的subject.login(token)会进入自定义的UserNamePasswordRealm类的doGetAuthenticationInfo身份验证方法 通常情 ...

  7. Some 3D Graphics (rgl) for Classification with Splines and Logistic Regression (from The Elements of Statistical Learning)(转)

    This semester I'm teaching from Hastie, Tibshirani, and Friedman's book, The Elements of Statistical ...

  8. 项目管理之 Git 管理软件 SourceTree for Mac

    Git 项目管理: Mac Terminal 生成 Git 秘钥流程: git config --global user.name "yourname" git config -- ...

  9. day5_ 导入模块和包

    ######################模块导入模块做的事1.产生新的名称空间2.以新建的名称空间为全局名称空间,执行文件的代码3.拿到一个模块名spam,指向spam.py产生的名称空间 imp ...

  10. 3.Node.js 自定义微信菜单

    文章目录:         1.Node.js 接入微信公众平台开发         2.Node.js access_token的获取.存储及更新         3.Node.js 自定义微信菜单 ...