1480:一维数组的动态和

This commit is contained in:
huangge1199 2021-08-23 16:51:34 +08:00 committed by huangge1199@hotmail.com
parent df651c040a
commit fd1cc1158b
4 changed files with 90 additions and 2 deletions

View File

@ -0,0 +1,55 @@
//给你一个数组 nums 数组动态和的计算公式为runningSum[i] = sum(nums[0]nums[i])
//
// 请返回 nums 的动态和
//
//
//
// 示例 1
//
// 输入nums = [1,2,3,4]
//输出[1,3,6,10]
//解释动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4]
//
// 示例 2
//
// 输入nums = [1,1,1,1,1]
//输出[1,2,3,4,5]
//解释动态和计算过程为 [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]
//
// 示例 3
//
// 输入nums = [3,1,2,10,1]
//输出[3,4,6,16,17]
//
//
//
//
// 提示
//
//
// 1 <= nums.length <= 1000
// -10^6 <= nums[i] <= 10^6
//
// Related Topics 数组 前缀和 👍 107 👎 0
package leetcode.editor.cn;
//1480:一维数组的动态和
class RunningSumOf1dArray{
public static void main(String[] args) {
//测试代码
Solution solution = new RunningSumOf1dArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] runningSum(int[] nums) {
for (int i = 1; i < nums.length; i++) {
nums[i] += nums[i-1];
}
return nums;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,33 @@
<p>给你一个数组 <code>nums</code> 。数组「动态和」的计算公式为:<code>runningSum[i] = sum(nums[0]&hellip;nums[i])</code></p>
<p>请返回 <code>nums</code> 的动态和。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3,4]
<strong>输出:</strong>[1,3,6,10]
<strong>解释:</strong>动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4] 。</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [1,1,1,1,1]
<strong>输出:</strong>[1,2,3,4,5]
<strong>解释:</strong>动态和计算过程为 [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1] 。</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>nums = [3,1,2,10,1]
<strong>输出:</strong>[3,4,6,16,17]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>-10^6&nbsp;&lt;= nums[i] &lt;=&nbsp;10^6</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>前缀和</li></div></div><br><div><li>👍 107</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long