https://leetcode.com/problems/reverse-string/description/
Reverse String - LeetCode
Reverse String - Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm] with O(1) extra memory. Example 1:
leetcode.com
문자열 관련 함수 reverse를 사용해보는 문제였습니다.
📕 풀이방법
📔 풀이과정
https://cplusplus.com/reference/algorithm/reverse/
https://cplusplus.com/reference/algorithm/reverse/
12345678 template void reverse (BidirectionalIterator first, BidirectionalIterator last) { while ((first!=last)&&(first!=--last)) { std::iter_swap (first,last); ++first; } }
cplusplus.com
revoerse함수는 반환형이 void이며 iterator형 문자열을 받아 swap형식을 통해 문자를 뒤집는 함수입니다.vector <char>또는 string형을 begin(), end()함수로 first와 last를 지정해 인자로 넣을 수 있습니다.
📕 Code
📔 C++
class Solution {
public:
void reverseString(vector<char>& s) {
reverse(s.begin(), s.end());
}
};
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > String' 카테고리의 다른 글
(C++) - LeetCode (easy) 28. Find the Index of the First Occurrence in a String (0) | 2023.03.08 |
---|---|
(C++) - LeetCode (easy) 345. Reverse Vowels of a String (0) | 2023.02.20 |
(C++) - LeetCode (easy) 290. Word Pattern (0) | 2023.02.07 |
(C++) - LeetCode (easy) 125. Valid Palindrome (0) | 2022.11.30 |
(C++) - LeetCode (easy) 58. Length of Last Word (0) | 2022.11.03 |