Sfoglia il codice sorgente

Adding even faster solution for 2108

Vinicius Teshima 1 mese fa
parent
commit
1672462db3
1 ha cambiato i file con 10 aggiunte e 10 eliminazioni
  1. 10 10
      C++/2108.cpp

+ 10 - 10
C++/2108.cpp

@@ -9,19 +9,19 @@ 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]; }
+            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 "";
     }