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. cakephp中sql查询between

    $trading_list = $this->Trading->find('all', array('conditions' => array('buy_time BETWEEN ? ...

  2. [SoapUI] 按照 Test Step Type 获取所有满足条件的 Test Step

    获取当前测试用例下所有Groovy Script类型的测试步骤 def testStepList = testRunner.testCase.getTestStepsOfType(com.eviwar ...

  3. Asp.net相关知识和经验的碎片化记录

    1.解决IIS7.0下“HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求”问题 方案1:在程序的web.config中system.web节点里 ...

  4. Servlet面试题

    Servlet运行在Servlet容器中,其生命周期由容器来管理.Servlet的生命周期通过javax.servlet.Servlet接口中的init().service()和destroy()方法 ...

  5. javascript的那些事儿你都懂了吗

    javascript从开始的验证表单的脚本语言发展到现在能运行在服务器上,其影响力不断的提升.自己作为一个做前端的,编写js是必不可少,从自己学习js的历程来看其实也是比较吃力.要 学好它,还是的花费 ...

  6. sql 两大类 DDL数据定义语言 和DCL数据控制语言

    SQL分为五大类: DDL:数据定义语言   DCL:数据控制语言     DML:数据的操纵语言  DTL:数据事务语言  DQL:数据查询语言. DDL (date definition lang ...

  7. 14、Semantic-UI之菜单样式

    14.1 基础菜单样式   在Semantic-UI中使用class="ui menu". 示例:定义基础菜单样式 <div class="ui menu" ...

  8. [示例] 使用 TStopwatch 计时

    使用 TStopwatch 计时 uses System.Diagnostics; var t1: TStopwatch; begin t1 := TStopwatch.StartNew; // do ...

  9. Robot Framework资料

    https://www.cnblogs.com/pachongshangdexuebi/category/981644.html 虫师 :http://www.cnblogs.com/fnng/   ...

  10. Luckily general gradient for spherical harmonics is defined

    http://web4.cs.ucl.ac.uk/staff/j.kautz/publications/gradientSH_RS04.pdf