Python implementation of Leetcode Primary algorithm--string

Source: Internet
Author: User

Python implementation of Leetcode Primary algorithm--string
# invert string def reversestring (s): Return s[::-1]# reverse number def reverse (x): if x < 0:flag =-2 * * result =         -1 * INT (str (x) [1:][::-1]) if result < Flag:return 0 Else:return result Else:            Flag = 2 * * 31-1 result = Int (str (x) [:: -1]) If result > Flag:return 0 Else: Return result# the first unique character in a string def firstuniqchar (s): D = {} for I in range (len (s)): d[s[i]] = D.get (s[ I], 0) + 1 for I in range (len (s)): if d[s[i]] = = 1:return I return-1# valid letter ectopic word def isanagram (s, t        ): If Len (t)! = Len (s): return False If Len (set (t))! = Len (set (s)): return False for ex in Set (T):    If S.count (ex)! = T.count (ex): Return False return true# verify palindrome string def ispalindrome (s): Import re  s = s.lower () NewS = Re.sub (R ' [^a-za-z0-9] ', "", s) if news[::-1] = = News:return True Else:return false# string to Integer (atoi) defMyatoi (str): Import re if Re.match (' \s+ ', str)! = none:a, B = re.match (' \s+ ', str). span () str = str[b :] if str = = ": return 0 flag = True if str[0] = = '-': str = str[1:] Flag = False elif s Tr[0] = = ' + ': str = str[1:] If Re.match (' \d+ ', str)! = none:a, B = re.match (' \d+ ', str). Span () St            r = str[a:b] If flag = = True:if int (str) > 2 * * 31-1: Return 2 * * 31-1 return int (str) else:if-1 * INT (str) <-2 * * 31:return-2 * * return -1 * INT (str) Else:return 0# implements the StrStr () function.        def strStr (haystack, needle): If Len (needle) = = 0:return 0 for i in range (len (haystack)-Len (needle) + 1):   If haystack[i:i + len (needle)] = = Needle:return I return-1# number and say Def countandsay (n): keystr = ' 1 ' # starting from 1 for I in Range (n-1): # loop n times newstr = "" # store new characters strlist = []  # Loop A string down to get the number of each character sList = [] # Number of identical characters flag = True for j in Range (Len (KEYSTR)-1): # loop character length minus A flag indicates that the default last character differs from the previous one, Slist.append (Keystr[j]) slist.append (1) if keystr[j] = = Keystr                [j + 1]: # If the character of the current position is the same as the character at the next position slist[1] + = 1 flag = False Else: Strlist.append (sList) # different words store the case of the previous character in the list sList = [] flag = True If flag: # if the last                Character is different from the previous character, the character condition is added to Strlist.append ([keystr[-1], 1]) else:if sList! = []: # Last string of the same character added to the list Strlist.append (sList) for K in range (len (strlist)): # takes the characters of the list out sequentially newstr = newstr + ' + S TR (strlist[k][1]) Newstr = newstr + ' + strlist[k][0] keystr = newstr return keystr# longest common prefix def longe Stcommonprefix (STRs): if STRs = = []: return ' Lenkey = Len (strs[0]) key = 0 Longest = 0 for i in RA      Nge (1, Len (STRs)):  If Len (Strs[i]) < Lenkey:key = I lenkey = Len (strs[i]) for I in Range (len (Strs[key])):        Flag = True for j in Range (Len (STRs)): if strs[j][i]! = Strs[key][i]: flag = False If flag:longest + = 1 else:break return strs[key][0:longest]

Python implementation of the Leetcode primary algorithm--string

Related Article

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.