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. Vue 学习笔记之快速入门篇

    Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手,还便于与 ...

  2. 设置SSH免密码登录

    1.cd .ssh 2.执行下面的命令,三次回车. ssh-keygen -t rsa cat id_rsa.pub >> authorized_keys 3.发送公钥 scp .ssh/ ...

  3. 【算法】LeetCode算法题-Search Insert Position

    这是悦乐书的第152次更新,第154篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第11题(顺位题号是35).给定排序数组和目标值,如果找到目标,则返回索引. 如果没有, ...

  4. March 10th, 2018 Week 10th Saturday

    All good things must come to an end. 好景无常. Love is when the other person's happiness is more importa ...

  5. 假设在本地搭一个server和mysql数据库环境,假设使用java来訪问数据库

    我们能够使用speedamp来搭一个server环境,能够在http://download.csdn.net/detail/baidu_nod/7630265下载 解压后无需安装直接能够使用.点击Sp ...

  6. Error response from daemon: --cluster-store and --cluster-advertise daemon configurations are incompatible with swarm mode

    将docker worker node加入swarm集群时,出现以下错误 1.试验环境: centos7 2.报错翻译:--cluster-store和--cluster-advertise后台配置与 ...

  7. 【css】IE盒子模型和标准W3C盒子模型

    其实盒子模型有两种,分别是 IE 盒子模型和标准 W3C 盒子模型. 1.标准盒子 从上图可以看到标准 W3C 盒子模型的范围包括 margin.border.padding.content,并且 c ...

  8. day24 Pythonpython 面向对象设计 类

    将一些相同特征和动作的成为类,现有类才能创建对象,对象就是特征和动作的结合体 类:把一类事物的相同特征和动作整合到一起就是类.类是一个抽象概念 对象:就是基于类而创建的一个具的事物(具体存在的),也是 ...

  9. NOIP2018初赛游记

    NOIP2018初赛游记 (编辑中)

  10. vue 2.0多页面开发

    1.为项目添加多个入口 找到\build\webpack.base.conf.js文件: module.exports = { //..., //vue的多页面开发:应用程序可以存在多个入口 entr ...