mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-12 08:55:14 +08:00
38 lines
1.4 KiB
HTML
38 lines
1.4 KiB
HTML
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>.</p>
|
|
|
|
<p>Define two arrays <code>leftSum</code> and <code>rightSum</code> where:</p>
|
|
|
|
<ul>
|
|
<li><code>leftSum[i]</code> is the sum of elements to the left of the index <code>i</code> in the array <code>nums</code>. If there is no such element, <code>leftSum[i] = 0</code>.</li>
|
|
<li><code>rightSum[i]</code> is the sum of elements to the right of the index <code>i</code> in the array <code>nums</code>. If there is no such element, <code>rightSum[i] = 0</code>.</li>
|
|
</ul>
|
|
|
|
<p>Return an integer array <code>answer</code> of size <code>n</code> where <code>answer[i] = |leftSum[i] - rightSum[i]|</code>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [10,4,8,3]
|
|
<strong>Output:</strong> [15,1,11,22]
|
|
<strong>Explanation:</strong> The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0].
|
|
The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [1]
|
|
<strong>Output:</strong> [0]
|
|
<strong>Explanation:</strong> The array leftSum is [0] and the array rightSum is [0].
|
|
The array answer is [|0 - 0|] = [0].
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= nums.length <= 1000</code></li>
|
|
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
|
</ul>
|