|
@@ -9,19 +9,19 @@ using std::string;
|
|
|
class Solution {
|
|
class Solution {
|
|
|
public:
|
|
public:
|
|
|
string firstPalindrome(vector<string>& words) {
|
|
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 len = words.size();
|
|
|
auto i = len;
|
|
auto i = len;
|
|
|
for ( i = 0; i < len; ++i ) {
|
|
for ( i = 0; i < len; ++i ) {
|
|
|
- if ( isPalindrome(words[i]) ) { return words[i]; }
|
|
|
|
|
|
|
+ string s = words[i];
|
|
|
|
|
+ unsigned int slen = (unsigned int)s.length() - 1;
|
|
|
|
|
+ unsigned int end = ((unsigned int) (slen / 2)) + 1;
|
|
|
|
|
+
|
|
|
|
|
+ for ( unsigned int j = 0; j < end; ++j ) {
|
|
|
|
|
+ if ( s[j] != s[slen-j] ) { goto outer; }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return s;
|
|
|
|
|
+ outer: ;
|
|
|
}
|
|
}
|
|
|
return "";
|
|
return "";
|
|
|
}
|
|
}
|