Leetcode 3079. Find the Sum of Encrypted Integers
You are given a list of integers. You need to encrypt these integers and then find their sum. The encryption process is defined as follows:
Give the result as an integer.
Clarification: Assume the input values range between standard 32-bit integer limits (-2^31
to 2^31 - 1
).
Clarification: Yes, assume the input list is non-empty.
Clarification: The output should be returned.
#include <iostream>
#include <vector>
#include <cmath>
class Solution {
public:
int reverseDigits(int num) {
int reversedNum = 0;
int sign = num < 0 ? -1 : 1;
num = std::abs(num);
while (num > 0) {
reversedNum = reversedNum * 10 + (num % 10);
num /= 10;
}
return reversedNum * sign;
}
int sumOfEncryptedIntegers(const std::vector<int>& nums) {
int sum = 0;
for (int num : nums) {
sum += reverseDigits(num);
}
return sum;
}
};
int main() {
Solution solution;
std::vector<int> nums = {123, -456, 789};
std::cout << "Sum of Encrypted Integers: " << solution.sumOfEncryptedIntegers(nums) << std::endl;
return 0;
}
reverseDigits
function.The space complexity is (O(1)) for the reverseDigits
process since it uses a fixed amount of extra space regardless of the input size. The overall space complexity is (O(n)) considering the input list.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?