This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

First, write first

I heard you’re still writing a two-layer for loop to solve the sum of two numbers?

LeetCode 2: Sum of two numbers transfer gate: the median of two sorted arrays, “most” technical solution!

LeetCode 第 3 题 : Longest loop transport gate: Horse-and-cart algorithm to solve longest loop! Manacher

Question 4: String to integer (ATOI), for the interview, looking forward to your participation.

Today’s topic

Implement AToi, convert string to integer.

The function first discards as many space characters as needed until the first non-space character is found. If the first non-null character is a plus or minus sign, take that symbol and combine it with as many consecutive digits as possible to make that part of the character the value of the integer. If the first non-null character is a number, it is directly combined with successive numeric characters to form an integer.

Strings can include extra characters after characters that form integers. These characters can be ignored and have no effect on the function.

When the first non-null character sequence in a string is not a valid integer; Or the string is empty; Or if the string contains only whitespace characters, no conversion is performed.

If the function cannot perform a valid conversion, return 0.

Description:

Assume that our environment can store only 32-bit signed integers with values in the range of [−231, 231 − 1]. If the value exceeds the representable range, INT_MAX (231 − 1) or INT_MIN (231) is returned.

Example:

The sample1Input:"42"Output:42The sample2Input:"- 42"Output: -42Explanation: The first non-whitespace character isThe '-'It's a minus sign. We can combine the minus sign with all the consecutive numbers that follow, and we get minus42. The sample3Input:"4193 with words"Output:4193Explanation: Conversion ends at number'3'Because its next character is not a number. The sample4Input:"words and 987"Output:0Explanation: The first non-null character is'w'But it's not a number or a plus or minus sign. Therefore, a valid transformation cannot be performed. The sample5Input:"91283472332"Output: -2147483648Explanation: Numbers"91283472332"More than32Bit range of signed integers. So INT_MIN (−) is returned231).Copy the code

Three, the analysis

This topic, the topic is longer, the sample is much also, can you look at more impatient or “fear”, I’m talking to tell you that the subject is very simple, look me in the face of the topic under, such as the title says, I wish the method called “yu gong yi shan”, direct points of discussion, Then merge into the general (general) situation and characteristic (case) situation, one by one judgment can be broken, welcome to share more solutions in the comments section.

The process may look a little complicated, but read carefully, are relatively simple implementation, next to the keyboard ~

Fourth, the problem solving

  • My approach:

Return 0 = 0 return 0 = 0 return 0 = 0 return 0 = 0

The code debugging process is extremely difficult, the various bugs encountered in the process of running…

Input:"- 5 -"Output:0Expectations: -5Input:"+ - 2"Output:2Expectation:0Input:"5 +"Output: error,'5 +'Can't be converted tofloatExpectation:5Here are just a few of the typical mistakes...Copy the code
  • code
Title: Implement AToi to convert a string to an integer. ascii to integer '''

# My way
class Solution(object) :
    def myAtoi(self, str) :
        """ :type str: str :rtype: int """
	    # string is not empty/the first character is not a number /-/+
        str = str.strip()  # remove Spaces at both ends
        if str= =' ':   # is empty
	        return 0
        if str[0] != The '-' and str[0] != '+' and not str[0].isdigit():
            return 0    # The first character must not be a digit /-/+
        import re
        # Make sure the string contains the numbers /+/-
        pattern = re.compile("[+ 0-9] +")
        judge = pattern.findall(str)
        # for example: "","-","+"
        if not judge or judge[0] = ='+' or judge[0] = =The '-':
              return 0
        # example: "++","--","-+/+-","-2-","2-","--2"
        if len(judge[0> =])2:
	        # Make sure the string contains numbers
            pattern0 = re.compile("[0-9] +")
            test01 = pattern0.findall(judge[0])
            if not test01:
                return 0  # only + / -
            if not judge[0] [1:2].isdigit():
                return 0   # remove ++/-- the first character is not a number
            if judge[0] [0] = =The '-' or judge[0] [0] = ='+':
                judge[0] = judge[0] [0] + test01[0]   # The first character is +/- and the result is a symbol + number
            else:  # only numbers, no sign bits
                judge[0] = test01[0]
        interim_target = float(judge[0])  Convert to float to determine the range
        if interim_target < -2147483648:
            return -2147483648
        if interim_target > 2147483647:
            return 2147483647
        target = int(judge[0])  # range, converted to int return
        return target
Copy the code
  • Submit the results

Test data: 1079 group running time: 48ms beat percentage :73.24%

Fortunately, the submission results are good, otherwise, it takes so long, really want to vomit blood ah…

Five, the conclusion

Persistence and hard work: results.

The idea is very complicated,

The implementation is interesting,

As long as you don’t give up,

Fame will come.

— Old Watch doggerel

See you next time. I’m a cat lover and a tech lover. If you find this article helpful, please like, comment and follow me!