-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1073.cs
45 lines (37 loc) · 1005 Bytes
/
Solution1073.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1073
{
/// <summary>
/// 1073. Adding Two Negabinary Numbers - Medium
/// <a href="https://leetcode.com/problems/adding-two-negabinary-numbers"</a>
/// </summary>
public int[] AddNegabinary(int[] arr1, int[] arr2)
{
var result = new List<int>();
int carry = 0;
int i = arr1.Length - 1;
int j = arr2.Length - 1;
while (i >= 0 || j >= 0 || carry != 0)
{
if (i >= 0)
{
carry += arr1[i--];
}
if (j >= 0)
{
carry += arr2[j--];
}
result.Add(carry & 1);
carry = -(carry >> 1);
}
// Remove leading zeros
while (result.Count > 1 && result[^1] == 0)
{
result.RemoveAt(result.Count - 1);
}
result.Reverse();
return [.. result];
}
}