View
📚 문제
Given an array nums. We define a running sum of an array as runningSum[i] = sum(num[0]...nums[i]).
Return the running sum of nums.
Example 1 :
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
💻 코드
class Solution {
public int[] runningSum(int[] nums) {
int[] sum = new int[nums.length];
for(int i=0; i<nums.length; i++){
for(int j=0; j<=i; j++) sum[i] += nums[j];
}
return sum;
}
}
💡 다른 사람 코드
class Solution {
public int[] runningSum(int[] nums) {
for(int i=1;i<nums.length;i++) nums[i]+=nums[i-1];
return nums;
}
}
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode] 724. Find Pivot Index(Java) (0) | 2023.04.05 |
---|---|
[LeetCode] 704. Binary Search(Java) (0) | 2023.04.04 |
reply