Skip to content

Commit 0244c2e

Browse files
committed
1. Formatting fixes.
2. Added new problem: Product of Array Except Self - 238.
1 parent 0fe69ff commit 0244c2e

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

include/leeter/h_index_274.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
#include <vector>
21
#include <algorithm>
3-
using std::vector;
2+
#include <vector>
43
using std::sort;
4+
using std::vector;
55

66
namespace h_index {
77
class Solution {
88
public:
99
int hIndex(vector<int>& citations) {
10-
sort(citations.begin(), citations.end());
11-
int n = citations.size();
12-
for (int i = 0; i < n; ++i) {
13-
if (citations[i] >= n - i) {
14-
return n - i;
15-
}
10+
sort(citations.begin(), citations.end());
11+
int n = citations.size();
12+
for (int i = 0; i < n; ++i) {
13+
if (citations[i] >= n - i) {
14+
return n - i;
1615
}
17-
return 0;
16+
}
17+
return 0;
1818
}
1919
};
2020
} // namespace h_index
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <vector>
2+
using std::vector;
3+
4+
namespace product_of_array_except_self {
5+
class Solution {
6+
public:
7+
vector<int> productExceptSelf(vector<int>& nums) {
8+
int prefix_sum = 1;
9+
int suffix_sum = 1;
10+
vector<int> result(nums.size(), 1);
11+
for (int i = 0; i < nums.size(); i++) {
12+
result[i] = prefix_sum;
13+
prefix_sum *= nums[i];
14+
}
15+
for (int i = nums.size() - 1; i >= 0; i--) {
16+
result[i] *= suffix_sum;
17+
suffix_sum *= nums[i];
18+
}
19+
return result;
20+
}
21+
};
22+
} // namespace product_of_array_except_self

test/source/test_h_index_274.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <doctest/doctest.h>
2-
#include "leeter/h_index_274.hpp"
32

3+
#include "leeter/h_index_274.hpp"
44

55
TEST_CASE("h index: test case 1") {
66
vector<int> input = {3, 0, 6, 1, 5};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <doctest/doctest.h>
2+
3+
#include "leeter/product_of_array_except_self_238.hpp"
4+
5+
TEST_CASE("Product of Array Except Self") {
6+
product_of_array_except_self::Solution s;
7+
std::vector<int> nums = {1, 2, 3, 4};
8+
std::vector<int> expected = {24, 12, 8, 6};
9+
CHECK(s.productExceptSelf(nums) == expected);
10+
}
11+
12+
TEST_CASE("Product of Array Except Self - Test case 2") {
13+
product_of_array_except_self::Solution s;
14+
std::vector<int> nums = {-1, 1, 0, -3, 3};
15+
std::vector<int> expected = {0, 0, 9, 0, 0};
16+
CHECK(s.productExceptSelf(nums) == expected);
17+
}

0 commit comments

Comments
 (0)