翻译

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

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

原文

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. HDU 1754.I Hate It-结构体版线段树(单点更新+区间查询最值)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. poj1062 最短路径 dijkstra

    题目连接:http://poj.org/problem?id=1062 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用 1000 ...

  3. luogu P1359会议

    //以一号节点为根节点,求出所有节点到根结点的距离,以及所有点的子节点的个数 //然后计算根据已知信息计算所有节点到当前结点的距离 //然后扫描n个点,O(n)求解 #include<bits/ ...

  4. 洛谷——P2958 [USACO09OCT]木瓜的丛林Papaya Jungle

    P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...

  5. SPOJ UOFTCG - Office Mates (树的最小路径覆盖)

    UOFTCG - Office Mates no tags  Dr. Baws has an interesting problem. His N graduate students, while f ...

  6. CentOS下MySQL主从复制,读写分离

    1.环境:所有系统都是CentOS5.5 mysql-5.6.31-2.el5,MySQL中都没有数据 主服务器IP为192.168.128.230 从服务器IP为192.168.128.235 代理 ...

  7. sqlldr Field in data file exceeds maximum length "

    使用sqlldr导数时出现如下错误: " Record 1: Rejected - Error on table PC_PLANNAME, column PLANNAME.Field in ...

  8. POJ 2348 Euclid's Game(博弈论)

    [题目链接] http://poj.org/problem?id=2348 [题目大意] 给出两个数,两个参赛者轮流用一个数减去另一个数的倍数,当一个数为0的时候游戏获胜, 求先手是否必胜 [题解] ...

  9. 一个强大的UI node 抽象

    基于cocos2d -x的一个强大的 界面对象的基类 ---@type uinode ui 对象的抽象 --@usage -- 界面打开的执行流程 -- 带*的是可选重写的函数,不带*的为必须实现的 ...

  10. ToggleButton控件,Switch控件

    (一) 1.效果图    2. activity_main.xml <?xml version="1.0" encoding="utf-8"?> & ...