본문 바로가기

LeetCode

2562. Find the Array Concatenation Value / TypeScript

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.

 

출처 : https://leetcode.com/problems/find-the-array-concatenation-value/description/

 

풀이

function findTheArrayConcVal(nums: number[]): number {
let answer = 0;

    while (nums.length > 0) {
        if (nums.length > 1) {
            const merged = Number(`${nums.shift()}${nums.pop()}`);
            answer += merged;
        } else {
            answer += nums.pop()!;
        }
    }

    return answer;
};

 

 

  1. shift(): 이 메서드는 배열에서 첫 번째 요소를 제거하고, 그 요소를 반환. 제거된 요소에 의해 배열의 길이가 달라져 만약 배열이 비어있으면 undefined를 반환.

예를 들어, 다음과 같이 배열 arr이 있을 때:

const arr = [1, 2, 3, 4];
const firstElement = arr.shift();
console.log(firstElement); // 출력: 1
console.log(arr); // 출력: [2, 3, 4]
  1. pop(): 이 메서드는 배열에서 마지막 요소를 제거하고, 그 요소를 반환. 제거된 요소에 의해 배열의 길이가 달라져 만약 배열이 비어있으면 undefined를 반환.

예를 들어, 위의 arr 배열을 다시 사용하여 pop()을 적용

 

const lastElement = arr.pop();
console.log(lastElement); // 출력: 4
console.log(arr); // 출력: [2, 3]

 

 

정리:

shift()는 배열의 시작 부분에서 요소를 제거하고, pop()은 배열의 끝 부분에서 요소를 제거.

 

'LeetCode' 카테고리의 다른 글

228. Summary Ranges / TypeScript  (0) 2024.05.20
2932. Maximum Strong Pair XOR I / TypeScript  (0) 2024.05.19
455. Assign Cookies / TypeScript  (0) 2024.05.17
1695. Maximum Erasure Value / TypeScript  (0) 2024.05.17
2427. Number of Common Factors  (0) 2024.05.15