Defeated algorithm - convert a string integer

Reference article

From exam on LeetCode - String to Integer, this article is mainly official solution to a problem on the reference LeetCode

The official solution to a problem using the concept of compiler theory of finite automata, did not expect the compiler theory of knowledge can be applied to the algorithm, so in this article scala recorded version of the solution, but not the specific questions construed to reports, want to see a detailed explanation Please also force the venue's official website buckle

https://leetcode-cn.com/problems/string-to-integer-atoi/

Convert a string integer problems

First, the function will begin with a space character discard useless if necessary, until the find to the first non-space character so far. The following conversion rules are as follows:

  • If the first character is a non-null positive or negative number, the sign of the back with as many consecutive numeric characters are combined to form a signed integer
  • If the first non-space character is a number, which is directly after the continuous combination of numeric characters, forming a integer
  • The string also there may be extra characters after a valid integer part, these characters can be ignored, they should not affect the function of

note:

If the string when the first non-space character is not a valid integer character string is empty or contains only whitespace character string, the function does not require you to convert that conversion can not be effective. In any case, if the function can not effectively convert, returns 0

Tip:
The title includes only whitespace space character ''
is assumed that we can store 32-bit environment sized signed integer, then the value range of [
- 231 231  - 1]. If the value exceeds this range, return INT_MAX (231  - . 1) or INT_MIN ( - 231)

Example 1:
Input: "42
Output: 42

Example 2:
Input: "-42"
output: -4
explanation: the first non-blank character is '-', it is a negative sign, we as a negative number and all the numbers appear in a row behind the combine, and finally get -42

Example 3:
Input: "4193 with words"
Output: 4193
Explanation: converting the digital OFF '3' as its next character is not a digital

Example 4:
Input: "words and 987"
Output: 0
Explanation: valid is a first non-blank character 'w', but it is not a positive number or a negative number, and therefore can not perform the conversion

Example 5:
Input: "-91283472332
Output: -2147483648
explanation: the number" -91283472332 "more than 32 signed integer range, the flow returns INT_MIN (
- 231)

 

Deterministic finite automata five elements of DFA

I.e. pentad = A (K, [Sigma, [delta], Q 0 , the Z)

Finite set of K; [Sigma finite set of input symbols; transition function [delta]; a start state Q 0 ; final state set Z

Transfer function δ: K × Σ → K, exceptions: δ (q, ε) = q

Here on the official website of borrowing transition diagram to illustrate the concepts it

Finite set K = {start, signed, in_number, end}

Finite set of input symbols Σ = {number, +, - , ︺, other}
herein, "number" refers to the ten digits 0 to 9, "other" refers to all other characters in addition to numbers, a positive sign and spaces

Transfer function δ (start, +) = δ (start, -) = signed, δ (signed, number) = number other transfer functions are not excessive Examples

Start state Q 0 = Start

Final state set Z = {end}

Official website also provides a transition table representation of

If the input symbol can correspond to a plurality of other states of the state, in a non-deterministic finite automaton NFA

 

DFA solution

def myAtoi(str: String): Int = {
  /*
   *
构造转移表
   */
  var
map = new mutable.HashMap[String, List[String]]()
  map += ("start" -> List("start", "signed", "in_number", "end"))
  map += ("signed" -> List("end", "end", "in_number", "end"))
  map += ("in_number" -> List("end", "end", "in_number", "end"))
  map += ("end" -> List("end", "end", "end", "end"
  

=dx  var   * /StartStart state   *DyanddxCoordinate transition table of
   *  / *))

"Start"
  var Dy = 0
  / *
   *
record negative
   * /
  var
Sign = . 1
  / *
   *
storing results
   * /
  var
ANS = 0
  for ( char <- STR IF DX ! = "End" ) {
    Dy = IF ( char . isWhitespace ) 0
         the else IF ( char == '+' || char == '-' ) . 1
         else if (char.isDigit) 2
         else 3

    dx = map(dx)(dy)
    if (dx == "in_number") {
    /*
     *
判断是否超出Int范围
     */
      if
((Int.MaxValue - char + '0') / 10 < ans)
        return if (sign == 1) Int.MaxValue else Int.MinValue
      else
        ans = ans * 10 + char - '0'
    } else if (dx == "signed") {
      sign = if (char == '+') 1 else -1
    }
  }

  ans * sign
}

Guess you like

Origin www.cnblogs.com/kuluo/p/12656450.html