Re: [闲聊] 每日leetcode

楼主: eden6197 (:))   2024-08-04 14:32:45
题目:
问你subarray的和sort之后
left到right加起来是多少
思路:
照做
把subarray 通通弄出来之后
sort加起来
姆咪
```cpp
class Solution {
public:
int rangeSum(vector<int>& nums, int n, int left, int right)
{
vector<int> sum(n,0);
vector<int> paper(n * (n + 1) / 2 , 0);
sum[0] = nums[0];
for(int i = 1 ; i < n ; i ++)
{
sum[i]+=sum[i-1];
sum[i]+=nums[i];
}
int p = 0;
for(int i = 0 ; i < n ; i ++)
{
paper[p] = sum[i];
p++;
for(int j = i+1 ; j < n ; j ++)
{
paper[p] = sum[j] - sum[i];
p++;
}
}
sort(paper.begin() , paper.end());
int res = 0;
for(int i = left -1 ; i < right ; i ++)
{
res += paper[i];
res %= 1000000007;
}
return res;
}
};
```
作者: nozomizo (希霙)   2024-08-04 14:34:00
什么!
作者: SAKIASHIZAWA (芦泽サキ)   2024-08-04 14:34:00
鸡哥多才多艺 竟然也会写code

Links booklink

Contact Us: admin [ a t ] ucptt.com