-
[LeetCode] 917. Reverse Only LettersLeetCode 2021. 10. 15. 13:58728x90
https://leetcode.com/problems/reverse-only-letters/
Reverse Only Letters - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Reverse Only Letters

String이 주어졌을때, 영문자끼리만 reverse 시켜주는 문제이다.
되게 간단한문제인데 자꾸 사소한 실수들을 해서 오래걸렸다..
투 포인터를 이용해 풀이했다.
왼쪽포인터를 0, 오른쪽포인터를 s.size()-1 부터 시작해서
한쪽씩 a~z, A~Z 사이에 있는지 체크한다.
없을 경우엔 왼쪽일경우 ++, 오른쪽일 경우 -- 하여
다시 진행한다.
둘다 사이에 있을경우 위치를 바꿔준다.
전체코드
class Solution { public: string reverseOnlyLetters(string s) { set<int> word; for (int i=0; i<26; i++) { word.insert('a'+i); word.insert('A'+i); } int l=0, r=s.size()-1; while(l<r) { if (word.find(s[l])==word.end()) {l++;continue;} if (word.find(s[r])==word.end()) {r--;continue;} char temp = s[l]; s[l++] = s[r]; s[r--] = temp; } return s; } };'LeetCode' 카테고리의 다른 글
[LeetCode] 1961. Check If String Is a Prefix of Array (0) 2021.10.15 [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown (0) 2021.10.15 [LeetCode] 1695. Maximum Erasure Value (0) 2021.10.14 [LeetCode] 189. Rotate Array (0) 2021.10.14 [LeetCode] 279. Perfect Squares (0) 2021.10.14