题目:

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. SQL SERVER大话存储结构(3)_数据行的行结构

            一行数据是如何来存储的呢?     变长列与定长列,NULL与NOT NULL,实际是如何整理存放到 8k的数据页上呢?     对表格进行增减列,修改长度,添加默认值等DDL SQL ...

  2. python requests 模拟登陆网站,抓取数据

    抓取页面数据的时候,有时候我们需要登陆才可以获取页面资源,那么我们需要登陆以后才可以跳转到对应的资源页面,那么我们需要通过模拟登陆,登陆成功以后再次去抓取对应的数据. 首先我们需要通过手动方式来登陆一 ...

  3. JSP----获取表单参数

    在页面中可大量使用 request 对象来获取表单域的值,获取表单域的值有如下两个 方法. • String getParamete(String para mN ame): 获取表单域的值. • S ...

  4. 13、Java菜单条、菜单、菜单项

    13.Java菜单条.菜单.菜单项 一般用Java做界面时,都得牵涉到菜单条.菜单.菜单项的设计.菜单项放在菜单里,菜单放在菜单条里,且其字体均可设置. 13.1.菜单条(Menubar) Frame ...

  5. Java Garbage Collectors

    Generational Collectors (分代收集器) GC algos optimised based on two hypotheses / observations: Most obje ...

  6. Fragment回调接口应用间分享数据

    package com.example.mydemo; import java.util.List; import android.app.Activity; import android.app.A ...

  7. 详解Centos默认磁盘分区

    对于有经验的Linux系统管理员,在安装系统之前都会对系统的分区进行规划:针对这一需求,下面就通过默认的Centos分区与大家分享一些关于Linux系统的知识.Linux系统的磁盘命名规范:硬盘类型标 ...

  8. 如何运行容器?- 每天5分钟玩转 Docker 容器技术(22)

    上一章我们学习了如何构建 Docker 镜像,并通过镜像运行容器.本章将深入讨论容器:学习容器的各种操作,容器各种状态之间如何转换,以及实现容器的底层技术. 运行容器 docker run 是启动容器 ...

  9. VR全景:“互联网+之后的下一个“风口”

    2017年VR虚拟现实会成为流行趋势吗? 2017年,另一个时代正在悄然走来--720全景时代!如果你错过了前十年的互联网大爆发,千万不要再错过接下来十年的VR全景时代的机遇! VR全景是" ...

  10. 01--数据库MySQL:【数据库DB】和【数据库管理系统DBMS】 简介

    1.数据库DB 数据库:DB(DataBase) 按照一定规则存储在计算机的内部存储设备上被各种用户或者应用共享的数据集合 2.数据库管理系统DBMS 1)数据库管理系统DBMS:DBMS(DataB ...