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 <= 100000 <= A[i] <= 90 <= 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> ...
随机推荐
- SpringBoot @Async注解失效分析
有时候在使用的过程中@Async注解会失效(原因和@Transactional注解有时候会失效的原因一样). 下面定义一个Service: 两个异步执行的方法test03()和test02()用来模拟 ...
- Netty 能做什么
作为一个学Java的,如果没有研究过Netty,那么你对Java语言的使用和理解仅仅停留在表面水平,会点SSH,写几个MVC,访问数据库和缓存,这些只是初等Java程序员干的事.如果你要进阶,想了解J ...
- day17 正则表达式 re模块和hashlib模块
今日内容 1. re&正则表达式(*****) 注:不要将自定义文件命名为re import re re.findall(正则表达式,被匹配的字符串) 拿着正则表达式去字符串中找,返回一个列表 ...
- python学习day5 常量 运算符补充 流程控制基础
1.常量 值不会改变的量 python中没有特别的语法定义常量,一般约定用全大写字母命名常量,比如圆周率pi 2.预算符补充 2.1算数运算符 print(10/3)除法运算 print(10//3) ...
- html中相对(relative),绝对(absolute)位置以及float的学习和使用案例 (转)
这几天着手于CSS的研究,研究的原因主要是工作需要,最近发现如果做前端仅仅会javascript很难尽善尽美,当然懂样式和html在一定程度上可以让我们更近一步. css较为简单,由于个人擅长编写代码 ...
- PHP判断是否都是中文
{ } }
- 【收藏】UI自动化测试基本规则与设计模式
总体规则 所有模块设计均遵循page object结构 用例层:测试人员编写测试用例代码的地方,可以调用page层和封装层. page层:一个页面一个类,包含该页面的业务逻辑封装以及部分控件定义. 封 ...
- c# tcp协议发送数据
private void tcp_send(string data)//tcp协议转发数据 { TcpClient tcpClient = new TcpClient(); tcpClient.Con ...
- 转Genymetion
http://www.cnblogs.com/rainboy2010/p/6387770.html 介绍 Genymotion是一款出色的跨平台的Android模拟器,具有容易安装和使用.运行速度快的 ...
- 【环境配置】本地配置sublime text以及和远程linux设置sftp
工具: sublime text 2(mac版) 远程linux(centos 7系) securCRT(for mac) [本地安装并配置securCRT(for mac)] 关于配置: 1.解决终 ...