3427
This commit is contained in:
15
include/solution/3427.h
Normal file
15
include/solution/3427.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_3427_H
|
||||||
|
#define INC_3427_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int subarraySum(int *nums, int numsSize);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_3427_H
|
||||||
15
src/3427.c
Normal file
15
src/3427.c
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
#include <solution/3427.h>
|
||||||
|
|
||||||
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||||
|
|
||||||
|
int subarraySum(int *nums, int numsSize)
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < numsSize; i++)
|
||||||
|
for (int j = max(0, i - nums[i]); j <= i; j++)
|
||||||
|
sum += nums[j];
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
19
tests/test_3427.cpp
Normal file
19
tests/test_3427.cpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/25.
|
||||||
|
//
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/3427.h>
|
||||||
|
|
||||||
|
TEST(SubarraySumTest, Test1)
|
||||||
|
{
|
||||||
|
int nums[] = {2, 3, 1};
|
||||||
|
int result = subarraySum(nums, 3);
|
||||||
|
EXPECT_EQ(result, 11);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SubarraySumTest, Test2)
|
||||||
|
{
|
||||||
|
int nums[] = {3, 1, 1, 2};
|
||||||
|
int result = subarraySum(nums, 4);
|
||||||
|
EXPECT_EQ(result, 13);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user