# Problem 8: String to Integer (atoi)

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

## 思路

* 题目不难，但是考虑的东西很多：
* 首位的空白
* “+/-” 首位是否是符号，首位如果有符号，后面再有的话就要报错了
* 是否越界

```java
public class Solution {
    public int myAtoi(String str) {
        if (str == null || str.length() == 0) {
            return 0;
        }

        str = str.trim();
        boolean isNegative = false;
        int pos = 0;
        if (str.charAt(0) == '+') {
            pos++;
        }
        if (str.charAt(0) == '-') {
            isNegative = true;
            pos++;
        }


        double rst = 0;
        while (pos < str.length() && str.charAt(pos) == '0') pos++;
        while (pos < str.length()) {
            char c = str.charAt(pos);
            if (c < '0' || c > '9') {
                break;
            }

            rst = 10 * rst + (c - '0'); 
            pos++;
        }
        if (isNegative) {
            rst = -rst;
        }

        if (rst > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        }
        if (rst < Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        }

        return (int) rst;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/chapter_2_string/problem_8_string_to_integer_atoi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
