https://leetcode.com/problems/add-to-array-form-of-integer/

For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  For example, if X = 1231, then the array form is [1,2,3,1].

Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.

Example 1:

Input: A = [1,2,0,0], K = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234

Example 2:

Input: A = [2,7,4], K = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455

Example 3:

Input: A = [2,1,5], K = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021

Example 4:

Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
Output: [1,0,0,0,0,0,0,0,0,0,0]
Explanation: 9999999999 + 1 = 10000000000

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 9
  3. 0 <= K <= 10000
  4. If A.length > 1, then A[0] != 0

代码:

class Solution {
public:
vector<int> addToArrayForm(vector<int>& A, int K) {
int n = A.size();
vector<int> ans;
string words1 = toString(K), words2 = "";
for(int i = 0; i < n; i ++)
words2 += ('0' + A[i]);
string sum = addStrings(words1, words2);
for(int i = 0; i < sum.length(); i ++)
ans.push_back(sum[i] - '0');
return ans;
}
string toString(int x) {
string s = "";
while(x) {
s += (x % 10) + '0';
x /= 10;
}
for(int i = 0; i < s.length() / 2; i ++)
swap(s[i], s[s.length() - i - 1]);
return s;
}
string addStrings(string num1, string num2) {
string c = "";
int len1 = num1.length();
int len2 = num2.length();
int len = max(len1, len2);
for(int i = len1; i < len; i ++)
num1 = "0" + num1;
for(int i = len2; i < len; i ++)
num2 = "0" + num2;
int ok = 0;
for(int i = len - 1; i >= 0; i --) {
char temp = num1[i] + num2[i] - '0' + ok;
if(temp > '9') {
ok = 1;
temp -= 10;
}
else ok = 0;
c = temp + c;
}
if(ok) c = "1" + c;
return c;
}
};

  数组模拟大数加法

 

#Leetcode# 989. Add to Array-Form of Integer的更多相关文章

  1. 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...

  2. 【leetcode】989. Add to Array-Form of Integer

    题目如下: For a non-negative integer X, the array-form of X is an array of its digits in left to right o ...

  3. LC 989. Add to Array-Form of Integer

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  ...

  4. 【Leetcode_easy】989. Add to Array-Form of Integer

    problem 989. Add to Array-Form of Integer 参考 1. Leetcode_easy_989. Add to Array-Form of Integer; 完

  5. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  6. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  7. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  8. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  9. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. [转]JIRA 7.2.6与Confluence 6.0.3的安装与配置之MS SQL Server版

    相关软件版本信息 说明:下方软件可以点击链接,通过百度云盘进行下载. 操作系统:Windows 10(密码:foht)或者Windows Server 2012(密码:lsad): 数据库:SQL S ...

  2. C# -- 二分法查找

    二分法查找:适用于已经排序好的数组 1.二分法查找(入门案例) static void Main(string[] args) { , , , , , , , , , , , , , , , , , ...

  3. ubuntu16.04如何安装多个版本的CUDA

    我的机器是CUDA16.04的,之前装过CUDA10.0,因为一些原因,现在需要安转CUDA9.0. 1.首先https://developer.nvidia.com/cuda-90-download ...

  4. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

  5. C#批量向数据库插入数据

    程序中,批量插入数据有两种思路. 1.用for循环,一条一条的插入,经实测,这种方式太慢了(插入一万条数据至少都需要6-7秒),因为每次插入都要打开数据库连接,执行sql,关闭连接,显然这种方式不可行 ...

  6. Python开发【第三篇】:函数&读写文件

    三元运算 三元运算,是条件语句的简单的写法.如果条件为真,则返回值1,否则,返回值2. ret = 值1 if 条件 else 值2 深浅拷贝 对于数字(int)和字符串(str)而言,赋值.深拷贝. ...

  7. MySQL初识

    1.MySQL版本 社区版:免费的,功能够用. 商业版:更能更加强大,更加稳定,但是收费的. 2.每个版本都分四个版本发布 Alpha版本:一般只在开发公司内部使用,不对外公开,测试.自我检查版本: ...

  8. 【Teradata】日期类型转换

    1.字符串与日期间转换 date '2007-05-10' cast( (curent_timestamp() (format )) //结果为20180615164201 2.毫秒转换为时间戳 / ...

  9. 浅谈javascript的Touch事件

    js的touch事件,一般用于移动端的触屏滑动 代码如下: $(function(){ document.addEventListener("touchmove", _touch, ...

  10. MySQL高级知识(十二)——全局查询日志

    前言:全局查询日志用于保存所有的sql执行记录,该功能主要用于测试环境,在生产环境中永远不要开启该功能. 1.如何开启 #1.通过my.cnf配置开启该功能. 注:对my.cnf文件配置后,需重启my ...