1. 题目

1.1 英文题目

Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

1.2 中文题目

给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

1.3输入输出

输入 输出
digits = [1,2,3] [1,2,4]
digits = [4,3,2,1] [4,3,2,2]
digits = [0] [1]

1.4 约束条件

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9

2. 实验平台

IDE:VS2019

IDE版本:16.10.1

语言:c++11

3. 程序

3.1 测试程序

#include "Solution.h"
#include <vector> // std::vector
#include<iostream> // std::cout
using namespace std; // 主程序
void main()
{
// 输入
vector<int> digits = { 9,9,9 }; Solution solution; // 实例化Solution
vector<int> output = solution.plusOne(digits); // 主功能 // 输出
for (auto i : output)
cout << i;
}

3.2 功能程序

3.2.1 最优算法

(1)代码

#pragma once
#include<vector> // std::vector
//#include<limits.h> // INT_MIN整型最小值
#include<algorithm> // std::max
using namespace std; //主功能
class Solution {
public:
vector<int> plusOne(vector<int>& digits)
{
int carry = 1; // 存放进位数字
int length = digits.size();
for (int i = length - 1; i >= 0; --i)
{
// 若某一位没有上一位的进位,则再高位都不会再有进位,也就是说高位都保持不变,可以直接返回结果
if (carry == 0) return digits; // 若有进位,则需要计算进位值及其当前位数字,也就是需要进行循环
int sum = digits[i] + carry;
digits[i] = sum % 10;
carry = sum / 10;
} // 若是最高位还需要进位,则需要在最高位插入"1"
if (carry == 1) digits.insert(digits.begin(), 1);
return digits;
}
};

参考:https://www.cnblogs.com/grandyang/p/4079357.html

(2)解读

参考:

https://blog.csdn.net/linhuanmars/article/details/22365957

https://blog.csdn.net/Tianc666/article/details/105769411

3.2.2 直观求解法

(1)代码1

#pragma once
#include<vector> // std::vector
//#include<limits.h> // INT_MIN整型最小值
#include<algorithm> // std::max
using namespace std; //主功能
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
vector<int> digitsNew(digits.size() + 1);//新数字(逆序)
int count = 1;//进位(0/1)
for (int i = 0; i < digits.size(); i++)
{
int tempOrder = digits.size() - 1 - i;
int sumTemp = digits[tempOrder] + count;
count = sumTemp / 10;
digitsNew[i] = sumTemp % 10;+
if (i == digits.size() - 1)
digitsNew[i + 1] = count;
} // 逆序转正序
int length = digits.size() + digitsNew.back();
vector<int> digitsFinal(length);
for (int i = length - 1; i >= 0; i--)
digitsFinal[i] = digitsNew[length - 1 - i];
return digitsFinal;
}
};

(2)思路

先逆转求值,再逆转过来。但是我的代码写法不太简洁,可以参考代码2

(3)代码2

#pragma once
#include<vector> // std::vector
//#include<limits.h> // INT_MIN整型最小值
#include<algorithm> // std::max
using namespace std; //主功能
class Solution
{
public:
vector<int> plusOne(vector<int> &digits)
{
vector<int> ret(digits);
reverse(ret.begin(), ret.end()); int flag = 1;
for(int i = 0; i < ret.size(); i++)
{
ret[i] += flag;
//这里的flag的结果要么是0,要么是1
flag = ret[i] / 10;
ret[i] %= 10;
} if (flag == 1)
ret.push_back(1); reverse(ret.begin(), ret.end()); return ret;
}
};

参考:https://theonegis.blog.csdn.net/article/details/44258329

3.2.3 其他算法

(1)代码

#pragma once
#include<vector> // std::vector
//#include<limits.h> // INT_MIN整型最小值
#include<algorithm> // std::max
using namespace std; //主功能
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int n = digits.size();
for (int i = n - 1; i >= 0; --i) {
if (digits[i] == 9) digits[i] = 0;
else {
digits[i] += 1;
return digits;
}
}
if (digits.front() == 0) digits.insert(digits.begin(), 1);
return digits;
}
};

参考:https://www.cnblogs.com/grandyang/p/4079357.html

(2)解读

将一个数字的每个位上的数字分别存到一个一维向量中,最高位在最开头,我们需要给这个数字加一,即在末尾数字加一,如果末尾数字是9,那么则会有进位问题,而如果前面位上的数字仍为9,则需要继续向前进位。具体算法如下:首先判断最后一位是否为9,若不是,直接加一返回,若是,则该位赋0,再继续查前一位,同样的方法,知道查完第一位。如果第一位原本为9,加一后会产生新的一位,那么最后要做的是,查运算完的第一位是否为0,如果是,则在最前头加一个1。

参考:https://www.cnblogs.com/grandyang/p/4079357.html

Leetcode No.66 Plus One(c++实现)的更多相关文章

  1. 【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. ...

  2. 【一天一道LeetCode】#66. Plus One

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】66. 加一

    66. 加一 知识点:数组: 题目描述 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 ...

  4. 【LeetCode】66. Plus One 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数九 采用进位 日期 [LeetCode] 题目地址 ...

  5. LeetCode OJ 66. Plus One

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  6. 【LeetCode】66. Plus One

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

  7. LeetCode(66): 加一

    Easy! 题目描述: 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会 ...

  8. 力扣(LeetCode) 66. 加一

    给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入 ...

  9. 【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 ...

  10. LeetCode第[66]题(Java):Plus One

    题目:数组加一 难度:Easy 题目内容:   Given a non-empty array of digits representing a non-negative integer, plus ...

随机推荐

  1. 解决1字节的UTF-8序列的字节1无效问题

    学习路上碰到了这个异常 解决方法如下: 1.手动将< ? xml version="1.0" encoding="UTF-8"?>中的UTF-8更改 ...

  2. node.js学习(4)事件

    1 导入事件库

  3. PyTorch 自动微分

    PyTorch 自动微分 autograd 包是 PyTorch 中所有神经网络的核心.首先简要地介绍,然后将会去训练的第一个神经网络.该 autograd 软件包为 Tensors 上的所有操作提供 ...

  4. 多加速器驱动AGX的目标检测与车道分割

    多加速器驱动AGX的目标检测与车道分割 Object Detection and Lane Segmentation Using Multiple Accelerators with DRIVE AG ...

  5. 菜鸟刷题路(随缘刷题):leetcode88

    lc88 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m - 1, j = ...

  6. 【NX二次开发】Block UI 树列表

    属性说明 属性   类型   描述   常规           BlockID    String    控件ID    Enable    Logical    是否可操作    Group    ...

  7. 【NX二次开发】体消参,移除体参数UF_MODL_delete_body_parms()

    例子: 源码: extern DllExport void ufusr(char *param, int *returnCode, int rlen) { UF_initialize(); tag_t ...

  8. MySQL 页完全指南——浅入深出页的原理

    之前写了一些关于 MySQL 的 InnoDB 存储引擎的文章,里面好几次都提到了页(Pages)这个概念,但是都只是简要的提了一下.例如之前在聊 InnoDB内存结构 时提到过,但当时的重点是内存架 ...

  9. 【复习】Listening and Reading Comprehension

    短对话 M: Why do you declare the news that you're pregnant on your blog directly? W: I'm so excited tha ...

  10. 示例讲解PostgreSQL表分区的三种方式

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 表分区是解决一些因单表过大引用的性能问题的方式,比如某张表过大就会造成查询变慢,可能分区是一种解决方案.一般建议 ...