翻译

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

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

原文

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. Codeforces 538 A. Cutting Banner-substr()函数字符串拼接

      A. Cutting Banner   time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. hdu6086(AC 自动机)

    hdu6086 题意 字符串只由 \(01\) 组成,求长度为 \(2L\) 且包含给定的 \(n\) 个子串的字符串的个数(且要求字符串满足 \(s[i] \neq s[|s| - i + 1]\) ...

  3. Oracle doesn't have on duplicate key update Use MERGE instead:

    Oracle doesn't have on duplicate key update Use MERGE instead: MERGE INTO my_table trg USING (SELECT ...

  4. small test on 5.30 night T2

    (题面写错了,应该是一条从b -> a 的边) 让我们设状态 (a,b,c) 表示存在一个点k,使得  dist(k,b) - dist(k,a) * 2 + 3 = c,显然这里的第三维可以压 ...

  5. [BZOJ3684]大朋友和多叉树

    设答案为$f_s$,它的生成函数为$\begin{align*}F(x)=\sum\limits_{i=0}^\infty f_ix^i\end{align*}$,则我们有$\begin{align* ...

  6. 【分块】hdu5057 Argestes and Sequence

    分块,v[i][j][k]表示第i块内第j位是k的元素数.非常好写.注意初始化 要注意题意,①第i位是从右往左算的. ②若x没有第i位,则用前导零补齐10位.比如103---->00000001 ...

  7. 【树状数组】bzoj2789 [Poi2012]Letters

    处理数组A,A[i]表示字符串a的第i个字符排序后应去的位置,队列可. 对于多次出现的字符,其在a中的顺序和在b中的顺序应该是一一对应的. #include<cstdio> #includ ...

  8. 【强联通分量缩点】【搜索】bzoj2208 [Jsoi2010]连通数

    两次dfs缩点,然后n次dfs暴搜. #include<cstdio> #include<vector> #include<cstring> using names ...

  9. 130804组队练习赛ZOJ校赛

    A.Ribbon Gymnastics 题目要求四个点作圆,且圆与圆之间不能相交的半径之和的最大值.我当时想法很简单,只要两圆相切,它们的半径之和一定最大,但是要保证不能相交的话就只能取两两个点间距离 ...

  10. WebLogic Cluster Sevlet的配置

    虽然生产环境中不建议使用,但因为客户需要考试可能用到,所以又做了一遍 1. 配置受管Server,ProxyServer,过程略 2.构建Proxy Application 建立一个ProxyApp的 ...