【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 digits are stored such that the most significant digit is at the head of the list.
从个位数字往前扫,只要一个数字不产生进位,即可加1返回。
如果直到最高位仍有进位,则在数组头部插入1,结果为100…0
解法一:
inplace但插入操作时会产生数组的移位
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int carry = ;
for(int i = digits.size()-; i >= ; i --)
{
int sum = digits[i]+carry;
carry = sum / ;
digits[i] = sum % ;
if(carry == )
break;
}
if(carry == )
digits.insert(digits.begin(), );
return digits;
}
};
解法二:
inplace且数组不用移位
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
for(int i = digits.size()-; i >= ; i --)
{
if(digits[i] <= )
{
digits[i] += ;
return digits;
}
else
{//
if(i != )
digits[i] = ;
else
{
digits[] = ;
digits.push_back();
return digits;
}
}
}
}
};
【LeetCode】66. Plus One (2 solutions)的更多相关文章
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【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】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 【LeetCode】66. 加一
66. 加一 知识点:数组: 题目描述 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 ...
- 【LeetCode】66. Plus One 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数九 采用进位 日期 [LeetCode] 题目地址 ...
- 【LeetCode】338. Counting Bits (2 solutions)
Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
随机推荐
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- JAVA容器-模拟LinkedList实现(双链表)
概述 LinkedList实质上就是双向链表的拓展的实现,我们将关注一下问题.LinkedList 1.双向链表怎么来实现插入.删除.查询? 2.利用二分法提高查询效率. 3.不同步,线程不安全,需要 ...
- 微信小程序缓存滑动距离,当页面浏览到一定位置,滑动其他页面后返回该页面记录之前的滑动距离
15.微信小程序缓存滑动距离 我们在浏览页面的时候,然后左滑或者右滑到新的页面,等返回此页面,我们希望可以记录上次滑动的距离 虽然这个实现起来并不难,但是会遇到一些坑,因为scroll-view的组件 ...
- 基于ARM的射频识别读卡器电路设计
http://tech.yktworld.com/201010/201010032128115666.html 来源:一卡通世界 作者:江小平,李中捷,余晓峰 2010-10-3 ...
- GCC降级
前阵子将Ubuntu升级到了12.04,原来装得virtualbox也可以正常使用.后来几次内核升级之后,virtualbox突然不能用了.virtualbox提示进行/etc/init.d/vbox ...
- Python break 语句
Python break 语句 Python break语句,就像在C语言中,打破了最小封闭for或while循环. break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归 ...
- 固定的价格就意味着背叛——《practices of an agile developper》
“对这个项目,我们必须要有固定的报价.虽然我们还不清楚项目的具体情况,但仍要有一个报价.到星期一,我需要整个团队的评估,并且我们必须要在年末交付整个项目.” Venkat & Andy 提出了 ...
- 8)Linux程序设计入门--线程操作
)Linux程序设计入门--线程操作 前言:Linux下线程的创建 介绍在Linux下线程的创建和基本的使用. Linux下的线程是一个非常复杂的问题,由 于我对线程的学习不时很好,我在这里只是简单的 ...
- MVC4 过滤器(转)
先来看看一个例子演示过滤器有什么用: public class AdminController : Controller { // ... instance variables and constru ...
- Informatica 常用组件Source Qualifier之五 User Defined Join
User defined join : 输入用户定义的联接与输入自定义 SQL 查询类似.但是,只需输入 WHERE 子句的内容,而不是整个查询. 添加用户定义的联接时,源限定符转换包括默认 ...