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

Idea 1: 大数的相加

Time complexity: O(max(n, log(K))), n = A.length

Space complexity: O(max(n, log(K)))

 class Solution {
public List<Integer> addToArrayForm(int[] A, int K) {
List<Integer> result = new ArrayList<>();
int carry = 0; for(int left = A.length-1, curr = K; left >= 0 || curr != 0 || carry != 0; --left) {
int val1 = left >= 0? A[left] : 0;
int val2 = curr%10;
int sum = val1 + val2 + carry;
result.add(sum%10);
carry = sum/10;
curr = curr/10;
} Collections.reverse(result);
return result;
}
}

Idea 1.a 网上的解法,代码更简洁,少用carry这个变量,直接用K加数组里的每个数字

Note. 如果K是Integer.MAX_VALUE, 这样加会overflow

A= {1, 2, 9, 9}, K = 99

 class Solution {
public List<Integer> addToArrayForm(int[] A, int K) {
List<Integer> result = new ArrayList<>();
int carry = 0; for(int left = A.length-1, curr = K; left >= 0 || curr != 0 ; --left) {
if(left >= 0) {
curr += A[left];
} result.add(curr%10);
curr = curr/10;
} Collections.reverse(result);
return result;
}
}

Add to Array-Form of Integer LT989的更多相关文章

  1. JavaScript -Array.form方法

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

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

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

  3. [Swift]LeetCode989. 数组形式的整数加法 | 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. 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.  ...

  5. 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...

  6. #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- ...

  7. 123th LeetCode Weekly Contest 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.  ...

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

  9. java.lang.Integer源码浅析

    Integer定义,final不可修改的类 public final class Integer extends Number implements Comparable<Integer> ...

随机推荐

  1. CentOS7下解决yum install mysql-server没有可用包

    # wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm --  http://repo.mysql.com/mysq ...

  2. python-ceilometerclient命令行(1)

    1.导入模块,可以动态获取模块中方法并调用,其功能与from...import...一致 2. callback = getattr(actions_module, attr) 从模块中获取方法. 调 ...

  3. day14 迭代器和生成器

    1.迭代器 名词解释 什么是迭代:迭代是一个重复过程,但是每次重复都是基于上一次的结果而继续的 #下列循环只是单纯的重复,没有意义 while True: print(1) #基于索引的迭代取值 l ...

  4. js 判断整数

    参考 https://blog.csdn.net/tangxiujiang/article/details/78073792 1.使用取余运算符(%) + 判断对象是否是数字来判断: 注意:空字符串. ...

  5. NumPy 副本和视图

    NumPy 副本和视图 副本是一个数据的完整的拷贝,如果我们对副本进行修改,它不会影响到原始数据,物理内存不在同一位置. 视图是数据的一个别称或引用,通过该别称或引用亦便可访问.操作原有数据,但原有数 ...

  6. 思维+并查集 hdu5652

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意: 输入T,接下来T个样例,每个样例输入n,m代表图的大小,接下来n行,每行m个数,代表图, ...

  7. 使用phpStudyy运行tipask

    tipask官网:https://www.tipask.com/tipask源码下载:https://www.tipask.com/download.html 可参考此处安装文档的链接 除此之外可以参 ...

  8. jggrid应用,后台c#

    参考网址: 1.https://www.cnblogs.com/miro/p/jqGrid.html 2.https://blog.csdn.net/ainuser/article/details/6 ...

  9. RxJS之组合操作符 ( Angular环境 )

    一 merge操作符 把多个 Observables 的值混合到一个 Observable 中 import { Component, OnInit } from '@angular/core'; i ...

  10. Qt的pro文件--项目配置的部分字段

    Qt项目配置的部分字段: 库: LIBS += -L /usr/local/lib -lpcap INCLUDEPATH += /usr/local/include/