728x90
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/
for문으로도 할 수 있고, Two Pointer로 양 옆에서 포인터를 가리키며 진행할 수도 있다. 이번에는 two pointer로 해봤다.
string firstPalindrome(vector<string> words) {
string answer = "";
for(int i=0; i<words.size(); i++){
string s = words[i];
int l=0, r=s.size()-1;
bool f = false;
while(l<r){
if(s[l] != s[r]) {
f = true;
break;
}else{
l++; r--;
}
}
if(!f) return s;
}
return answer;
}
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode/easy/String] Merge Strings Alternately (0) | 2022.08.16 |
---|---|
[LeetCode/easy/String] Reverse Prefix of Word (0) | 2022.08.16 |
[LeetCode/easy/Array] Single Number (0) | 2022.08.16 |
[LeetCode/easy/Array] Best Time to Buy and Sell Stock (0) | 2022.08.16 |
[LeetCode/easy/Array] Pascal's triangle II (0) | 2022.08.16 |