翻译

给定一个以一系列数字表示的非负数。将其加一并转换成数字。

数字存储的最高位在列的最前面。

原文

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中的数字组合成数,然后对这个数进行加一操作。然后将这个数转换成vector的形式存储。

    int powTen(int num, int times) {
for (int i = 0; i < times; ++i) {
num *= 10;
}
return num;
} int getNumFromVector(vector<int>& digits) {
int num = 0;
for (int i = 0; i < digits.size(); ++i) {
num += powTen(digits[i], digits.size() - i - 1);
}
return num;
} vector<int> getVectorFromNum(int num) {
vector<int> v;
stack<int> s;
while (num >= 10) {
s.push(num % 10);
num /= 10;
}
s.push(num);
while (!s.empty()) {
v.push_back(s.top());
s.pop();
}
return v;
} vector<int> plusOne(vector<int>& digits) {
int num = getNumFromVector(digits);
num += 1;
return getVectorFromNum(num);
}

然而后来发现并不能通过。于是将int改成了long,改来改去还是不信。好吧题意不希望我这样写吧……没事,那就改,有的是思路。

详细代码见后文。首先对vector的最后一个数字进行加1操作。然后再開始遍历。并推断是否大于10。进行对应的操作。Ok,这里能做的都做了。

最后假设第一个数字是9然后加上1变成了10。或者其它大于10的情况,就启用了以下的if推断,这个占了整个代码的一半。

由于须要对整个vector进行重组。事实上我反而认为这样的方法写起代码来比較简单,仅仅只是不太好直观地了解。所以我还是更喜欢上面的代码,即便不符合题目的用意,可是毕竟更加的抽象。

代码

class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
digits[digits.size() - 1] += 1;
for (int i = digits.size() - 1; i > 0; --i) {
if (digits[i] >= 10) {
digits[i] -= 10;
digits[i - 1] += 1;
}
}
if (digits[0] >= 10) {
vector<int> newVec;
newVec.push_back(digits[0] - 9);
digits[0] -= 10;
for (auto i : digits) {
newVec.push_back(i);
}
return newVec;
}
return digits;
}
};

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

  1. [LeetCode] 66. Plus One 加一

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

  2. [leetcode]66. Plus One加一

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

  3. 前端与算法 leetcode 66. 加一

    目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...

  4. 每日一道 LeetCode (14):数组加一

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  5. LeetCode 66. Plus One(加1)

    Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...

  6. LeetCode(66): 加一

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

  7. Java实现 LeetCode 66 加一

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

  8. [LeetCode]66. 加一(数组)

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

  9. python(leetcode)-66加一问题

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

随机推荐

  1. UTC时间

    世界的每个地区都有自己的本地时间,在Internet及无线电通信时,时间的统一非常重要! 整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信中,为统一而普遍使用一个标准时间,称为通用 ...

  2. Codeforces 1039A. Timetable

    题目地址:http://codeforces.com/problemset/problem/1039/A 题目的关键在于理清楚思路,然后代码就比较容易写了 对于每一个位置的bus,即对于每一个i(i& ...

  3. 洛谷——P1088 火星人

    P1088 火星人 题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常 ...

  4. [luogu3455]ZAP-Queries

    有时候我们会遇到一类问题:求$f(n)$,当然它是不好直接计算的,但如果$F(n)=\sum\limits_{d|n}f(d)$或$F(n)=\sum\limits_{\substack{n|d\\d ...

  5. python 文件操作与集合

    对文件的操作 1.打开文件,获取句柄 2.根据句柄操作文件 3.关闭文件 现有文档 poem.txt 一天很短, 短得来不及拥抱清晨, 就已经手握黄昏. 一年很短, 短得来不及细品初春殷红窦绿, 就要 ...

  6. 安装scrapy报错问题解决

    今天在安装scrapy时候,最后一步出现下面报错(操作系统为centerOS 6.4) error: Setup script exited with error: command 'gcc' fai ...

  7. Debian安装VirtualBox增强工具

    切换到root用户: apt-get install build-essential 或者 apt-get install gcc make apt-get install dkms apt-get ...

  8. Amazon EC2安装mysql多实例并配置主从复制

    1.MySQL安装: EC2直接使用yum安装mysql很方便: yum install mysql mysql-server mysql-libs 此处安装了三个包 完事之后可以直接使用 servi ...

  9. jQuery:多个AJAX/JSON请求相应单个回调

    原文链接:jQuery: Multiple AJAX and JSON Requests, One Callback 原文日期: 2014年4月15日 翻译日期: 2014年4月22日 翻译人员: 铁 ...

  10. [Flutter] Creating, Importing & Using Dynamic Widgets from Other Files in a Flutter Application

    In this lesson we’ll learn how to import widgets we’ve created in other files & use them in our ...