Happyleetcode2:valid palindrome

Source: Internet
Author: User

Topic

Problem solving steps
    1. The string is processed, the characters are lowercase, and the extra punctuation is removed.
    2. The case of an empty string is judged.
    3. Creates a new string object, storing the result after the original string is flipped
    4. Returns true if the two strings are equal, otherwise returns false.
My answer to the 1th edition:

Class Solution:

# @param s, a string

# @return A Boolean

def ispalindrome (self, s):

S=s.strip ()

S=s.lower ()

eff= ' qwertyuiopasdfghjklzxcvbnm1234567890 '

S=[s.replace (Cha, ') for Cha in S if not cha in eff]

If s== ':

Return True

return S[:,len (s)/2]==s[(Len (s) +1)/2,:][::-1]

Put into the test, the results found too much time-consuming, in the input as "" The time completely, but do not know why.

Class Solution {

Public

BOOL Ispalindrome (string s) {

String snew= "";

int i;

For (I=0;i<s.size (); ++i) {

if (Isalnum (S[i])) {

Snew+=tolower (S[i]);

}

}

For (I=0;i<=snew.size ()/2;++i) {

if (snew[i]! = Snew[snew.size () -1-i])

return false;

}

return true;

}

};

this C + + version of the answer was successfully passed.

Reference Answer 1

Class Solution:

# @param s, a string

# @return A Boolean

def ispalindrome (self, s):

news= [I.lower () for I in S if I.isalnum ()]

#return NewS = = News[::-1]

Return News[:len (News)/2] = = news[(len (News) +1)/2:][::-1]

Analytical:

I've used two functions I'm not familiar with: I.isalnum (): Determine whether a string or a character is a letter or a number. This is very useful.

For the Last word: News[:len (News)/2] = = news[(len (News) +1)/2:][::-1]. If there is a problem with the length of the list, it is sometimes considered that the list length is odd and even in different cases. This sentence for the list length of the odd and even have been considered, all meet the requirements.

Reference Answer 2python
Class Solution:
# @param s, a string
# @return A Boolean
def ispalindrome (self, s):
if s = = ':
Return True
Else
STMP = ' '
For I in range (0, Len (s)):
If s[i] >= ' a ' and s[i] <= ' z ' or s[i] >= ' 0 ' and s[i] <= ' 9 ' or s[i] >= ' a ' and s[i] <= ' z ':
STMP + = S[i]
STMP = Stmp.lower ()
For I in range (0, Len (STMP)/2):
if stmp[i]! = Stmp[len (STMP) -1-i]:
return False
return True
C++

bool ispalindrome (string s) {

if (!s.compare ("")) return true;

for (int i = 0, j = s.length ()-1; J >= 0 && i < s.length ();) {

if (!isalpha (s[i]) &&!isdigit (S[i])//If not alphabetic increment

i++;

Else

if (!isalpha (S[j]) &&!isdigit (S[j])//If not alphabetic increment

j--;

Else {

if (ToLower (s[i])! = ToLower (S[j]))

return false;

i++; j--;

}

}

return true;

}

Analytical:

Take a good look at the bold part.

My idea is to create a new string and store it up.

After reading some reference answers, I found that there was no need to create a new string. Directly on the original string to judge on the line, of course, this scheme in the later also has a certain degree of complexity. Try to learn a bit.

Lessons learned:

For the use and operation of the string is still very serious not familiar AH.

What I learned from the harvest

Function:

Isalnum (): This function is available in both C + + and Python, and is used directly to determine whether it is a character or a number.

ToLower (): Converts uppercase letters to lowercase. The same function also has ToUpper (): The function is self-evident.

Happyleetcode2:valid palindrome

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.