| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- #include <iostream>
- #include <cstdint>
- #include <vector>
- #include <string>
- using std::vector;
- using std::string;
- class Solution {
- public:
- string firstPalindrome(vector<string>& words) {
- auto isPalindrome = [](string s) {
- unsigned int i = 0;
- unsigned int j = s.size() - 1;
- while ( j > i ) {
- if ( s[i] != s[j] ) { return false; }
- ++i; --j;
- }
- return true;
- };
- auto len = words.size();
- auto i = len;
- for ( i = 0; i < len; ++i ) {
- if ( isPalindrome(words[i]) ) { return words[i]; }
- }
- return "";
- }
- };
- void r(vector<string>& w) {
- std::cout << "Solution().firstPalindrome([";
- for ( uint64_t i = 0; i < w.size(); ++i ) {
- if ( i == 0 ) { std::cout << "\"" << w[i] << "\""; continue; }
- std::cout << ", \"" << w[i] << "\"";
- }
- std::cout << "]) = \"" << Solution().firstPalindrome(w) << "\"";
- };
- int main(void) {
- vector<string> w1 = {"abc", "car", "ada", "racecar", "cool"};
- r(w1);
- return 0;
- }
|