Skip to content

Commit 6af8812

Browse files
authored
Sri Hari: Batch-4/Neetcode-All/Added-articles (neetcode-gh#3775)
* Batch-4/Neetcode-All/Added-articles * Batch-4/Neetcode-All/Added-articles * Batch-4/Neetcode-All/Added-articles
1 parent 2327992 commit 6af8812

18 files changed

+8283
-9
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
## 1. Reverse and Add
2+
3+
::tabs-start
4+
5+
```python
6+
class Solution:
7+
def addToArrayForm(self, num: List[int], k: int) -> List[int]:
8+
num.reverse()
9+
i = 0
10+
while k:
11+
digit = k % 10
12+
if i < len(num):
13+
num[i] += digit
14+
else:
15+
num.append(digit)
16+
carry = num[i] // 10
17+
num[i] %= 10
18+
k //= 10
19+
k += carry
20+
i += 1
21+
num.reverse()
22+
return num
23+
```
24+
25+
```java
26+
public class Solution {
27+
public List<Integer> addToArrayForm(int[] num, int k) {
28+
List<Integer> result = new ArrayList<>();
29+
for (int i = num.length - 1; i >= 0; i--) {
30+
k += num[i];
31+
result.add(k % 10);
32+
k /= 10;
33+
}
34+
while (k > 0) {
35+
result.add(k % 10);
36+
k /= 10;
37+
}
38+
Collections.reverse(result);
39+
return result;
40+
}
41+
}
42+
```
43+
44+
```cpp
45+
class Solution {
46+
public:
47+
vector<int> addToArrayForm(vector<int>& num, int k) {
48+
reverse(num.begin(), num.end());
49+
int i = 0;
50+
while (k) {
51+
int digit = k % 10;
52+
if (i < num.size()) {
53+
num[i] += digit;
54+
} else {
55+
num.push_back(digit);
56+
}
57+
int carry = num[i] / 10;
58+
num[i] %= 10;
59+
k /= 10;
60+
k += carry;
61+
i++;
62+
}
63+
reverse(num.begin(), num.end());
64+
return num;
65+
}
66+
};
67+
```
68+
69+
```javascript
70+
class Solution {
71+
/**
72+
* @param {number[]} num
73+
* @param {number} k
74+
* @return {number[]}
75+
*/
76+
addToArrayForm(num, k) {
77+
num.reverse();
78+
let i = 0;
79+
while (k > 0) {
80+
const digit = k % 10;
81+
if (i < num.length) {
82+
num[i] += digit;
83+
} else {
84+
num.push(digit);
85+
}
86+
const carry = Math.floor(num[i] / 10);
87+
num[i] %= 10;
88+
k = Math.floor(k / 10) + carry;
89+
i++;
90+
}
91+
num.reverse();
92+
return num;
93+
}
94+
}
95+
```
96+
97+
::tabs-end
98+
99+
### Time & Space Complexity
100+
101+
* Time complexity: $O(max(n, m))$
102+
* Space complexity: $O(n)$.
103+
104+
> Where $n$ is the size of the array $num$ and $m$ is the number of digits in $k$.
105+
106+
---
107+
108+
## 2. Without Reverse()
109+
110+
::tabs-start
111+
112+
```python
113+
class Solution:
114+
def addToArrayForm(self, num: List[int], k: int) -> List[int]:
115+
from collections import deque
116+
result = deque()
117+
i = len(num) - 1
118+
carry = 0
119+
120+
while i >= 0 or k > 0 or carry > 0:
121+
digit = k % 10
122+
sum_val = carry + (num[i] if i >= 0 else 0) + digit
123+
124+
result.appendleft(sum_val % 10)
125+
carry = sum_val // 10
126+
127+
k //= 10
128+
i -= 1
129+
130+
return list(result)
131+
```
132+
133+
```java
134+
public class Solution {
135+
public List<Integer> addToArrayForm(int[] num, int k) {
136+
LinkedList<Integer> result = new LinkedList<>();
137+
int carry = 0, i = num.length - 1;
138+
139+
while (i >= 0 || k > 0 || carry > 0) {
140+
int digit = k % 10;
141+
int sum = carry + (i >= 0 ? num[i] : 0) + digit;
142+
143+
result.addFirst(sum % 10);
144+
carry = sum / 10;
145+
146+
k /= 10;
147+
i--;
148+
}
149+
150+
return result;
151+
}
152+
}
153+
```
154+
155+
```cpp
156+
class Solution {
157+
public:
158+
vector<int> addToArrayForm(vector<int>& num, int k) {
159+
list<int> result;
160+
int carry = 0, i = num.size() - 1;
161+
162+
while (i >= 0 || k > 0 || carry > 0) {
163+
int digit = k % 10;
164+
int sum = carry + (i >= 0 ? num[i] : 0) + digit;
165+
166+
result.push_front(sum % 10);
167+
carry = sum / 10;
168+
169+
k /= 10;
170+
i--;
171+
}
172+
173+
return vector<int>(result.begin(), result.end());
174+
}
175+
};
176+
```
177+
178+
```javascript
179+
class Solution {
180+
/**
181+
* @param {number[]} num
182+
* @param {number} k
183+
* @return {number[]}
184+
*/
185+
addToArrayForm(num, k) {
186+
let res = new Deque();
187+
let carry = 0, i = num.length - 1;
188+
189+
while (i >= 0 || k > 0 || carry > 0) {
190+
const digit = k % 10;
191+
const sum = carry + (i >= 0 ? num[i] : 0) + digit;
192+
193+
res.pushFront(sum % 10);
194+
carry = Math.floor(sum / 10);
195+
196+
k = Math.floor(k / 10);
197+
i--;
198+
}
199+
200+
const resultArray = [];
201+
while (!res.isEmpty()) {
202+
resultArray.push(res.popFront());
203+
}
204+
205+
return resultArray;
206+
}
207+
}
208+
```
209+
210+
::tabs-end
211+
212+
### Time & Space Complexity
213+
214+
* Time complexity: $O(max(n, m))$
215+
* Space complexity: $O(n)$
216+
217+
> Where $n$ is the size of the array $num$ and $m$ is the number of digits in $k$.

0 commit comments

Comments
 (0)