题目

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

The digits are stored such that the most significant digit is at the head of the list.

分析

该题目要求:将一整数按位存储在vector中,对其实现+1操作,返回结果。

是一道简单题目,对存储序列vector中元素,倒序遍历,末位+1,若<10可直接返回,否则,保存进位加之到下一位,循环至最高位。

若此时,进位依然为1,则新建长度增一的vector首位为1,其余为0,返回即可。

AC代码


class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len = digits.size(); if (len == 0)
return digits; int carry = 1;
for (int i = len - 1; i >= 0; --i)
{
digits[i] += carry;
if (digits[i] >= 10)
{
digits[i] -= 10;
carry = 1;
continue;
}
else{
carry = 0;
break;
}
} if (carry == 0)
return digits;
else
{
vector<int> v;
v.push_back(1);
for (int i = 0; i < len; i++)
v.push_back(0);
return v;
}
}
};

GitHub测试程序源码

LeetCode(66)Plus One的更多相关文章

  1. LeetCode(66): 加一

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

  2. Qt 学习之路 2(66):访问网络(2)

    Home / Qt 学习之路 2 / Qt 学习之路 2(66):访问网络(2) Qt 学习之路 2(66):访问网络(2)  豆子  2013年10月31日  Qt 学习之路 2  27条评论 上一 ...

  3. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  4. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  7. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  8. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  9. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

随机推荐

  1. Beta版本冲刺第一天!

    该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业地址:https://edu.cnblogs.com/c ...

  2. python中时间日期格式化符号的含义

    %y   两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H    24小时制小时数(0-23) %I   12 ...

  3. archive log full ora-00257

    ############# sample 0 asmcmd show free 37G in archive_log ASMCMD> lsdgState Type Rebal Unbal Sec ...

  4. [未读]backbonejs应用程序开发

    买来就没有动过,那阵子刚好离职找工作,之后学backbone的劲头就过去了= =

  5. Suricata的规则解读(默认和自定义)

    不多说,直接上干货! 见suricata官网 https://suricata.readthedocs.io/en/latest/rules/index.html 一.Suricata的规则所放位置 ...

  6. wamp无法进入phpMyAdmin或localhost的解决方法

    我用的是最新版的wampsever5,在win7(64位)下安装正常使用,没有无法进入phpMyAdmin的问题,但是我在虚拟机安装了win8(64位专业版),测试在win8下面的使用情况时,就有问题 ...

  7. Java_面向对象中的this和super用法

    this: 1.使用在类中,可以用来修饰属性.方法.构造器 2.表示当前对象或者是当前正在创建的对象 3.当形参与成员变量重名时,如果在方法内部需要使用成员变量,必须添加 this 来表明该变量时类成 ...

  8. AJPFX总结内部类

    内部类:内部类的访问规则:1. 内部类可以直接访问外部类中的成员,包括私有   原因是内部类中持有了一个外部类的引用,格式:外部类.this2. 外部类要访问内部类,必须建立内部类对象访问格式:1.  ...

  9. 001.JS特效

    一.Js实现单行文本的滚动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  10. requirejs&&springboot

    1.Spring Boot Spring boot 基础结构主要有三个文件夹: (1)src/main/java  程序开发以及主程序入口 (2)src/main/resources 配置文件 (3) ...