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的更多相关文章

  1. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  2. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. [leetcode.com]算法题目 - Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  5. [leetcode.com]算法题目 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [leetcode.com]算法题目 - Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...

  9. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  10. [leetcode.com]算法题目 - Pow(x, n)

    Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...

随机推荐

  1. 一台老服务器的升级日志(PHP4.4.9+MySQL5.1)升级MySQL5.6

    1.备份数据库 mysqldump -uroot -p --all-databases > databases.sql 2.停止mysql服务 service mysqld stop 3.卸载旧 ...

  2. jQuery实现多个ajax请求等待

    通常,jQuery的函数ajax进行Ajax调用.函数ajax只能做一个Ajax调用.当Ajax调用成功时,执行回调函数.可选地,当Ajax调用返回错误时,调用另一个回调函数.但是,该功能不能根据这些 ...

  3. 三大框架中各种xml的存放位置

      web.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar ...

  4. svn 修改原来包名的方法和会报的错误

    SVN E200009 which is not part of the commit; both sides of the move must be committed together 在svn上 ...

  5. mybatis学习七 typeAliases 别名

    1. mybatis中内置的一些别名,例如Map,List,int 等常用类型 2.手动为某个类设置别名 在mybatis的全局配置文件中加如下代码 <typeAliases> <t ...

  6. docker使用自定义镜像zabbix服务

    一.关闭firewall,永久关闭,使用iptables防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firew ...

  7. 2018.06.26 NOIP模拟 号码(数位dp)

    题目背景 SOURCE:NOIP2015-GDZSJNZX(难) 题目描述 Mike 正在在忙碌地发着各种各样的的短信.旁边的同学 Tom 注意到,Mike 发出短信的接收方手机号码似乎都满足着特别的 ...

  8. redis 的一主二从三哨兵模式

    概述 在部署redis 的时候,如果redis宕机,缓存将不可用,redis提供了哨兵模式保证redis实现高可用. 即一台主机两台从机,三台哨兵主机,如果主实例宕机,哨兵将将一台从机升级为主机.实现 ...

  9. GHOST完成后出现GRUB解决方法

    1.试一下这个命令: grub> rootnoverify (hd0,0)(注意空格!!!) 或者 grub>makeacrive (hd0,0)grub> chainloader ...

  10. BZOJ 2005 [Noi2010]能量采集 (数学+容斥 或 莫比乌斯反演)

    2005: [Noi2010]能量采集 Time Limit: 10 Sec  Memory Limit: 552 MBSubmit: 4493  Solved: 2695[Submit][Statu ...