[leetcode.com]算法题目 - Plus One
Given a number represented as an array of digits, plus one to the number.
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
bool allNine = true;
int size = digits.size(); for(int i = ; i< size; i++){
if(digits[i] != ){
allNine = false;
break;
}
} if(allNine){
vector<int> result(+size, );
result[] = ;
return result;
} vector<int> result(size); for(int i=;i<size;i++){
result[i]=digits[i];
} result[size-] += ;
int k = size-; while(==result[k]){
result[k] = ;
k--;
result[k]++;
} return result;
}
};
我的答案
思路:可能出现的意外情况只有数字全是9的时候,这种情况单独拿出来讨论一下,剩下的情况都不可能全为9了。
[leetcode.com]算法题目 - Plus One的更多相关文章
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode.com]算法题目 - Pow(x, n)
Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...
随机推荐
- 字符串方法 split() & replace()
split() 语法:stringObject.split(separator) 功能:把一个字符串分割成字符串数组 返回值:Array 说明:separator 是必须的,分隔符. var str= ...
- python下使用opencv拍照
首先在命令中安装opencv: pip install opencv-python 然后打开notebook: jupyter notebook 建立文件,写入如下代码: import cv2 cap ...
- docker 批量操作容器
docker stop $(sudo docker ps -q)
- Java往hbase写数据
接上篇读HDFS 上面读完了HDFS,当然还有写了. 先上代码: WriteHBase public class WriteHBase { public static void writeHbase( ...
- python的6种基本数据类型--字典
python的6种基本数据类型--字典 字典 字典的定义与特性 字典是Python语言中唯一的映射类型. 定义:{key1:value1,key2:value2} 1.键与值用冒号":& ...
- Redis和RabbitMQ在项目中的使用
一:Redis的使用 1.先引入pom.xml的依赖 <dependency> <groupId>redis.clients</groupId> <artif ...
- AngularJS实战之Controller之间的通信
我们时常会在不同controller之间进行通信,接下来就介绍三种controller之间的通信方式 一.使用$on.$emit和$broadcast进行controller通信 虽然AngularJ ...
- IE上如何设置input type=file的光标不闪烁
我们使用文件上传时,时常自定义图标,这时候通常会把input的透明度设置为0,但是在IE上使用时会出现光标闪烁问题 解决办法 css设置font-size为0
- Codeforces Round#416 Div.2
A. Vladik and Courtesy 题面 At regular competition Vladik and Valera won a and b candies respectively. ...
- DynamicJson-好用dotnet4的json对象
最近看的书很杂,也深深被ES6的语法感动了,原来javascript可以这么好用.于是重新学习C#4.5.6,发现有一个dynamic就象是一个javascript对象,好玩!且有人写了一个json到 ...