Pergunta de entrevista da empresa Capital One
Given two array of characters, add them together like an addition problem and return them in a char array. ['3', '5', '9'] ['1', 2', '8', '4'] = 1284 + 359 = 1643 = ['1', '6', '4', '3']
Respostas da entrevista
//Addition of two char arrays and store in char array
char[] c1 = {'4', '5', '6'};
char[] c2 = {'7', '8', '9', '1'};
Integer temp = Integer.parseInt(String.valueOf(c1)) + Integer.parseInt(String.valueOf(c2));
char[] sum = temp.toString().toCharArray();
for (char c: sum)
{
System.out.print(c);
}
const addCharArrays = (arr1, arr2) => {
let arr1Int = arr1.join('');
let arr2Int = arr2.join('');
let finalAnswer = parseInt(arr1Int) + parseInt(arr2Int);
return finalAnswer.toString().split('');
}
Use 2 stacks to store the chars and if in the loop you get a exception push a '0' in the stack then pop the length of the maxArr size and convert to int , add , then convert back to char and push into array that you want to return
public int convertToInt(int[] digits){
double sum = 0;
for (int i = digits.length-1; i >= 0 ; i--) {
sum = sum + digits[i] * Math.pow(10, Math.abs(i+1-digits.length));
}
return (int) sum;
}