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. centos mongodb

    cd到mongodb目录下的bin文件夹,执行命令./mongo 运行如下: [root@namenode mongodb]# ./bin/mongo MongoDB shell version: 1 ...

  2. 数据类型&分支流程控制(2)

    1.数据类型 1.数据类型 局部变量:书写在方法中的变量: 全局变量:书写在类中,与方法平级的变量: -如果没有给变量赋初值 -全局变量会默认拥有一个初始值 -局部变量将没有初始值,这个时候不能使用这 ...

  3. Python之-------基础数据类型

    数据类型: 计算可以处理各种不同文件,图形,音频,视频,网页等各种各样的数据,不同的数据,需要定义不同的数据类型.在Python中,能够直接处理的数据类型有以下几种: 一:nubmer(数字) 1.1 ...

  4. ORACLE grant权限

    oracle的系统和对象权限 本文转自: http://hi.baidu.com/zhaojing_boy/blog/item/0ffe95091266d939e824885f.html alter ...

  5. js中的arguments对象

    在Javascript中没有函数重载,而arguments对象弥补了这点不足. js函数的参数是一个数组,在参数个数不固定的情况下,只需要给方法传递不同元素个数的数组即可.即使声明函数时没有形式参数, ...

  6. WPF双向绑定

    需求: 思想批量保存数据. 思路: 看了一下MVVM.发现只需要实现前台和后台数据的同步即可.也就是前台的文本框内容变化时后台的对象的属性也要变化就可以了. 参考: http://www.cnblog ...

  7. JMeter学习(十七)JMeter测试MongoDB(转载)

    转载自 http://www.cnblogs.com/yangxia-test JMeter测试MongoDB性能有两种方式,一种是利用JMeter直接进行测试MongoDB,还有一种是写Java代码 ...

  8. django models实际操作中遇到的一些问题

    问题1.将主键id改成自动生成的python3 manage.py migrate时报下面的错误 django.db.utils.InternalError: (1091, "Can't D ...

  9. spark基本组件与概念

    数据结构 核心之数据集RDD 俗称为弹性分布式数据集.Resilient Distributed Datasets,意为容错的.并行的数据结构,可以让用户显式地将数据存储到磁盘和内存中,并能控制数据的 ...

  10. JavaScript中判断null、undefined与NaN的方法

    1.判断undefined: ? 1 2 3 4 var tmp = undefined; if (typeof(tmp) == "undefined"){ alert(" ...