[LeetCode] Find the Array Concatenation Value

2562. Find the Array Concatenation Value

You are given a 0-indexed integer array nums.

The concatenation of two numbers is the number formed by concatenating their numerals.

  • For example, the concatenation of 15, 49 is 1549.

The concatenation value of nums is initially equal to 0. Perform this operation until nums becomes empty:

  • If there exists more than one number in nums, pick the first element and last element in nums respectively and add the value of their concatenation to the concatenation value of nums, then delete the first and last element from nums.
  • If one element exists, add its value to the concatenation value of nums, then delete it.

Return the concatenation value of the nums.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
long long findTheArrayConcVal(vector<int>& nums) {
long long res = 0;
int l = 0, r = nums.size() - 1;
while(l <= r) {
if(l == r) {
res += nums[l];
} else {
res += stoll(to_string(nums[l]) + to_string(nums[r]));
}
l += 1, r -= 1;
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/02/12/PS/LeetCode/find-the-array-concatenation-value/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.