[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 ...
随机推荐
- mysql cmd 无法登录
第一次折腾mysql诉苦记 版本注明: mysql 5.7.21 本地部署mysql,配置完成后(配置没有问题) cmd命令连接mysql: mysql -uroot -p 提示: ERROR 104 ...
- centos6.5上配置apache + mysql + php4.4.9 + eaccelerator-0.9.5 + postgresql-8.3.13 备忘
1.apache + mysql 直接利用 yum 安装 yum -y install httpd httpd-devel mysql mysql-server httpd-manual mod_pe ...
- 将hibernate框架融入到spring框架中
第一步:首先创建表: create table user( id int(2) primary key,name varchar(20),password varchar(20)); 第二步:建立d ...
- 【Java】Maven Tomcat插件使用
本例是用的是tomcat7-maven-plugin 插件 依赖 tomcat7-maven-plugin 插件的pom.xml依赖为 <dependency> <groupId&g ...
- Java的GUI设计小技巧
不可关闭窗口 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
- php中 isset函数有什么功能
isset是判断一个变量是否定义过即使它没有值,返回值也是true比如$name="";或var $name;那么if(isset($name))echo 1;它也会输出1,因为$ ...
- 2019.02.09 bzoj4710: [Jsoi2011]分特产(容斥原理)
传送门 题意简述:有nnn个人,mmm种物品,给出每种物品的数量aia_iai,问每个人至少分得一个物品的方案数(n,m,每种物品数≤1000n,m,每种物品数\le1000n,m,每种物品数≤10 ...
- 2019.01.13 bzoj4137: [FJOI2015]火星商店问题(线段树分治+可持久化01trie)
传送门 题意:序列上有nnn个商店,有两种事件会发生: sss商店上进购标价为vvv的一个物品 求编号为[l,r][l,r][l,r]之间的位置买ddd天内新进购的所有物品与一个数xxx异或值的最大值 ...
- Java核心技术之基础知识
一.类型转换 数值类型之间的转换 强制类型转换 a) 将一个数值强制转换成另一种类型时,如果超出目标类型的便是范围,结果就会截断成一个完全不同的值.(如:(byte)300的实际值为44) ...
- The XOR Largest Pair(Tire字典树应用)
题目链接:传送门 思路:建立一个32位的字典树,对每一个要插入的数字查找它异或的最大值(就是尽量全部二进制的值都相反), 然后获得两个数异或的最大值. #include<iostream> ...