2562. Find the Array Concatenation Value

problem

solution

C++ solution

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
class Solution {
public:
int digitLen(int n)
{
int len = 0;
while(n>0){
n/=10;
len++;
}
return len;
}
long long findTheArrayConcVal(vector<int>& nums) {
long long ret = 0 ;
int size = nums.size();
int l = 0, r = size-1;
while(l<=r)
{
if(l==r) ret += nums[l];
// else ret += nums[l] * pow( 10, trunc(log10(nums[r])) + 1) + nums[r] ;
else ret += nums[l] * pow( 10, digitLen(nums[r])) + nums[r] ;
l++;r--;
}
return ret;
}
};

Python solution

1
2
3
4
5
6
7
8
9
10
class Solution:
def findTheArrayConcVal(self, nums: List[int]) -> int:
sums = 0
while nums:
if len(nums) > 1 :
sums += int(str(nums.pop(0)) + str(nums.pop()))
else:
sums += nums.pop()
return sums

analysis

  • time complexity O(n)
  • space complexity O(1)