Add to Array-Form of Integer LT989
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 <= A.length <= 10000
0 <= A[i] <= 9
0 <= K <= 10000
- If
A.length > 1
, thenA[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的更多相关文章
- JavaScript -Array.form方法
Array.from方法可以把一个类数组或者课遍历对象转换为一个正真的数组 语法 Array.from(arrayLike[, mapFn[, thisArg]]) 参数 arrayLike 想要转换 ...
- ES6的新API如Promise,Proxy,Array.form(),Object.assign()等,Babel不能转码, 使用babel-polyfill来解决
Babel默认只转换新的JavaScript句法(syntax),而不转换新的API,比如Iterator.Generator.Set.Maps.Proxy.Reflect.Symbol.Promis ...
- [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. ...
- 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. ...
- 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...
- #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- ...
- 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. ...
- 【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 ...
- java.lang.Integer源码浅析
Integer定义,final不可修改的类 public final class Integer extends Number implements Comparable<Integer> ...
随机推荐
- 1.5.6、CDH 搭建Hadoop在安装之前(定制安装解决方案---使用Cloudera Manager模板创建CDH群集)
使用Cloudera Manager模板创建CDH群集 您可以通过从Cloudera Manager管理的现有CDH群集导出群集模板来创建新的CDH群集.然后,您可以修改模板并使用它在新的主机集上创建 ...
- 下载安装windows版Redis
链接 https://github.com/MicrosoftArchive/redis/releases 选择版本下载 在redis目录打开cmd命令输入 redis-server.ex ...
- vue 中安装使用sass 报错遇到的问题整理
不出错的情况下,正常安装: 1.安装包: npm install node-sass --save-dev npm install sass-loader --save-dev (sass-loade ...
- ffmpeg编码中的二阻塞一延迟
1. avformat_find_stream_info接口延迟 不论是减少预读的数据量,还是设置flag不写缓存,我这边都不实用,前者有风险,后者会丢帧,可能我还没找到好姿势,记录在此,参考:htt ...
- HDU-1257.最少拦截系统(基础DP)
本题大意:给出n和n个整数,让你求出其中不上升子序列的个数. 本题思路:用dp[ i ]保存第i个防御系统攻击的最低的导弹,遍历数组,遇到更低的导弹则更新最小值,否则新加一个系统用来防御,并且更新最小 ...
- mysql 取得各种时间
转载 取得当前日期:DATE_FORMAT(NOW(),'%e'): 取得当前年月:DATE_FORMAT(NOW(),'%Y-%c'):Y:四位.y:两位:m:两位.c:前面不加0: /*当前时间加 ...
- WISH开发API
https://merchant.wish.com/documentation/api#api http://wishquan.com/
- IIS挂起网站配置文件地址
“C/用户/Administrator/我的文档/IISExpress/Config/applicationhost”
- Js学习(1)基本语法
变量: 用var声明变量,如果只是声明变量而不赋值,则变量的值是undefined,表示无定义 不写·var也有效,但不建议 变量声明两次无效,但第二次声明时赋值会覆盖掉前面的值 变量提升: Js引擎 ...
- jq里面关于disable的用法
//两种方法设置disabled属性$('#areaSelect').attr("disabled",true);$('#areaSelect').attr("disab ...