【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

[Q7]  把数倒过来

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Solution: https://leetcode.com/problems/reverse-integer/discuss/229800/Python3-Faster-than-100

class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        
        label = 1
        if x<0:
            label = -1
        s = str(abs(x))
        S = label*int(s[-1:None:-1])
        if S<-2**31 or S>2**31-1:
            return 0
        else:
            return S

【Q8】在给定列表中搜索数字,且数字必须在最开头位置

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.

Solution:正则表达式

class Solution:
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        
        import re
        
        x = re.search('^\s*[-\+]?\d+',str)
        if not x:
            return 0
        x = int(x.group(0))
        if x>2**31-1:
            return 2**31-1
        elif x<-2**31:
            return -2**31
        else:
            return x

【Q9】判断是否为回文

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Solution:从两边同时搜索
class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        x = str(x)
        if len(x)==0:
            return False
        i = 0
        j = len(x)-1
        while i<=j:
            if x[i]!=x[j]:
                return False
            i += 1
            j -= 1
        return True

猜你喜欢

转载自www.cnblogs.com/YunyiGuang/p/10346587.html
今日推荐