Leetcode record-string to integer

Source: Internet
Author: User

Tag: Record leetcode EOF. Math cannot be shared based on the turn result.

ImplementationatoiTo convert the string into an integer.

This function first discards any number of space characters as needed until the first non-space character is found. If the first non-null character is a positive or negative sign, select the symbol and combine it with as many consecutive numbers as possible. This character is the value of an integer. If the first non-null character is a number, it is directly combined with the subsequent consecutive numeric characters to form an integer.

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

If the first non-null character sequence in a string is not a valid integer, or the string is empty, or the string contains only blank characters, no conversion is performed.

If the function cannot perform a valid conversion, 0 is returned.

Note:

Assume that our environment can only store 32-bit signed integers. The value range is [? 231,231? 1]. If the value exceeds the value range that can be expressed, int_max (231? 1) or int_min (? 231 ).

Example 1:

Input: "42" output: 42

Example 2:

Input: "-42" output:-42 explanation: the first non-blank character is '-', which is a negative number. We try our best to combine the negative number with all the numbers that appear consecutively, and finally get-42.

Example 3:

Input: "4193 with words" output: 4193 explanation: the conversion ends with the number '3' because its next character is not a number.

Example 4:

Input: "Words and 987" output: 0 explanation: the first non-null character is 'w', but it is not a number or positive or negative number. Therefore, a valid conversion cannot be performed.

Example 5:

Input: "-91283472332" output:-2147483648 explanation: the number "-91283472332" exceeds the 32-Bit Signed Integer Range. Therefore, int_min (? 231 ).


Answer:
Import Java. math. biginteger; Class solution {public int myatoi (string Str) {STR = Str. trim (); stringbuilder sb = new stringbuilder (); For (INT I = 0; I <Str. length (); I ++) {char c = Str. charat (I); // filter invalid 0 if (sb. length () = 0 & C = 48) {continue;} if (I = 0) {If (C = '-' | C = '+' | (C> = 48 & C <= 57) {sb. append (c) ;}else {break ;}} else {If (C >=48 & C <= 57) {sb. append (c) ;}else {break ;}} if ("". equals (sb. tostring () | "-". equals (sb. tostring () | "+ ". equals (sb. tostring () {return 0;} int result; try {result = integer. valueof (sb. tostring ();} catch (numberformatexception e) {If (sb. tostring (). startswith ("-") {return integer. min_value;} else {return integer. max_value ;}} return result ;}}

 

Submission result:

Leetcode record-string to integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.