-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db3002f
commit d59f999
Showing
4 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <numeric> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <gtest/gtest.h> | ||
|
||
class Solution { | ||
public: | ||
static auto pivot_index(std::vector<int32_t> &nums) -> int32_t { | ||
int32_t left_sum = 0; | ||
int32_t right_sum = std::accumulate(nums.begin(), nums.end(), 0); | ||
|
||
for (size_t i = 0; i < nums.size(); ++i) { | ||
right_sum -= nums[i]; | ||
if (right_sum == left_sum) return i; | ||
left_sum += nums[i]; | ||
} | ||
|
||
return -1; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "../include/solution.hpp" | ||
|
||
class FindPivotIndex : public ::testing::Test { | ||
protected: | ||
~FindPivotIndex() override = default; | ||
}; | ||
|
||
TEST_F(FindPivotIndex, FirstTest) { | ||
std::vector<int32_t> nums = {1, 7, 3, 6, 5, 6}; | ||
|
||
int32_t result = Solution::pivot_index(nums); | ||
int32_t expected = 3; | ||
|
||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
TEST_F(FindPivotIndex, SecondTest) { | ||
std::vector<int32_t> nums = {1, 2, 3}; | ||
|
||
int32_t result = Solution::pivot_index(nums); | ||
int32_t expected = -1; | ||
|
||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
TEST_F(FindPivotIndex, ThirdTest) { | ||
std::vector<int32_t> nums = {2, 1, -1}; | ||
|
||
int32_t result = Solution::pivot_index(nums); | ||
int32_t expected = 0; | ||
|
||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
auto main(int argc, char **argv) -> int { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters