2108.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <vector>
  4. #include <string>
  5. using std::vector;
  6. using std::string;
  7. class Solution {
  8. public:
  9. string firstPalindrome(vector<string>& words) {
  10. auto isPalindrome = [](string s) {
  11. unsigned int i = 0;
  12. unsigned int j = s.size() - 1;
  13. while ( j > i ) {
  14. if ( s[i] != s[j] ) { return false; }
  15. ++i; --j;
  16. }
  17. return true;
  18. };
  19. auto len = words.size();
  20. auto i = len;
  21. for ( i = 0; i < len; ++i ) {
  22. if ( isPalindrome(words[i]) ) { return words[i]; }
  23. }
  24. return "";
  25. }
  26. };
  27. void r(vector<string>& w) {
  28. std::cout << "Solution().firstPalindrome([";
  29. for ( uint64_t i = 0; i < w.size(); ++i ) {
  30. if ( i == 0 ) { std::cout << "\"" << w[i] << "\""; continue; }
  31. std::cout << ", \"" << w[i] << "\"";
  32. }
  33. std::cout << "]) = \"" << Solution().firstPalindrome(w) << "\"";
  34. };
  35. int main(void) {
  36. vector<string> w1 = {"abc", "car", "ada", "racecar", "cool"};
  37. r(w1);
  38. return 0;
  39. }