Valid number: determines whether the string is a valid number.
RT, interview question, given a string to determine whether it is a valid number in scientific notation. This topic is complicated to consider. Today, we finally learned to use finite state machines for processing. It is easy to understand. I would like to share with you my understanding and derivation ideas. There is a solution on the Internet that first records the meaning of each State, and then writes the transfer matrix based on the possible transfer of the State. However, you must be confused. If you have more than one status, it will be messy. It is difficult to be patient or carefully count the status clearly and record it correctly. Now, how can I help you better understand the situation? In the end, we don't need to know what the meaning of each status is at last, just write it according to the entered specifications. First, we need to know that each input has six possibilities: invalid, space, sign, digit, dot, the index e or E is currently recorded as eE. The first transfer is of course from the status 0, and the transfer records after the status 0 are as follows: status 0: if an invalid is returned, the next transfer-1; (-1 indicates that there is no such status, then false is returned.) If a space is used, it is still in the same status as the current status 0, so it is transferred to 0. If a sign is used, so we have to transfer it out, but we do not know what status can be transferred for it now. If there is a digit, we need to continue the transfer, but we still don't know where to transfer it, if there is another dot, it also needs to be transferred because (. 0) it is also a legal number, but I still don't know what status can be changed. If an eE is put, it is eE At the beginning. It is invalid because there must be a number before eE, so at this time, make sure to transfer to-1, that is, false. The following figure is used to mark: status 0: invalid->-1 space-> 0 sign->? -----> After status 1 is considered, it changes to sign-> 1 digit->? -----> After status 2 is considered, it changes to digit-> 2 dot->? ------> After status 3 is considered, the status changes to "dot-> 3 eE->-1". Because there is a question mark on it, we need to create a new status to let the question mark return, until all the statuses are recovered. So we should first sign->? This question mark is a status 1, so status 1 refers to the status from the +-sign, so there are: Status 1 :( previously it is a positive or negative sign) invalid->-1 // The invalid character must be-1 space->-1 // the plus and minus spaces certainly do not match, decisive-1 sign->-1 // The symbol cannot be signed,-1 digit->? // Add a positive or negative number to the dot->? // The plus and minus signs can also be transferred. eE->-1 // It is illegal to directly add the index eE to the positive and minus signs.-1 is here, although status 1 also has a question mark, however, the question mark of sign in status 0 can be replaced by the red part. Similarly, now we need to consider status 1 in digit->? The question mark, give it a state 2, then State 2 refers to the previous normal number is transferred, General refers to the previous no point, no eE. So status 2 can be expressed as: status 2 :( previously a normal number) invalid->-1 // The invalid character must be-1 space->? // It is a number before, because it is valid to end with a space, so we need to jump, but we do not have this status, first put the sign->-1 // number cannot be signed, -1 digit-> 2 // after the number is still a number, this is still a common number, so transfer to itself, the record is 2, if this ends, it must be legal dot->? // After the number is a vertex, you can continue to transfer eE->? // Add the number and the eE to continue the transfer, because it may be legal. At this point, we know that if the final state stays at 2, it is legal. Because only common numbers that can be jumped to 2 are valid. The question mark in status 0 can also be updated. For details, see the purple part in status 0. We complete the question mark in the order of one status and one status. The next step is to give the dot-> 3 of the status 0, and the status 3 is status 3: (the previous step is skipped) invalid->-1 // The invalid character must be-1 space->-1 // The space after the dot is invalid sign->-1 // The digit after the dot is invalid digit->? // After the vertex number, the dot->-1 // The vertex after the vertex is invalid eE->-1 // The index is invalid after only one vertex, if it is a numeric plus an index, it is valid, but here it is only a vertex, so it is invalid. Now, status 0 is a success. Status 0: invalid->-1 space-> 0 sign-> 1 digit-> 2 dot-> 3 eE->-1, here, you should solve the problem from the question mark of status 1. Take a sheet of paper and draw a picture. Upload a picture I drew: When status 2 is in the middle, the jump is wrong and the code is changed after debugging. So your status may be different from mine, but as long as it can be AC. It indicates that if the status ends, it is a valid number. Otherwise, it is invalid. The Code is as follows: copy the code class Solution {public: bool isNumber (const char * s) {enum InputType {invalid, space, sign, digit, dot, eE, len }; int trans [] [len] = {-1, 0, 1, 2, 3,-1,-1,-1,-1, 2, 3,-1, -1, 4,-1, 2, 6, 5,-1,-1,-1, 6,-1,-1,-1, 4,-1, -1,-1,-1,-1,-1,-1, 7, 8,-1,-1,-1, 4,-1, 6,-1, 5, -1,-1,-1, 8,-1,-1,-1, 4,-1, 8,-1,-1,}; int state = 0; while (* s! = '\ 0') {InputType inputType = invalid; if (* s = '') inputType = space; else if (* s = '+' | * s = '-') inputType = sign; else if (isdigit (* s) inputType = digit; else if (* s = '. ') inputType = dot; else if (* s = 'E' | * s = 'E') inputType = eE; state = trans [state] [inputType]; if (state =-1) return false; s ++ ;} return state = 2 | state = 4 | state = 6 | state = 8 ;}};