{"id":9936,"date":"2023-02-12T08:03:16","date_gmt":"2023-02-12T16:03:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9936"},"modified":"2023-02-12T08:03:41","modified_gmt":"2023-02-12T16:03:41","slug":"leetcode-2562-find-the-array-concatenation-value","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2562-find-the-array-concatenation-value\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2562.\u00a0Find the Array Concatenation Value"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>concatenation<\/strong>&nbsp;of two numbers is the number formed by concatenating their numerals.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, the concatenation of&nbsp;<code>15<\/code>,&nbsp;<code>49<\/code>&nbsp;is&nbsp;<code>1549<\/code>.<\/li><\/ul>\n\n\n\n<p>The&nbsp;<strong>concatenation value<\/strong>&nbsp;of&nbsp;<code>nums<\/code>&nbsp;is initially equal to&nbsp;<code>0<\/code>. Perform this operation until&nbsp;<code>nums<\/code>&nbsp;becomes empty:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If there exists more than one number in&nbsp;<code>nums<\/code>, pick the first element and last element in&nbsp;<code>nums<\/code>&nbsp;respectively and add the value of their concatenation to the&nbsp;<strong>concatenation value<\/strong>&nbsp;of&nbsp;<code>nums<\/code>, then delete the first and last element from&nbsp;<code>nums<\/code>.<\/li><li>If one element exists, add its value to the&nbsp;<strong>concatenation value<\/strong>&nbsp;of&nbsp;<code>nums<\/code>, then delete it.<\/li><\/ul>\n\n\n\n<p>Return<em>&nbsp;the concatenation value of the&nbsp;<code>nums<\/code><\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [7,52,2,4]\n<strong>Output:<\/strong> 596\n<strong>Explanation:<\/strong> Before performing any operation, nums is [7,52,2,4] and concatenation value is 0.\n - In the first operation:\nWe pick the first element, 7, and the last element, 4.\nTheir concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74.\nThen we delete them from nums, so nums becomes equal to [52,2].\n - In the second operation:\nWe pick the first element, 52, and the last element, 2.\nTheir concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596.\nThen we delete them from the nums, so nums becomes empty.\nSince the concatenation value is 596 so the answer is 596.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [5,14,13,8,12]\n<strong>Output:<\/strong> 673\n<strong>Explanation:<\/strong> Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0.\n - In the first operation:\nWe pick the first element, 5, and the last element, 12.\nTheir concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512.\nThen we delete them from the nums, so nums becomes equal to [14,13,8].\n - In the second operation:\nWe pick the first element, 14, and the last element, 8.\nTheir concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660.\nThen we delete them from the nums, so nums becomes equal to [13].\n - In the third operation:\nnums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673.\nThen we delete it from nums, so nums become empty.\nSince the concatenation value is 673 so the answer is 673.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Follow the rules<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(sum(log(nums[i]))<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  long long findTheArrayConcVal(vector<int>& nums) {\n    long long ans = 0;\n    const int n = nums.size();\n    for (int i = 0; i < n \/ 2; ++i) {\n      int cur  = nums[i];\n      for (int t = nums[n - i - 1]; t > 0; t \/= 10)\n        cur *= 10;    \n      cur += nums[n - i - 1];\n      ans += cur;\n    }          \n    if (n & 1) ans += nums[n \/ 2];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums. The&nbsp;concatenation&nbsp;of two numbers is the number formed by concatenating their numerals. For example, the concatenation of&nbsp;15,&nbsp;49&nbsp;is&nbsp;1549. The&nbsp;concatenation value&nbsp;of&nbsp;nums&nbsp;is initially equal&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,669,222],"class_list":["post-9936","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-concatenation","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9936"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9936\/revisions"}],"predecessor-version":[{"id":9938,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9936\/revisions\/9938"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}