|
| 1 | +<h2><a href="https://leetcode.com/problems/find-the-array-concatenation-value">2698. Find the Array Concatenation Value</a></h2><h3>Easy</h3><hr><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>The <strong>concatenation</strong> of two numbers is the number formed by concatenating their numerals.</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>For example, the concatenation of <code>15</code>, <code>49</code> is <code>1549</code>.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>The <strong>concatenation value</strong> of <code>nums</code> is initially equal to <code>0</code>. Perform this operation until <code>nums</code> becomes empty:</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li>If <code>nums</code> has a size greater than one, add the value of the concatenation of the first and the last element to the <strong>concatenation value</strong> of <code>nums</code>, and remove those two elements from <code>nums</code>. For example, if the <code>nums</code> was <code>[1, 2, 4, 5, 6]</code>, add 16 to the <code>concatenation value</code>.</li> |
| 13 | + <li>If only one element exists in <code>nums</code>, add its value to the <strong>concatenation value</strong> of <code>nums</code>, then remove it.</li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p>Return<em> the concatenation value of <code>nums</code></em>.</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong class="example">Example 1:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> nums = [7,52,2,4] |
| 23 | +<strong>Output:</strong> 596 |
| 24 | +<strong>Explanation:</strong> Before performing any operation, nums is [7,52,2,4] and concatenation value is 0. |
| 25 | + - In the first operation: |
| 26 | +We pick the first element, 7, and the last element, 4. |
| 27 | +Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74. |
| 28 | +Then we delete them from nums, so nums becomes equal to [52,2]. |
| 29 | + - In the second operation: |
| 30 | +We pick the first element, 52, and the last element, 2. |
| 31 | +Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596. |
| 32 | +Then we delete them from the nums, so nums becomes empty. |
| 33 | +Since the concatenation value is 596 so the answer is 596. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong class="example">Example 2:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>Input:</strong> nums = [5,14,13,8,12] |
| 40 | +<strong>Output:</strong> 673 |
| 41 | +<strong>Explanation:</strong> Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0. |
| 42 | + - In the first operation: |
| 43 | +We pick the first element, 5, and the last element, 12. |
| 44 | +Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512. |
| 45 | +Then we delete them from the nums, so nums becomes equal to [14,13,8]. |
| 46 | + - In the second operation: |
| 47 | +We pick the first element, 14, and the last element, 8. |
| 48 | +Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660. |
| 49 | +Then we delete them from the nums, so nums becomes equal to [13]. |
| 50 | + - In the third operation: |
| 51 | +nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673. |
| 52 | +Then we delete it from nums, so nums become empty. |
| 53 | +Since the concatenation value is 673 so the answer is 673. |
| 54 | +</pre> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | +<p><strong>Constraints:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li><code>1 <= nums.length <= 1000</code></li> |
| 61 | + <li><code>1 <= nums[i] <= 10<sup>4</sup></code></li> |
| 62 | +</ul> |
| 63 | + |
| 64 | +<p> </p> |
| 65 | +<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;Â |
| 66 | +} |
| 67 | +.spoiler {overflow:hidden;} |
| 68 | +.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} |
| 69 | +.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} |
| 70 | +.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} |
| 71 | +</style> |
0 commit comments