From 91af41b8506732e25274030657736346706ef49d Mon Sep 17 00:00:00 2001 From: wxchen Date: Wed, 2 Nov 2022 23:08:19 +0800 Subject: [PATCH] offer05 --- .vscode/c_cpp_properties.json | 13 +++++++++++++ .vscode/launch.json | 7 +++++++ offer05.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 offer05.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..092afcc --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,13 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/offer05.cpp b/offer05.cpp new file mode 100644 index 0000000..c52986a --- /dev/null +++ b/offer05.cpp @@ -0,0 +1,33 @@ +#include +#include +using namespace std; +class Solution { +public: + string replaceSpace(string s) { + int count = 0; + int lenth = s.size(); + for(auto i = 0; i < lenth; ++i){ + if(s[i] == ' '){ + count = count + 1; + } + } + s.resize(s.size() + 2 * count); + for(int i = lenth - 1, j = s.size() - 1; i < j; --i, --j){ + if (s[i] != ' ') + s[j] = s[i]; + else { + s[j - 2] = '%'; + s[j - 1] = '2'; + s[j] = '0'; + j -= 2; + } + } + return s; + } +}; + +int main(){ + string s = "We are happy."; + Solution test; + cout<