From 6cfc7529fd9c314c3e50378b3693e602406aff21 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Mon, 7 Mar 2022 07:51:15 +0800 Subject: [PATCH] add: 220307-CN [cpp] --- cpp/2203/220307-CN.cpp | 23 +++++++++++++++++++++++ cpp/2203/CMakeLists.txt | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 cpp/2203/220307-CN.cpp diff --git a/cpp/2203/220307-CN.cpp b/cpp/2203/220307-CN.cpp new file mode 100644 index 0000000..315a35a --- /dev/null +++ b/cpp/2203/220307-CN.cpp @@ -0,0 +1,23 @@ +#include + +/** + * 504. Base 7 + * Given an integer num, return a string of its base 7 representation. + */ + +class Solution { +public: + static std::string convertToBase7(int num) { + bool isNeg = num < 0; + std::string ret; + num = num < 0 ? -num : num; + while (num) { + ret.push_back('0' + num % 7); + num /= 7; + } + if (isNeg) + ret.push_back('-'); + std::reverse(ret.begin(), ret.end()); + return ret.empty() ? "0" : ret; + } +}; diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index 3b7275a..4ef520e 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220306.cpp) +ADD_EXECUTABLE(2203 220307-CN.cpp)