๐ ๋ฌธ์
๐ญ ์๊ฐ์ ํ๋ฆ
๋ฐฐ์ด์ ์ ํ ์๋ฃ๊ตฌ์กฐ๋ก ์ ์ฒด ๊ธธ์ด / 2๋ฅผ ๊ธฐ์ค์ผ๋ก ์ข์ฐ ๋์นญ์ ์ด๋ฃจ๊ณ ์๋ค.
๋งจ ์ฒซ์๋ฆฌ <-> ๋์๋ฆฌ
์ฒซ์๋ฆฌ + 1 <-> ๋์๋ฆฌ - 1
index๋ฅผ ์ฌ์ฉํ์ฌ ๋์นญ์ ์ด๋ฃจ๋ ๋ฌธ์์ด์ Swapping ํด์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
๐ ํ์ด (C++)
string reverseWord(string str){
char characterArray[str.length() + 1];
const int length = sizeof(characterArray);
strncpy(characterArray, str.c_str(), length);
// after strncpy null-terminate character should not be created
characterArray[length] = '\0';
for (int i = 0; i < length / 2 ; i++) {
std::swap(characterArray[i], characterArray[length - i - 2]);
}
return string(characterArray);
}
'Algorithm > Algorithm (๋ฌธ์ ํ์ด)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] Iterator (0) | 2021.02.20 |
---|---|
[Geeks For Geeks] Reverse words in a given String (0) | 2021.02.18 |
[GeeksForGeeks] Prim (0) | 2021.02.08 |
[GeeksForGeeks] Detect Cycle in a directed graph (0) | 2021.02.05 |
[GeeksForGeeks] Topological Sort using Kahn's Algorithm (0) | 2021.02.04 |