Skip to content

Commit 1e48136

Browse files
committed
leetcode
1 parent 6bb34c8 commit 1e48136

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
3+
4+
5+
6+
7+
-* 989. Add to Array-Form of Integer *-
8+
9+
10+
The array-form of an integer num is an array representing its digits in left to right order.
11+
12+
For example, for num = 1321, the array form is [1,3,2,1].
13+
Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k.
14+
15+
16+
17+
Example 1:
18+
19+
Input: num = [1,2,0,0], k = 34
20+
Output: [1,2,3,4]
21+
Explanation: 1200 + 34 = 1234
22+
Example 2:
23+
24+
Input: num = [2,7,4], k = 181
25+
Output: [4,5,5]
26+
Explanation: 274 + 181 = 455
27+
Example 3:
28+
29+
Input: num = [2,1,5], k = 806
30+
Output: [1,0,2,1]
31+
Explanation: 215 + 806 = 1021
32+
33+
34+
Constraints:
35+
36+
1 <= num.length <= 104
37+
0 <= num[i] <= 9
38+
num does not contain any leading zeros except for the zero itself.
39+
1 <= k <= 104
40+
41+
42+
43+
44+
*/
45+
46+
import 'dart:collection';
47+
48+
class A {
49+
List<int> addToArrayForm(List<int> num, int k) {
50+
Queue<int> res = Queue();
51+
int carry = 0;
52+
int i = 0;
53+
/*We always start computing from array's last element and k's last digit and will
54+
compute sum and carry. We will iterate it till k and index of array both have existence.
55+
If one of them gets exhausted the for loop below will not work.*/
56+
for (i = num.length - 1; i >= 0 && k > 0; i--) {
57+
int temp = num[i];
58+
res.addFirst((temp + carry + (k % 10)) % 10);
59+
carry = (temp + carry + (k % 10)) ~/ 10;
60+
k ~/= 10;
61+
}
62+
/*If for an instance your k is greater than the number that is present in the form of
63+
array then the below while loop will work.*/
64+
while (k != 0) {
65+
int compute = (k % 10) + carry;
66+
res.addFirst(compute % 10);
67+
carry = compute ~/ 10;
68+
k ~/= 10;
69+
}
70+
/*If for an instance the number that is present in the form of array is greater than k
71+
then the below for loop will work.*/
72+
for (int r = i; r >= 0; r--) {
73+
int temp = num[r];
74+
res.addFirst((temp + carry) % 10);
75+
carry = (temp + carry) ~/ 10;
76+
}
77+
/*If there is some carry still remaining at last then add it to beginning of the
78+
array-list or Queue.*/
79+
if (carry != 0) res.addFirst(carry);
80+
return res.toList();
81+
}
82+
}
83+
84+
class B {
85+
List<int> addToArrayForm(List<int> num, int k) {
86+
Queue<int> res = Queue();
87+
int len = num.length - 1;
88+
while (len >= 0 || k > 0) {
89+
if (len >= 0) {
90+
k += num[len--];
91+
}
92+
res.addFirst(k % 10);
93+
k ~/= 10;
94+
}
95+
return res.toList();
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
func addToArrayForm(num []int, k int) []int {
4+
res := []int{}
5+
size := len(num) - 1
6+
for size >= 0 || k > 0 {
7+
if size >= 0 {
8+
k += num[size]
9+
size--
10+
}
11+
res = append([]int{k % 10}, res...)
12+
k /= 10
13+
}
14+
return res
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# 🔥 2 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Code -1
4+
5+
```dart
6+
import 'dart:collection';
7+
8+
class Solution {
9+
List<int> addToArrayForm(List<int> num, int k) {
10+
Queue<int> res = Queue();
11+
int carry = 0;
12+
int i = 0;
13+
/*
14+
15+
We always start computing from array's last element and k's last digit and will
16+
compute sum and carry. We will iterate it till k and index of array both have existence.
17+
If one of them gets exhausted the for loop below will not work.
18+
19+
*/
20+
for (i = num.length - 1; i >= 0 && k > 0; i--) {
21+
int temp = num[i];
22+
res.addFirst((temp + carry + (k % 10)) % 10);
23+
carry = (temp + carry + (k % 10)) ~/ 10;
24+
k ~/= 10;
25+
}
26+
/*
27+
28+
If for an instance your k is greater than the number that is present in the form of
29+
array then the below while loop will work.
30+
31+
*/
32+
while (k != 0) {
33+
int compute = (k % 10) + carry;
34+
res.addFirst(compute % 10);
35+
carry = compute ~/ 10;
36+
k ~/= 10;
37+
}
38+
/*
39+
40+
If for an instance the number that is present in the form of array is greater than k
41+
then the below for loop will work.
42+
43+
*/
44+
for (int r = i; r >= 0; r--) {
45+
int temp = num[r];
46+
res.addFirst((temp + carry) % 10);
47+
carry = (temp + carry) ~/ 10;
48+
}
49+
/*
50+
51+
If there is some carry still remaining at last then add it to beginning of the
52+
array-list or Queue.
53+
54+
*/
55+
if (carry != 0) res.addFirst(carry);
56+
return res.toList();
57+
}
58+
}
59+
```
60+
61+
## Intuition
62+
63+
We are taking k as carry.
64+
We start from the last or lowest digit in array num add k.
65+
Then update k and move until the highest digit.
66+
After traversing array if carry is > 0 then we add it to beginning of num.
67+
68+
## Approach
69+
70+
Example: `num` = [2,1,5], `k` = 806
71+
At index 2 num = [2, 1, 811]
72+
So, `k` = 81 and `num` = [2, 1, 1]
73+
74+
At index 1 num = [2, 82, 1]
75+
So, `k` = 8 and `num` = [2, 2, 1]
76+
77+
At index 0 num = [10, 2, 1]
78+
So, `k` = 1 and `num` = [0, 2, 1]
79+
80+
Now `k` > 0
81+
So, we add at the beginning of num
82+
`num` = [1, 0, 2, 1]
83+
84+
## Complexity
85+
86+
### Time complexity: O(N)
87+
88+
### Space complexity: O(1)
89+
90+
## Code -2
91+
92+
```dart
93+
class Solution {
94+
List<int> addToArrayForm(List<int> num, int k) {
95+
Queue<int> res = Queue();
96+
int len = num.length - 1;
97+
while (len >= 0 || k > 0) {
98+
if (len >= 0) {
99+
k += num[len--];
100+
}
101+
res.addFirst(k % 10);
102+
k ~/= 10;
103+
}
104+
return res.toList();
105+
}
106+
}
107+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
211211
- [**1129.** Shortest Path with Alternating Colors](ShortestPathWithAlternatingColors/shortest_path_with_alternating_colors.dart)
212212
- [**2477.** Minimum Fuel Cost to Report to the Capital](MinimumFuelCostToReportToTheCapital/minimum_fuel_cost_to_report_to_the_capital.dart)
213213
- [**1523.** Count Odd Numbers in an Interval Range](CountOddNumbersInAnIntervalRange/count_odd_numbers_in_an_interval_range.dart)
214+
- [**989.** Add to Array-Form of Integer](AddToArrayFormOfInteger/add_to_array_form_of_integer.dart)
214215

215216
## Reach me via
216217

0 commit comments

Comments
 (0)