1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-25 17:50:26 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/任意子数组和的绝对值的最大值 [maximum-absolute-sum-of-any-subarray].html

38 lines
1.3 KiB
HTML
Raw Normal View History

2022-03-27 20:45:09 +08:00
<p>给你一个整数数组 <code>nums</code> 。一个子数组 <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> 的 <strong>和的绝对值</strong> 为 <code>abs(nums<sub>l</sub> + nums<sub>l+1</sub> + ... + nums<sub>r-1</sub> + nums<sub>r</sub>)</code> 。</p>
<p>请你找出 <code>nums</code> 中 <strong>和的绝对值</strong> 最大的任意子数组(<b>可能为空</b>),并返回该 <strong>最大值</strong> 。</p>
<p><code>abs(x)</code> 定义如下:</p>
<ul>
<li>如果 <code>x</code> 是负整数,那么 <code>abs(x) = -x</code> 。</li>
<li>如果 <code>x</code> 是非负整数,那么 <code>abs(x) = x</code> 。</li>
</ul>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [1,-3,2,3,-4]
<b>输出:</b>5
<b>解释:</b>子数组 [2,3] 和的绝对值最大,为 abs(2+3) = abs(5) = 5 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [2,-5,1,-4,3,-2]
<b>输出:</b>8
<b>解释:</b>子数组 [-5,1,-4] 和的绝对值最大,为 abs(-5+1-4) = abs(-8) = 8 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
</ul>