LeetCode 66 Plus One(加一)(vector)
翻译
给定一个以一系列数字表示的非负数。将其加一并转换成数字。
数字存储的最高位在列的最前面。
原文
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)的更多相关文章
- [LeetCode] 66. Plus One 加一
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- [leetcode]66. Plus One加一
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- 前端与算法 leetcode 66. 加一
目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...
- 每日一道 LeetCode (14):数组加一
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- LeetCode 66. Plus One(加1)
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...
- LeetCode(66): 加一
Easy! 题目描述: 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会 ...
- Java实现 LeetCode 66 加一
66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...
- [LeetCode]66. 加一(数组)
###题目 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 ...
- python(leetcode)-66加一问题
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入 ...
随机推荐
- 暴力 【p4092】[HEOI2016/TJOI2016]树
Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作: 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其 ...
- Manacher【SP7586】NUMOFPAL - Number of Palindromes
Description 求一个串中包含几个回文串. Input 输入一个字符串\(S\) Output 包含的回文串的个数. 看到题解里面有人人写回文自动机. 有必要那么麻烦嘛 emmm 我们直接跑\ ...
- SQL Loader with utf8
alter this line in your control file characterset UTF8 to this characterset UTF8 length semantics ch ...
- py thon 多线程(转一篇好文章)
http://www.cnblogs.com/fnng/p/3670789.html
- 【博弈论】poj2348 Euclid's Game
假设当前b>a. 一.b%a==0 必胜 二.b<2*a,当前我们没有选择的余地,若下一步是必胜(最终能到情况一),则当前必败:反之,当前必胜. 三.b>2*a,假设x是使得b-ax ...
- [美团 CodeM 初赛 Round A]最长树链
题目大意: 给你一棵带点权的树,找出一个最长的树链满足链上点权的最大公因数不为1. 思路: 暴力DP. 对于每个点,记录一下以这个点为一个端点的所有链的最大公因数及长度. 然后暴力转移一下,时间复杂度 ...
- &#x开头的是什么编码?
在 Node 层利用 cheerio 解析网页时,输出的中文内容都是以 &#x 开头的一堆像乱码一样的东西,尝试过各种编码都无效,而且神奇的是,将这一堆“乱码”保存成网页后,通过浏览器打开又可 ...
- 上传ipa文件时报错 Your account already has a valid iOS distribution certificate
这个问题是因为你本机的生产证书是在别人的电脑上创建的,所以才会提示你已经有一个有效的生产证书,但是没有安装到本地:
- C#测试程序运行时间
一.用C#自带的StopWatch函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 using System; usi ...
- iOS:IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。
原文链接:http://blog.csdn.net/daleiwang/article/details/34081231 (3)在项目的AppDelegate中一般情况下有三个操作,第一是注册Shar ...