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. background-image使用svg如何改变颜色

    结论 在我多番测试之后,才发现background-image使用svg,改变颜色根本做不了. 分析 当svg图片被使用成background-image,颜色的设置需要在svg内部才能生效.在外部C ...

  2. nodejs websocket

    <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> <script ty ...

  3. jvm运行时内存模式

    jvm内存模型 内存模型粗略划分为:堆和栈 详细划分为:堆,虚拟机栈,方法区,本地方法区,程序计数器 程序计数器: 为了线程切换后能恢复到正确的执行位置,每条线程都需要有一个独立的程序计数器,各条线程 ...

  4. Java序列化由于没有指定serialVersionUID导致报错

    z.JobPersistenceException: Couldn't retrieve job because the BLOB couldn't be deserialized: com.mode ...

  5. P2690 接苹果(暴力搜索+记忆化)

    思路: 建树:就是在每一分钟进行分枝,是原地不动,还是移动.然后,走完整个过程. 但是,我其实还是走了弯路,因为,最开始想的是剪枝,没有用记忆化搜索.但是,肯定是能用dp来做,啊啊啊啊阿,能用dp肯定 ...

  6. P1182 数列分段`Section II`(贪心+二分, 好题)

    这道题让我见识了二分的新姿势.本来,我是二分的位置的. 思路:直接二分答案x, 关键是检验函数的写法: 先用前缀和 a[i....], 看满足多少段满足 a[ j ]-a[ i ]<=x; 的注 ...

  7. Linux Driver 开发 eclipse工程找不到头文件

    如下添加头文件路径, 右键单击工程,选择 Properties  >  C/C++ Build  >  Settings  >  >  GCC C/C++ Compiler  ...

  8. Spring Security(十七):5.8 Method Security

    From version 2.0 onwards Spring Security has improved support substantially for adding security to y ...

  9. Spring Security(一):官网向导翻译

    原文出自  https://spring.io/guides/topicals/spring-security-architecture Spring Security Architecture   ...

  10. oracle(环境搭建二)

    Configuration oracle database Password file(非必要)        cd $ORACLE_HOME/dbs/ ls 查看是否有init.ora 创建密码文件 ...