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. Python 渗透测试编程技术方法与实践 ------全书整理

    1.整个渗透测试的工作阶段 ( 1 )前期与客户的交流阶段.( 2 )情报的收集阶段.( 3 )威胁建模阶段.( 4 )漏洞分析阶段.( 5 )漏洞利用阶段.( 6 )后渗透攻击阶段.( 7 )报告阶 ...

  2. Bash基础——命令替换

    参考:Linux 下Shell 脚本几种基本命令替换区别 Command substitution 命令替换Command substitution https://www.jb51.net/arti ...

  3. Linux命令——source

    参考:What does 'source' do? 前言 当我们修改了/etc/profile文件,并想让它立刻生效,而不用重新登录,就可以使用source命令,如source /etc/profil ...

  4. Make 和 Makefile快速入门

    前言 一个项目,拥有成百上千的源程序文件,编译链接这些源文件都是有规则的.Makefile是整个工程的编译规则集合,只需要一个make命令,就可以实现“自动化编译”.make是一个解释makefile ...

  5. TCP的三次握手和四次挥手与路由器(三层)转发原理

    传输层是国际标准化组织提出的开放系统互连(OSI)参考模型中的第四层.该层协议为网络端点主机上的进程之间提供了可靠.有效的报文传送服务.其功能紧密地依赖于网络层的虚拟电路或数据报服务.传输层定义了主机 ...

  6. 【新品发布】智能驾驶实车测试系统-VDAS

    智能驾驶技术的迭代研发,需要多种传感器.海量数据.海量场景的支撑.而目前多种传感器Gbit/s级别的数据同步采集.海量数据的快速分析和评估.关键场景的切片和提取,是业界公认的棘手问题. 为了解决上述的 ...

  7. spring框架中的一个字符串的工具类

    stringutils.hasText("字符串") 如果字符串里面的值为null, "", "   ",那么返回值为false:否则为tr ...

  8. Django中的Session与Cookie

    1.相同与不同 Cookie和Session都是为了记录用户相关信息的方式, 最大的区别就是Cookie在客户端记录而Session在服务端记录内容. 2.Cookie和Session之间的联系的建立 ...

  9. js中的数据类型以及转换

    Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...

  10. sparkStreaming 读kafka的数据

    目标:sparkStreaming每2s中读取一次kafka中的数据,进行单词计数. topic:topic1 broker list:192.168.1.126:9092,192.168.1.127 ...