LeetCode #125 #680 #9 #234 2018-09-05

125. Valid Palindrome

https://leetcode.com/problems/valid-palindrome/description/

回文字符串与完全反转后的该字符串完全相同。所以利用这个性质来解代码简洁明了。当然,也可以用left, right指针分别从左右开始往中间扫,也是O(n)时间复杂度,但代码略复杂。
代码如下:

class Solution:
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        string = ''.join([char for char in s if char.isalnum()]).lower()
        return string == string[::-1]

680. Valid Palindrome II

https://leetcode.com/problems/valid-palindrome-ii/description/

这道题为上道题的变形,可以删掉一个字符。原理是利用left, right从左,右分别往中间扫描,遇到不同的字符时,分别判断删掉left或者删掉right的剩下字符组成的字符串是不是为回文字符串,只要有一个是即可,否则不可行。
代码如下:

class Solution:
    def validPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if not s:
            return True
        left, right = 0, len(s) - 1
        while left < right:
            if s[left] != s[right]:
                str1, str2 = s[left:right], s[left + 1:right + 1]
                return str1 == str1[::-1] or str2 == str2[::-1]
            left += 1
            right -= 1
        return True

9. Palindrome Number

https://leetcode.com/problems/palindrome-number/description/

取巧做法直接转换成字符串进行比较。
代码如下:

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        return str(x) == str(x)[::-1]

Follow up: Coud you solve it without converting the integer to a string?
Follow up要求不转换成字符串,我们每次需要比较最高位和个位。
我们先找到小于x的最大10的次方,比如x=34567,digit=10000,这样通过x // digit来获取最高位。
通过x % 10来获取个位。
代码如下:

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0:
            return False
        digit = 10
        while digit < x:
            digit *= 10
        digit /= 10
        while x > 0:
            first = x // digit
            last = x % 10
            if first != last:
                return False
            x %= digit
            x //= 10
            digit /= 100
        return True

234. Palindrome Linked List

https://leetcode.com/problems/palindrome-linked-list/description/

同时利用双指针和链表反转,找到链表的同时反转前半段链表,然后比较反转的前半段链表和后半段链表即可。
代码如下:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if not head or not head.next:
            return True
        pre = None
        walker = runner = head
        while runner and runner.next:
            runner = runner.next.next
            nxt = walker.next
            walker.next = pre
            pre = walker
            walker = nxt
        if runner:
            walker = walker.next
        runner = pre
        while walker and runner:
            if walker.val != runner.val:
                return False
            walker = walker.next
            runner = runner.next
        return True

你可能感兴趣的:(LeetCode #125 #680 #9 #234 2018-09-05)