[LeetCode-interview algorithm classic-Java implementation] [008-String to Integer (atoi) (String to Integer)], leetcode -- java
[008-String to Integer (atoi) (String to Integer )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Theme
Implements an atoi function to convert a string into an integer.
Key Point: Consider all input conditions.
Solutions
The leading character is + or-or does not exist. The subsequent input is a number. The number cannot be an integer that represents the maximum or minimum number. If the value is exceeded, the minimum or minimum value is returned.
Code Implementation
Public class Solution {public int atoi (String str) {if (str = null | str. length () = 0) {// throw new NumberFormatException ("Invalid input string:" + str); return 0 ;}// if the string starts with a space, int start = 0; // find the first digit not a space from the start. boolean positive = true; // whether the number is positive is true by default if (str. charAt (start) = '') {while (str. charAt (start) = '') {start ++; if (start> = str. length () {// all input values are spaces // throw new NumberFormatException ("Invalid input string:" + str); return 0 ;}} if (str. charAt (start) = '-') {// among the first non-blank characters,-positive = false; start ++;} else if (str. charAt (start) = '+') {// the first non-blank character is + start ++;} else if (str. charAt (start)> = '0' & str. charAt (start) <= '9') {// the first non-blank character is the number return cal (str, start, true );} else {// exception thrown in other cases // throw new NumberFormatException ("Invalid input string:" + str); return 0;} if (start> = str. length () {// the first non-blank character is + or-but also the last character // throw new NumberFormatException ("Invalid input string:" + str ); return 0;} if (str. charAt (start)> '9' | str. charAt (start) <'0') {// + or-is not followed by a number // throw new NumberFormatException ("Invalid input string:" + str); return 0 ;} else {return cal (str, start, positive) ;}} private int cal (String str, int start, boolean positive) {long result = 0; while (start <str. length () & str. charAt (start)> = '0' & str. charAt (start) <= '9') {result = result * 10 + (str. charAt (start)-'0'); if (positive) {// if it is a positive number if (result> Integer. MAX_VALUE) {// throw new NumberFormatException ("Invalid input string:" + str); return Integer. MAX_VALUE ;}} else {if (-result <Integer. MIN_VALUE) {// throw new NumberFormatException ("Invalid input string:" + str); return Integer. MIN_VALUE ;}} start ++;} if (positive) {return (int) result ;}else {return (int)-result ;}}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46938.pdf]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.