반응형

https://leetcode.com/problems/find-first-palindromic-string-in-the-array

 

LeetCode - The World's Leading Online Programming Learning Platform

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

 

첫번째로 등장하는 palindrome을 찾아줍니다.

class Solution {
public:
    string firstPalindrome(vector<string>& words) {
        for(string& word : words)
            if(isPalindrome(word)) return word;
        return "";
    }

    bool isPalindrome(string& s) {
        for(int i=0; i<s.size() / 2; i++)
            if(s[i] != s[s.size() - 1 - i]) return false;
        return true;
    }
};
반응형

+ Recent posts