题目:

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. 抓包工具 Charles 使用心得

    前言 虽然实习工作还没有着落,但学习还是要继续的嘛,今天就来学习使用下 Mac 下截取网络封包的工具:Charles. 我想,如果你是个善于利用搜索引擎的人,那么在 Google 中输入「Charle ...

  2. 关于XML(可扩展标记语言)的基础知识与写法------2017-05-18

    XML(Extensible Markup Language) HTML:超文本标记语言,主要用来展示   XML:可扩展标记语言,用来做数据传输XML特点: 1.树状结构,有且只有一个根 2.标签名 ...

  3. 单词计数,杭电0j-2072

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 [Problem Description] lily的好朋友xiaoou333最近很空,他想了一 ...

  4. 【JAVAWEB学习笔记】04_JavaScript

    晨读单词: onmouseover:鼠标移入 onmouseout:鼠标移出 attribute:属性 node:节点 document:文档 element:元素 textNode:文本节点 app ...

  5. php 使用composer

    之前写过相关的composer,之后碰到了几个朋友问我,我整理了一下,方便自己也方便大家日后查阅~~不玩开源的程序员不是好厨子     1.执行在线安装         curl -sS https: ...

  6. Javascript性能优化之节流函数

    在我们的工作中往往有这样的需求,下拉上拉加载实现无限加载列表数据这样的一个功能,这个时候小伙伴们可能就觉得这个功能几分钟的事,于是乎,下边这段代码浩浩荡荡就出来了 window.addEventLis ...

  7. Json及Json字符串

    JSON(JavaScript Object Notation)是一种独立于开发语言的用于存储和交换文本数据的格式,JSON 语法是JavaScript 语法的子集. Json 可以保存数组格式和对象 ...

  8. 开涛spring3(12.1) - 零配置 之 12.1 概述

    12.1  概述 12.1.1  什么是零配置 在SSH集成一章中大家注意到项目结构和包结构是不是很有规律,类库放到WEB-INF/lib文件夹下,jsp文件放到WEB-INF/jsp文件夹下,web ...

  9. C# MVC权限验证

    前言 之前一直没怎么接触过权限验证这块,刚好公司老平台改版,就有了这篇权限验证.此篇文章大致讲解下 精确到按钮级别的验证如何实现.以及权限验证设计的参考思路(菜鸟一枚,大神勿喷). 在开发大项目的时候 ...

  10. 【面向对象设计原则】之接口隔离原则(ISP)

    接口隔离原则(Interface  Segregation Principle, ISP):使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口. 从接口隔离原则的定义可以看 ...