|
| 1 | +--- |
| 2 | +title: 2221. Find Triangular Sum of an Array |
| 3 | +description: You are given a 0-indexed integer array nums, where nums[i] is a digit between 0 and 9 (inclusive). |
| 4 | +keywords: [LeetCode, 2221. Find Triangular Sum of an Array, Medium, Array, Math, Simulation, Combinatorics] |
| 5 | +--- |
| 6 | + |
| 7 | +# [{frontMatter.title}](https://leetcode.com/problems/find-triangular-sum-of-an-array/) |
| 8 | + |
| 9 | +:::danger Medium |
| 10 | +::: |
| 11 | + |
| 12 | +import Tabs from "@theme/Tabs"; |
| 13 | +import TabItem from "@theme/TabItem"; |
| 14 | + |
| 15 | +<Tabs> |
| 16 | + <TabItem value="description" label="題目描述" default> |
| 17 | + ## Description |
| 18 | +You are given a **0-indexed** integer array nums, where nums[i] is a digit between 0 and 9 (**inclusive**). |
| 19 | +The **triangular sum** of nums is the value of the only element present in nums after the following process terminates: |
| 20 | + 1. Let nums comprise of n elements. If `n == 1`, **end** the process. Otherwise, **create** a new **0-indexed** integer array `newNums` of length `n - 1`. |
| 21 | + 2. For each index i, where `0 <= i < n - 1`, **assign** the value of `newNums[i]` as `(nums[i] + nums[i+1]) % 10`, where % denotes modulo operator. |
| 22 | + 3. **Replace** the array `nums` with `newNums`. |
| 23 | + 4. **Repeat** the entire process starting from step 1. |
| 24 | +Return the triangular sum of `nums`. |
| 25 | + |
| 26 | +#### Example 1: |
| 27 | + |
| 28 | +> **Input:** nums = [1,2,3,4,5] |
| 29 | +**Output:** 8 |
| 30 | +**Explanation:** |
| 31 | +The above diagram depicts the process from which we obtain the triangular sum of the array. |
| 32 | + |
| 33 | +#### Example 2: |
| 34 | +> **Input:** nums = [5] |
| 35 | +**Output:** 5 |
| 36 | +**Explanation:** |
| 37 | +Since there is only one element in nums, the triangular sum is the value of that element itself. |
| 38 | + |
| 39 | +## Constraints: |
| 40 | + |
| 41 | + - `1 <= nums.length <= 1000` |
| 42 | + - `0 <= nums[i] <= 9` |
| 43 | + |
| 44 | + </TabItem> |
| 45 | + <TabItem value="solution" label="解答"> |
| 46 | + ## Solution |
| 47 | +```js title="JavaScript" |
| 48 | +/** |
| 49 | + * @param {number[]} nums |
| 50 | + * @return {number} |
| 51 | + */ |
| 52 | +var triangularSum = function (nums) { |
| 53 | + while (nums.length > 1) { |
| 54 | + const newNums = []; |
| 55 | + for (let i = 0; i < nums.length - 1; i++) { |
| 56 | + newNums.push((nums[i] + nums[i + 1]) % 10); |
| 57 | + } |
| 58 | + nums = newNums; |
| 59 | + } |
| 60 | + |
| 61 | + return nums[0]; |
| 62 | +}; |
| 63 | +``` |
| 64 | + </TabItem> |
| 65 | +</Tabs> |
| 66 | + |
| 67 | + |
| 68 | +## 解題思路 |
| 69 | +根據題目描述的 1. 2. 3. 4. 步驟那邊,第一步是 |
| 70 | +> 1. Let nums comprise of n elements. If `n == 1`, **end** the process. Otherwise, **create** a new **0-indexed** integer array `newNums` of length `n - 1`. |
| 71 | +
|
| 72 | +所以使用下面的程式碼來起頭,當 nums 這個 array 的長度仍大於一的時候就持續執行迴圈,且每次迴圈重新執行的時候建立一個新的 newNums 陣列。 |
| 73 | +```js |
| 74 | + while (nums.length > 1) { |
| 75 | + const newNums = []; |
| 76 | + } |
| 77 | +``` |
| 78 | + |
| 79 | +> 2. For each index i, where `0 <= i < n - 1`, **assign** the value of `newNums[i]` as `(nums[i] + nums[i+1]) % 10`, where % denotes modulo operator. |
| 80 | +
|
| 81 | +第二步是要算出下一排的值,規則是把 `nums[i]` 和 `nums[i + 1]` 加起來,如果大於 10 的話該格會變成加起來的值除 10 的餘數 |
| 82 | +ex: 如果是 `7+5=12` 的話那格就會變成 12 除 10 的餘數 `2`) |
| 83 | + |
| 84 | +直接把這段轉成程式碼就會變成 |
| 85 | +```js |
| 86 | + for (let i = 0; i < nums.length - 1; i++) { |
| 87 | + newNums.push((nums[i] + nums[i + 1]) % 10); |
| 88 | + } |
| 89 | +``` |
| 90 | + |
| 91 | +第三步就是直接使用 newNums 來取代運算前的 nums,更新新一排的陣列,新一排的長度會是前一排減一,就這樣子重複運算直到只剩下一個值。 |
| 92 | +如附圖的 `[1,2,3,4,5]`,在經過一輪的計算後會變成 `[3,5,7,9]`,再一輪則會是 `[8,2,6]` |
| 93 | +> 3. **Replace** the array `nums` with `newNums`. |
| 94 | +```js |
| 95 | + nums = newNums; |
| 96 | +``` |
| 97 | + |
| 98 | +最後重複執行整個過程,剩下一個值的時候就是要回傳的結果 |
| 99 | +> 4. **Repeat** the entire process starting from step 1. |
| 100 | +```js |
| 101 | +var triangularSum = function (nums) { |
| 102 | + while (nums.length > 1) { |
| 103 | + const newNums = []; |
| 104 | + for (let i = 0; i < nums.length - 1; i++) { |
| 105 | + newNums.push((nums[i] + nums[i + 1]) % 10); |
| 106 | + } |
| 107 | + nums = newNums; //會慢慢減少 nums.length,直到 nums.length = 1 時候跳出迴圈 |
| 108 | + } |
| 109 | + |
| 110 | + return nums[0]; |
| 111 | +}; |
| 112 | +``` |
| 113 | +## 心得 |
| 114 | + |
| 115 | +第一眼覺得很複雜,但搭配圖解之後有變得好理解不少! |
| 116 | +而且題目描述就把整體的 sudo code 完成得差不多了,只要再轉換成程式碼就差不多解出來了。 |
| 117 | + |
| 118 | + |
0 commit comments