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. IOS初级:NSUserDefaults

    NSUserDefaults(偏好设置),一个APP对应一个偏好设置 保存/新增数据 //存储数据 NSUserDefaults *defaults = [NSUserDefaults standar ...

  2. DB2 OLAP函数的使用

    说起 DB2 在线分析处理,可以用很好很强大来形容.这项功能特别适用于各种统计查询,这些查询用通常的SQL很难实现,或者根本就无发实现.首先,我们从一个简单的例子开始,来一步一步揭开它神秘的面纱,请看 ...

  3. The Django Book第六章(Admin)随笔

    要使用Django自带的管理界面,首先得激活- 激活的前提首先在你的项目的seeting目录下的INSTALL_APPS必须有以下的的包 django.contrib.admin django.con ...

  4. 消除flex-wrap之后每个item上下的距离

    设置flex-wrap后,每个item上下都会有距离.更改父元素的高度,就可以删除这些距离. 更改后:

  5. IE与非IE window.onload调用

    IEwin.attachEvent('onload', function(){ });非IEwin.onload=function(){}; if(navigator.appName == " ...

  6. Linux系统下修改环境变量PATH路径

    方法一: PATH=$PATH:/etc/apache/bin 该方法只对当前会话有效,每次注销或者拿出系统,该设置就会无效 方法二: vi /etc/profile 在适当的位置写入:PATH=$P ...

  7. Mybatis-Plus 实战完整学习笔记(七)------select测试二

    1.查询selectOne  (3.0.3版) @Test public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据 Empl ...

  8. 第07章:MongoDB-CRUD操作--文档--创建

    ①语法 insert() save()  --有修改没有新增 insertOne() [3.2版本新增]向指定集合中插入一条文档数据 insertMany() [3.2版本新增]向指定集合中插入多条文 ...

  9. java通过sftp对linux服务器文件夹进行操作

    本文主要讲sftp对linux服务器的文件和文件夹进行操作,windows server 服务器不支持. package com.lx.ftp; import java.io.File; import ...

  10. if结构和逻辑运算符

    一 :if选择结构 语法结构: 01.单个if if(表达式){ 如果满足表达式 则执行的代码 } 02.if(表达式) else if(表达式){ 如果满足表达式 则执行的代码 }else{ 不满足 ...