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
Runtime: 136 ms, faster than 100.00% of C++ online submissions for Add to Array-Form of Integer.
Memory Usage: 11.9 MB, less than 100.00% of C++ online submissions for Add to Array-Form of Integer.
class Solution {
public:
vector<int> addToArrayForm(vector<int>& A, int K) {
vector<int> ka = itoarray(K);
for(int a : ka) cout << a << endl;
reverse(A.begin(), A.end());
vector<int> ret;
int endi = max(A.size(), ka.size());
int add1, add2, sleft, sj;
sj = ;
for(int i=; i<endi; i++) {
add1 = add2 = sleft = ;
if(i < A.size()) add1 = A[i];
if(i < ka.size()) add2 = ka[i];
sleft = sj + add1 + add2;
if(sleft >= ) {sj = ; sleft %= ;}
else sj = ;
ret.push_back(sleft);
}
if(sj == ) ret.push_back();
reverse(ret.begin(), ret.end());
return ret;
}
vector<int> itoarray(int K) {
vector<int> a;
while(K != ) {
a.push_back(K % );
K /= ;
}
return a;
} };

LC 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

    https://leetcode.com/problems/add-to-array-form-of-integer/ For a non-negative integer X, the array- ...

  3. 【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; 完

  4. 【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 ...

  5. JavaScript -Array.form方法

    Array.from方法可以把一个类数组或者课遍历对象转换为一个正真的数组 语法 Array.from(arrayLike[, mapFn[, thisArg]]) 参数 arrayLike 想要转换 ...

  6. ES6的新API如Promise,Proxy,Array.form(),Object.assign()等,Babel不能转码, 使用babel-polyfill来解决

    Babel默认只转换新的JavaScript句法(syntax),而不转换新的API,比如Iterator.Generator.Set.Maps.Proxy.Reflect.Symbol.Promis ...

  7. LC 384. Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  8. [LC] 1055. Shortest Way to Form String

    From any string, we can form a subsequence of that string by deleting some number of characters (pos ...

  9. [LC] 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

随机推荐

  1. Win10 hosts文件无法保存

    Win10无法修改编辑保存hosts文件怎么办?Win10系统默认是没有权限去编辑保存系统里的文件,这也是权限不够才导致修改编辑hosts后无法保存的原因,解决的办法就是把自己的帐户权限给提高就行了. ...

  2. android:duplicateParentState属性使用场景

    对于这个属性的使用也是在偶然的时候发现的,之前从未使用它,所以有必要阐述一下它的用法,什么场景会要用它这个属性,在我不知道之前这个属性之前,也同样能实现效果,但是当我知道它的存在之后,我肯定在某种场景 ...

  3. SQLSERVER视图错位的解决办法

    原始需求如下: 有一个表T1 create table t1 (id int not null primary key ,v1 ) ) ,'aaa'); ,'bbb'); 有一个表TS,用于记录T1中 ...

  4. python模拟双色球大乐透生成算法

    每天练习一段python代码,健康生活一辈子.晚上下班没事,打开电脑继续编写python代码!今天分享的一个是大家熟悉的双色球彩票的游戏,根据这个进行写的一个python算法,代码精简,肯定有bug, ...

  5. java中使用redis --- List列表的简单应用

    1.Dos中启动server端 2.idea中启动client端 public class RedisTest01 { public static void main(String[] args){ ...

  6. js对iframe内外(父子)页面进行操作

    dom对象推荐阅读 怎么对iframe进行操作,1.在iframe里面控制iframe外面的js代码.2.在父框架对子iframe进行操作. 获取iframe里的内容 主要的两个API就是conten ...

  7. win10 注册DLL

    昨天用c++写了一个ocx插件,注册就死活注册不上,折腾了半天1.打开C:\Windows\SysWOW64 文件夹 找到cmd  右键管理员运行 2.将你的插件或者dll放到此目录下3.regsvr ...

  8. Django中出现:TemplateDoesNotExist at

    setting文件中加入:

  9. Z+F激光扫描仪

    链接:https://zhuanlan.zhihu.com/p/48589754 三维扫描仪有三个误差来源: ● 线性误差(激光雷达部分/LARA) ● 测距噪声(激光雷达部分/LARA) ● 测角误 ...

  10. WebService操作

    webservice是一种服务器通信技术,封装了socket.