返回介绍

solution / 2500-2599 / 2562.Find the Array Concatenation Value / README_EN

发布于 2024-06-17 01:03:04 字数 7363 浏览 0 评论 0 收藏 0

2562. Find the Array Concatenation Value

中文文档

Description

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_.

 

Example 1:

Input: nums = [7,52,2,4]
Output: 596
Explanation: Before performing any operation, nums is [7,52,2,4] and concatenation value is 0.
 - In the first operation:
We pick the first element, 7, and the last element, 4.
Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74.
Then we delete them from nums, so nums becomes equal to [52,2].
 - In the second operation:
We pick the first element, 52, and the last element, 2.
Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596.
Then we delete them from the nums, so nums becomes empty.
Since the concatenation value is 596 so the answer is 596.

Example 2:

Input: nums = [5,14,13,8,12]
Output: 673
Explanation: Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0.
 - In the first operation:
We pick the first element, 5, and the last element, 12.
Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512.
Then we delete them from the nums, so nums becomes equal to [14,13,8].
 - In the second operation:
We pick the first element, 14, and the last element, 8.
Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660.
Then we delete them from the nums, so nums becomes equal to [13].
 - In the third operation:
nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673.
Then we delete it from nums, so nums become empty.
Since the concatenation value is 673 so the answer is 673.

 

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 104

 

Solutions

Solution 1: Simulation

Starting from both ends of the array, we take out one element at a time, concatenate it with another element, and then add the concatenated result to the answer. We repeat this process until the array is empty.

The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Here, $n$ and $M$ are the length of the array and the maximum value in the array, respectively.

class Solution:
  def findTheArrayConcVal(self, nums: List[int]) -> int:
    ans = 0
    i, j = 0, len(nums) - 1
    while i < j:
      ans += int(str(nums[i]) + str(nums[j]))
      i, j = i + 1, j - 1
    if i == j:
      ans += nums[i]
    return ans
class Solution {
  public long findTheArrayConcVal(int[] nums) {
    long ans = 0;
    int i = 0, j = nums.length - 1;
    for (; i < j; ++i, --j) {
      ans += Integer.parseInt(nums[i] + "" + nums[j]);
    }
    if (i == j) {
      ans += nums[i];
    }
    return ans;
  }
}
class Solution {
public:
  long long findTheArrayConcVal(vector<int>& nums) {
    long long ans = 0;
    int i = 0, j = nums.size() - 1;
    for (; i < j; ++i, --j) {
      ans += stoi(to_string(nums[i]) + to_string(nums[j]));
    }
    if (i == j) {
      ans += nums[i];
    }
    return ans;
  }
};
func findTheArrayConcVal(nums []int) (ans int64) {
  i, j := 0, len(nums)-1
  for ; i < j; i, j = i+1, j-1 {
    x, _ := strconv.Atoi(strconv.Itoa(nums[i]) + strconv.Itoa(nums[j]))
    ans += int64(x)
  }
  if i == j {
    ans += int64(nums[i])
  }
  return
}
function findTheArrayConcVal(nums: number[]): number {
  const n = nums.length;
  let ans = 0;
  let i = 0;
  let j = n - 1;
  while (i < j) {
    ans += Number(`${nums[i]}${nums[j]}`);
    i++;
    j--;
  }
  if (i === j) {
    ans += nums[i];
  }
  return ans;
}
impl Solution {
  pub fn find_the_array_conc_val(nums: Vec<i32>) -> i64 {
    let n = nums.len();
    let mut ans = 0;
    let mut i = 0;
    let mut j = n - 1;
    while i < j {
      ans += format!("{}{}", nums[i], nums[j]).parse::<i64>().unwrap();
      i += 1;
      j -= 1;
    }
    if i == j {
      ans += nums[i] as i64;
    }
    ans
  }
}
int getLen(int num) {
  int res = 0;
  while (num) {
    num /= 10;
    res++;
  }
  return res;
}

long long findTheArrayConcVal(int* nums, int numsSize) {
  long long ans = 0;
  int i = 0;
  int j = numsSize - 1;
  while (i < j) {
    ans += nums[i] * pow(10, getLen(nums[j])) + nums[j];
    i++;
    j--;
  }
  if (i == j) {
    ans += nums[i];
  }
  return ans;
}

Solution 2

impl Solution {
  pub fn find_the_array_conc_val(nums: Vec<i32>) -> i64 {
    let mut ans = 0;
    let mut n = nums.len();

    for i in 0..n / 2 {
      ans += format!("{}{}", nums[i], nums[n - i - 1])
        .parse::<i64>()
        .unwrap();
    }

    if n % 2 != 0 {
      ans += nums[n / 2] as i64;
    }

    ans
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文