首页 > 其他分享 >leetcode-989-easy

leetcode-989-easy

时间:2022-10-30 16:23:51浏览次数:51  
标签:10 989 int list num easy carry array leetcode

Add to Array-Form of Integer

The array-form of an integer num is an array representing its digits in left to right order.

For example, for num = 1321, the array form is [1,3,2,1].
Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k.

 

Example 1:

Input: num = [1,2,0,0], k = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234
Example 2:

Input: num = [2,7,4], k = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455
Example 3:

Input: num = [2,1,5], k = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021
 

Constraints:

1 <= num.length <= 104
0 <= num[i] <= 9
num does not contain any leading zeros except for the zero itself.
1 <= k <= 104

思路一:分解整数为 list,然后模拟加法运算。代码写的有点丑,看了一下题解,感觉没必要把整数分解,直接做加法模拟,然后把结果变成 list 这样的思路代码会简洁很多

public List<Integer> addToArrayForm(int[] num, int k) {
	List<Integer> list = new ArrayList<>();

	while (k > 0) {
		list.add(k % 10);
		k /= 10;
	}

	int i = num.length - 1;
	int j = 0;
	int carry = 0;
	while (i >= 0 || j < list.size()) {
		int left = 0;
		if (i >= 0) {
			left = num[i];
		}

		int right = 0;
		if (j < list.size()) {
			right = list.get(j);
		}

		int val = left + right + carry;
		carry = val >= 10 ? 1 : 0;
		if (j < list.size()) {
			list.set(j, val % 10);
		} else {
			list.add(val % 10);
		}

		i--;
		j++;
	}
	if (carry == 1) list.add(carry);

	List<Integer> result = new ArrayList<>();
	for (int x = list.size() - 1; x >= 0; x--) {
		result.add(list.get(x));
	}

	return result;
}

标签:10,989,int,list,num,easy,carry,array,leetcode
From: https://www.cnblogs.com/iyiluo/p/16841531.html

相关文章

  • leetcode 第90场双周赛
    6226.摧毁一系列目标题意:对于数组中每一个数nums[i],可以摧毁数组中值等于nums[i]+c*space的数(c为非负整数),求摧毁最大数量时的最小nums[i]思路:如果两个数x,y可以同时被摧......
  • leetcode103-二叉树的锯齿形层序遍历
    103.二叉树的锯齿形层序遍历用两个栈来实现。先把根节点放入第一个栈。循环内部根据哪个栈为空判断新的节点放到哪个栈,确定先左还是先右。当两个栈都为空时,循环结束。......
  • LeetCode 2458. Height of Binary Tree After Subtree Removal Queries
    原题链接在这里:https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/题目:Youaregiventhe root ofa binarytree with n node......
  • leetcode-1790-easy
    CheckifOneStringSwapCanMakeStringsEqualYouaregiventwostringss1ands2ofequallength.Astringswapisanoperationwhereyouchoosetwoindices......
  • leetcode-2160-easy
    MinimumSumofFourDigitNumberAfterSplittingDigitsYouaregivenapositiveintegernumconsistingofexactlyfourdigits.Splitnumintotwonewintegers......
  • leetcode-1748-easy
    SumofUniqueElementsYouaregivenanintegerarraynums.Theuniqueelementsofanarrayaretheelementsthatappearexactlyonceinthearray.Returnthe......
  • leetcode102-二叉树的层序遍历
    102.二叉树的层序遍历有两种实现方法。第一种是递归,第二种是队列实现。第一种是看了别人的代码写出来的,第二种是自己写的。这道题的不能直接把遍历得到的数字直接塞进res......
  • EasyExcel导出Date类型格式处理
    EasyExcel导出Date类型格式处理​ 如果在导出的excel中有date时间类型的字段,直接导出会报错org.apache.poi.ss.usermodel.Cell.setCellValue(Ljava/time/LocalDateTime;)......
  • 刷题 LeetCode二叉树2
    代码随想录LeetCode102.二叉树的层序遍历carl二叉树遍历#层序遍历#队列#广度优先思路队首是待访问节点,每访问一个节点,将其左子和右子加入队列细节如何知道某......
  • LeetCode 题解 | 1. 两数之和 Javascript 版
    题目给定一个整数数组nums 和一个整数目标值target,请你在该数组中找出和为目标值target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个......