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
19 changes: 9 additions & 10 deletions leetcode/0392/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include <string>

class Solution {
public:
bool isSubsequence(std::string s, std::string t) {
// 方法1:双指针
struct Solution {
static bool isSubsequence(const std::string &s, const std::string &t) {
std::size_t ss{s.size()}, ts{t.size()};
if (s.size() > t.size()) {
return false;
}
std::size_t pos{0};
for (char ch : s) {
pos = t.find(ch, pos);
if (pos == t.npos) {
return false;
std::size_t si{0};
for (std::size_t ti{0}; si < ss && ti < ts; ++ti) {
if (s[si] == t[ti]) {
++si;
}
++pos;
}
return true;
return si == ss;
}
};
1 change: 1 addition & 0 deletions leetcode/0392/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ void Expect(const TestCase &tc) {

TEST_Y(LeetCode392, Example1);
TEST_Y(LeetCode392, Example2);
TEST_Y(LeetCode392, Regression1);
} // namespace
6 changes: 6 additions & 0 deletions leetcode/0392/test_suite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ example2:
s: "axc"
t: "ahbgdc"
expected: false

regression1:
input:
s: "abc"
t: ""
expected: false
8 changes: 3 additions & 5 deletions leetcode/2011/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

// 方法1:模拟
namespace approach1 {
class Solution {
public:
struct Solution {
static int finalValueAfterOperations(const std::vector<std::string> &operations) {
int res{0};
for (const auto &op : operations) {
Expand All @@ -20,10 +19,9 @@ class Solution {
};
} // namespace approach1

// 方法2:计数
// 方法2:计数 + 数学推导
namespace approach2 {
class Solution {
public:
struct Solution {
static int finalValueAfterOperations(const std::vector<std::string> &operations) {
auto isInc{[](const std::string &op) { return op[1] == '+'; }};
return 2 * std::ranges::count_if(operations, isInc) - operations.size();
Expand Down