From 812d9ac8f8a5a814297d86ffda1c6fbeaca40c9d Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Mon, 24 Jan 2022 12:40:42 +0800 Subject: [PATCH] add: 220124 --- 2201/220124.cpp | 30 ++++++++++++++++++++++++++++++ 2201/CMakeLists.txt | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 2201/220124.cpp diff --git a/2201/220124.cpp b/2201/220124.cpp new file mode 100644 index 0000000..c934eab --- /dev/null +++ b/2201/220124.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +/** + * 520. Detect Capital + * We define the usage of capitals in a word to be right when one of the following cases holds: + * All letters in this word are capitals, like "USA". + * All letters in this word are not capitals, like "leetcode". + * Only the first letter in this word is capital, like "Google". + * Given a string word, return true if the usage of capitals in it is right. + */ + +class Solution { +public: + static bool detectCapitalUse(const std::string& word) { + // auto n = std::count_if(word.begin(), word.end(), [](int x) { return (bool)std::isupper(x); }); + // Leetcode did not accept direct usage of std::isupper, but MSVC does. + const auto n = std::count_if(word.begin(), word.end(), std::isupper); + return (word.length() == n) || (!n) || (n == 1 && std::isupper(*word.begin())); + } +}; + +int main() { + std::printf("%s\n", Solution::detectCapitalUse("USA") ? "true" : "false"); + std::printf("%s\n", Solution::detectCapitalUse("leetcode") ? "true" : "false"); + std::printf("%s\n", Solution::detectCapitalUse("Google") ? "true" : "false"); + std::printf("%s\n", Solution::detectCapitalUse("FlaG") ? "true" : "false"); +} diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index ec6ad03..1ad5ee4 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220124-CN.cpp) +ADD_EXECUTABLE(2201 220124.cpp)