Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_compile_options(-std=c++17 -Werror -Wall -Wextra)
add_compile_options(-Werror -Wall -Wextra)
set(EXECUTABLE_OUTPUT_PATH ${project_dir}/build/bin)

if(ENABLE_ASAN)
Expand Down
15 changes: 13 additions & 2 deletions leetcode/0001-two-sum/test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
#include "solution.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

void Expect(const std::vector<int> &expected, const std::vector<int> &nums, int target) {
using namespace testing;

void Expect(const std::vector<int> &expected, const std::vector<int> &nums, const int target) {
// 检查约束条件
ASSERT_THAT(nums.size(), AllOf(Ge(2), Le(10000))); // 2 <= nums.length <= 10^4
for (int n : nums) {
ASSERT_THAT(n, AllOf(Ge(-1000000000), Le(1000000000))); // -10^9 <= nums[i] <= 10^9
}
ASSERT_THAT(target, AllOf(Ge(-1000000000), Le(1000000000))); // -10^9 <= target <= 10^9

// 验证目标函数
Solution solution;
auto res{solution.twoSum(nums, target)};
ASSERT_EQ(res.size(), 2);
ASSERT_EQ(2, res.size());
if (res[0] > res[1]) {
std::swap(res[0], res[1]);
}
Expand Down
19 changes: 16 additions & 3 deletions leetcode/0392-is-subsequence/test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#include "solution.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

TEST(LeetCode_0392, Case1) {
EXPECT_TRUE(Solution{}.isSubsequence("abc", "ahbgdc"));
EXPECT_FALSE(Solution{}.isSubsequence("axc", "ahbgdc"));
using namespace ::testing;

template <typename Solution>
void Expect(const bool expected, const std::string &s, const std::string &t) {
// 检查约束条件
ASSERT_THAT(s.size(), AllOf(Ge(0), Le(100))); // 0 <= s.length <= 100
ASSERT_THAT(t.size(), AllOf(Ge(0), Le(10000))); // 0 <= t.length <= 10^4

// 测试目标函数
Solution solution;
EXPECT_EQ(expected, solution.isSubsequence(s, t));
}

TEST(LeetCode392, Example1) { Expect<Solution>(true, "abc", "ahbgdc"); }

TEST(LeetCode392, Example2) { Expect<Solution>(false, "axc", "ahbgdc"); }
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Solution {
public:
static int finalValueAfterOperations(const std::vector<std::string> &operations) {
int res{0};
for (const std::string &op : operations) {
for (const auto &op : operations) {
if (op[1] == '+') {
++res;
} else {
Expand All @@ -20,14 +20,13 @@ class Solution {
};
} // namespace approach1

// 方法2:加法计数
// 方法2:++计数
namespace approach2 {
class Solution {
public:
static int finalValueAfterOperations(const std::vector<std::string> &operations) {
auto is_inc{[](const std::string &op) { return op[1] == '+'; }};
auto incs{std::count_if(operations.begin(), operations.end(), is_inc)};
return 2 * incs - operations.size();
auto isInc{[](const std::string &op) { return op[1] == '+'; }};
return 2 * std::ranges::count_if(operations, isInc) - operations.size();
}
};
} // namespace approach2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "gtest/gtest.h"

template <typename Solution>
void Expect(int expected, const std::vector<std::string> &operations) {
void Expect(const int expected, const std::vector<std::string> &operations) {
Solution solution;
EXPECT_EQ(expected, solution.finalValueAfterOperations(operations));
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>

// 方法1:枚举 + 二分查找
struct Solution {
static int maxFrequency(std::vector<int> nums, int k, int numOperations) {
std::ranges::sort(nums);
int fMax{0};
int i{nums.front()};
for (size_t a{0}, b{0}; a < nums.size() + 1; ++a) {
if (a < nums.size() && nums[a] == nums[b]) {
continue;
}
int count = int(a - b);
int limit = (a < nums.size()) ? nums[a] : nums.back() + 1;
while (i < std::min(nums[b] + k + 1, limit)) {
auto l{std::lower_bound(nums.begin(), nums.end(), i - k)};
auto r{std::upper_bound(nums.begin(), nums.end(), i + k)};
int f = std::min<int>(r - l, count + numOperations);
fMax = std::max<int>(fMax, f);
// std::cout << "i: " << i << " " <<r - l << " " << count + numOperations << " " << f << "\n";
++i;
count = 0;
}
b = a;
}
return fMax;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "solution.hpp"
#include "gtest/gtest.h"

template <typename Solution>
void Expect(const int expected, const std::vector<int> &nums, const int k, const int numOperations) {
Solution solution;
EXPECT_EQ(expected, solution.maxFrequency(nums, k, numOperations));
}

TEST(LeetCode3346, Example1) {
const std::vector<int> nums{1, 4, 5};
Expect<Solution>(2, nums, 1, 2);
}

TEST(LeetCode3346, Example2) {
const std::vector<int> nums{5, 11, 20, 20};
Expect<Solution>(2, nums, 5, 1);
}

TEST(LeetCode3346, Regression1) {
const std::vector<int> nums{88, 53};
Expect<Solution>(2, nums, 27, 2);
}

TEST(LeetCode3346, Regression2) {
const std::vector<int> nums{1, 2, 4, 5};
Expect<Solution>(4, nums, 2, 4);
}

TEST(LeetCode3346, Regression3) {
// 用例存在yaml中
}

TEST(LeetCode3346, Regression4) {
const std::vector<int> nums{2};
Expect<Solution>(1, nums, 7, 0);
}

TEST(LeetCode3346, Regression5) {
const std::vector<int> nums{1, 90};
Expect<Solution>(1, nums, 76, 1);
}

TEST(LeetCode3346, Regression6) {
const std::vector<int> nums{111, 21, 21, 13, 11};
Expect<Solution>(1, nums, 3, 0);
}
1 change: 1 addition & 0 deletions third_party/cmake/googletest.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
if(NOT TARGET gtest)
execute_process(COMMAND git submodule update --init --recursive ${3rd_dir}/googletest)
include_directories(SYSTEM ${3rd_dir}/googletest/googletest/include)
include_directories(SYSTEM ${3rd_dir}/googletest/googlemock/include)
add_subdirectory(${3rd_dir}/googletest)
endif()