Question d’entretien chez Meta

Write a palindrome-checking function

Réponses aux questions d'entretien

Utilisateur anonyme

19 nov. 2012

This has a N/2 complexity : function isPalyndrome($str) { $array = str_split($str); $size = count($array); $pivot = floor($size / 2); for($i = 0; $i < $pivot; $i++) { if($array[$i] != $array[$size - $i - 1]) { return false; } } return true; }

2

Utilisateur anonyme

29 déc. 2012

#include #include using namespace std; bool IsPalindrome(const string& input) { for (int i = 0; i < input.size()/2; ++i) { if (input[i] != input[input.size()-i-1]) { return false; } } return true; } int main() { const string str("madam"); const string str1("madama"); cout << IsPalindrome(str) << endl; cout << IsPalindrome(str1) << endl; return 0; }

Utilisateur anonyme

15 nov. 2012

My solution walked char pointers back from the end and forward from the beginning of the string, returning true if the crossed over and false if the two chars pointed to didn't match.