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 len = A.size();
int num=;
vector<int>Ve;
int a[]={},b[]={},c[]={};
for(int i=len-;i>=;i--){
a[num++]=A[i];
//cout<<A[i]<<endl;
}
int cnt=;
int i;
while(K){
b[cnt++]=K%;
K/=;
}
//cout<<"B"<<endl;
int k=max(len,cnt); for(i=;i<k;i++)
{
//cout<<a[i]<<" "<<b[i]<<endl;
c[i]=a[i]+b[i];
}
for(i=; i<k; i++){
//cout<<"A1"<<endl;
if(c[i]>=){
c[i+]+=c[i]/;
c[i]%=;
}
//cout<<"A2"<<endl;
} if(c[k]==) k--;
for(i=k;i>=;i--){
Ve.push_back(c[i]);
} return Ve; }
};

123th LeetCode Weekly Contest Add to Array-Form of Integer的更多相关文章

  1. 123th LeetCode Weekly Contest Broken Calculator

    On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...

  2. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  5. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  6. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  7. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  8. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  9. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. [SoapUI] UrlEncode编码/UrlDecode解码网站

    http://tool.chinaz.com/Tools/URLEncode.aspx 解码: 编码:

  2. source insight 保存时删除多余空格,去除多余空格 space tab键

    source insight 保存时删除多余空格,去除多余空格 space tab键 摘自:https://blog.csdn.net/lanmanck/article/details/8638391 ...

  3. 手工创建Oracle数据库

    数据库版本: SQL> select * from v$version; BANNER ----------------------------------------------------- ...

  4. spring加载ApplicationContext.xml的四种方式

    spring 中加载xml配置文件的方式,好像有4种, xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory , C ...

  5. scala初学笔记

    tips: 1.函数的定义: def addOne(m: Int): Int = m + 1 m为参数,要指定其类型,Int: 后边跟着返回值的类型,Int= 后边是函数的内部 2.匿名函数: (x: ...

  6. 项目中遇到的死锁问题: Lock wait timeout exceeded; try restarting transaction

    最近项目中频繁出现  Lock wait timeout exceeded; try restarting transaction这个错误,把我们弄得痛苦不堪啊,为了解决问题,上网上找好多资料,终于把 ...

  7. C# GDI+绘图介绍

    最近查阅网上资料,将GDI+的基本知识汇总如下: 一.基本的知识 GDI+:Graphics Device Interface Plus也就是图形设备接口,提供了各种丰富的图形图像处理功能; 在C#. ...

  8. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(10):通过注解(annotation)装配Bean之(@Configguration、@Component、@Value、@ComponentScan、@Autowired、@Primary、@Qualifier、@Bean)

    一.通过注解(annotation)装配Bean 通过之前的学习,我们已经知道如何使用XML装配Bean,但是更多的时候已经不再推荐使用XML的方式去装配Bean,更多的时候会考虑注解(annotat ...

  9. Gym 100989L (DFS)

    AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...

  10. CodeForces 343D water tree(树链剖分)

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...