【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 ...
随机推荐
- Codeforces Round #202 (Div. 1) A. Mafia 贪心
A. Mafia Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...
- HashMap结构及使用
HashMap的数据结构 HashMap主要是用数组来存储数据的,我们都知道它会对key进行哈希运算,哈系运算会有重复的哈希值,对于哈希值的冲突,HashMap采用链表来解决的.在HashMap里有这 ...
- Microcontroller measures resistance without an ADC
Sensors automate most of the processes in industry. Most of these sensors, such as those for ammonia ...
- 【转】2012年6月26 – PPS网络电视PHP工程师最新面试题
每一次面试都是一场较量,和面试官,更是和你自己! 前言:虽然面试职位是PHP工程师,但题目仅绝非限于PHP,甚至都没有多少PHP的题!inner peace!希望能给你带了一丝帮助. PPS网络电视面 ...
- [SQL基础]入门
目录 什么是SQL? SQL能做什么? RDBMS SQL常见数据类型 SQL语法 什么是SQL? 结构化查询语言(Structured Query Language)简称SQL. 结构化查询语言是一 ...
- jdbc如何锁定某一条数据或者表,不让别人操作?
jdbc如何锁定某一条数据或者表,不让别人操作? 只有并发的时候才会有死锁,你要把多个涉及到这个表的地方检查一下,排除死锁可能. 为了避免修改冲突,所以我要锁定.请问该如何实现 答: 例如:selec ...
- [mark]如何删除地址栏的记录?
比如,我输入字母a ..因为经常访问.它首先会自动帮我补填 amazon.cn 第二行出现 ali213...第三行是 alipay... 这个很简单,没必要搞得很复杂很Geek.比如楼主的情况,首先 ...
- Eclipse经常使用的快捷键
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1aHVhbmNoYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Qt Creator怎样更改默认构建目录
用过VS的朋友都知道,用VS编译工程时会将生成的可执行文件放在当前工程目录下,使每个工程独立地成为一个整体,管理起来颇为方便:而Qt Creator则不同,编译程序时会创建一个与当前工程目录同级的构建 ...
- add-strings
https://leetcode.com/problems/add-strings/ package com.company; import java.util.LinkedList; import ...