【LeetCode】66. 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.
提示:
此题其实是模拟我们在小学时候学习的加法进位的操作。难度不大。
代码:
空间复杂度是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的更多相关文章
- 【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. ...
- 【LeetCode】66. 加一
66. 加一 知识点:数组: 题目描述 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 ...
- 【LeetCode】66. Plus One 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数九 采用进位 日期 [LeetCode] 题目地址 ...
- 【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 ...
- 【LEETCODE】66、字符串分类,hard级别,题目:32,72,76
package y2019.Algorithm.str.hard; import java.util.Stack; /** * @ProjectName: cutter-point * @Packag ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- .NET面试题系列[18] - 多线程同步(1)
多线程:线程同步 同步基本概念 多个线程同时访问共享资源时,线程同步用于防止数据损坏或发生无法预知的结果.对于仅仅是读取或者多个线程不可能同时接触到数据的情况,则完全不需要进行同步. 线程同步通常是使 ...
- struts2.1.6教程十二、总结
本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例. 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官 ...
- 实现图标Icon+文字在div里自动中心居中(水平垂直居中)
已知div行高设置text-align:center文字会自动居中. 通过:before来设置icon的地址和高宽. 需要设置图片默认的垂直居中条件,与文字一致,为text-bottom. 设置图片行 ...
- java 与操作系统同步问题(三)————父亲儿子女儿水果问题
问题描述:父亲每次都会放一个水果在桌子上,女儿喜欢吃香蕉(只吃香蕉), 儿子喜欢吃苹果(只吃苹果).父亲每次只会随机往桌子上放一个水果(苹果或香蕉),儿子,女儿会来取.使用p.v操作来完成父亲.儿子. ...
- linux压缩及vi操作
一:Linux的压缩方式 1.tar cvf 对文件进行压缩,tar cvf+压缩文件完成的命名+需要压缩的文件 2,tar -tf +命名的压缩文件:表示查看目录里面的内容 3,tar -xf 解压 ...
- javascript基础-表单
图解: 表单只需要知道常用的,其他的了解原理就行.在实际项目中,序列化一般用库(jquery)带的方法.富文本引用组件.
- OpenCV中的结构体、类与Emgu.CV的对应表
OpenCv中的 C 结构 OpenCV中的 C++ 封装 Emgu.CV中的 C# 封装 OpenCV 和 Emgu.CV 中的结构罗列 谢谢阅读,有误希望指正 原文地址 Basic Structu ...
- C# 委托的理解
1.什么是委托 委托可以理解为持有一个或多个方法的对象.如果执行委托的话,委托会 执行它所"持有"的方法.委托可以避免程序中大量使用if-else语句,使 程序拥有更好的扩展性. ...
- C#实现SQLSERVER数据库中有序GUID生成(NewSequentialId)
GUID作为数据库主键由于其无序性所以性能不怎么好,SQL Server中有个函数NewSequentialId可以生成有序的GUID,由于在程序中需要用到,就用C#实现了一下,生成的GUID格式基本 ...
- sublime 新手代码提示
有提示的 你按 table 试试这就是按过的结果 是不是很方便这是按后的效果 是不是很方便 下面是各种简写效果html <html></html> ...