add: 230509-CN

This commit is contained in:
Eatswap 2023-05-09 10:08:21 +08:00
parent 615c8444a3
commit 4dcff98da5
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 46 additions and 0 deletions

36
cpp/2305/LC230509CN.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <string>
/**
* 2437. Number of Valid Clock Times
*
* You are given a string of length 5 called time, representing the current time on a digital clock in the format "hh:mm". The earliest possible time is "00:00" and the latest possible time is "23:59".
* In the string time, the digits represented by the ? symbol are unknown, and must be replaced with a digit from 0 to 9.
* Return an integer answer, the number of valid clock times that can be created by replacing every ? with a digit from 0 to 9.
*/
class LC230509CN {
public:
static int countTime(const std::string&) noexcept;
};
bool match(const char* s, const char* t) {
for (; *s && *t; ++s, ++t)
if (*s != *t && *t != '?')
return false;
return true;
}
int LC230509CN::countTime(const std::string& time) noexcept {
auto* time_ptr = time.c_str();
char buf[8];
int ret = 0;
for (int h = 0; h < 24; ++h) {
for (int m = 0; m < 60; ++m) {
std::sprintf(buf, "%02d:%02d", h, m);
ret += match(buf, time_ptr);
}
}
return ret;
}
using Solution = LC230509CN;

View File

@ -75,6 +75,11 @@ public:
static std::vector<int> longestObstacleCourseAtEachPosition(const std::vector<int>&) noexcept;
};
class LC230507CN {
public:
static int numPairsDivisibleBy60(const std::vector<int>&) noexcept;
};
class LC230508 {
public:
static int diagonalSum(const std::vector<std::vector<int>>&) noexcept;
@ -89,4 +94,9 @@ public:
static int minPushBox(const std::vector<std::vector<char>>&) noexcept;
};
class LC230509CN {
public:
static int countTime(const std::string&) noexcept;
};
#endif //LEETCODE_CPP_DEFS_H