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: 输入 ...
随机推荐
- HDU 3342 拓扑排序模板
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- dfs序学习总结
dfs序: 每个节点在dfs中的进出栈的时间序列. 树是非线性结构,根节点连着子节点,那么dfs序...节点进出栈的时间先后? 从根节点入栈,然后左儿子入栈,左儿子出栈,右儿子入栈,右儿子出栈,根节点 ...
- java中集合里的泛型
import java.util.ArrayList;/* * 泛型 : java jdk1.5新特性. * 泛型的好处 : * 1.将运行时的错诶提前到编译时. * 2.避免无谓的强制类型转换. * ...
- 【块状树】【树链剖分】bzoj1036 [ZJOI2008]树的统计Count
很早之前用树链剖分写过,但是代码太长太难写,省选现场就写错了. #include<cstdio> #include<algorithm> #include<cstring ...
- 【动态规划】【最长上升子序列】【贪心】bzoj1046 [HAOI2007]上升序列
nlogn求出最长上升子序列长度. 对每次询问,贪心地回答.设输入为x.当前数a[i]可能成为答案序列中的第k个,则若 f[i]>=x-k && a[i]>ans[k-1] ...
- Java高级架构师(一)第21节:通过X-gen生成商品模块
package com.sishuok.architecture1.goodsmgr.vo; import com.sishuok.architecture1.common.vo.BaseModel; ...
- mysql获取分类数量
1.sql <select id="getTypeNum" resultType="TypeNum" > select count(*) as al ...
- VS2017序列号|Visual Studio 2017 激活码 序列号
企业版:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF 专业版:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH
- 设置MySQL数据库名不区分大小写
Linux下的MySQL默认是区分表名大小写的,通过如下设置,可以让MySQL不区分表名大小写: 1.用root登录,修改 /etc/my.cnf: 2.在[mysqld]节点下,加入一行: lowe ...
- python的 json.dumps 中文编码
python的 json.dumps 中文编码 # -- coding: utf-8 -- 的作用:文件内容以utf-8编码 json.dumps 序列化时对中文默认使用的ascii编码, print ...