Skip to content

8. 字符串转换整数 ​

  • 题号:8
  • 来源:LeetCode
  • 难度:中等
  • 标签:字符串 模拟
  • 语言:TypeScript
  • 解法:1 个
  • 作者:lmliheng
  • 最近更新:2026-09-27

TypeScript · O(n) 时间 · O(n) 空间 · 更新于 2026-09-27

手写解析,去空格、判符号、截数字后按边界截断

typescript
/**
 * @difficulty medium
 * @tags 字符串,模拟
 * @time O(n)
 * @space O(n)
 * @note 手写解析,去空格、判符号、截数字后按边界截断
 * 8. 字符串转换整数 (atoi)
 * 
 */

function myAtoi(s: string) {

    // 记录前置空格数目
    let font_space = 0
    // 首字母判断正负号
    let isPositive = false;
    let isNegative = false;
    // 首字母是否为数字
    let isNumber = true;
    // 处理到这了,记作foot尾声阶段
    // 记录前置0的数目
    let foot_font_zero = 0
    // 记录前置空格数目
    let foot_font_space = 0
    // 记录数字数目
    let foot_number_space = 0


    for (let i = 0; i < s.length; i++) {
        if (s[i] == ' ') {
            font_space += 1;
        } else {
            break
        }
    }


    let s_nofontspace = ''
    for (let i = font_space; i < s.length; i++) {
        s_nofontspace += s[i]
    }

    if (s_nofontspace[0] == '+' || s_nofontspace[0] == '-') {
        isPositive = s_nofontspace[0] == '+' ? true : false;
        isNegative = s_nofontspace[0] == '-' ? true : false;
        // 去除正负号
        s_nofontspace = s_nofontspace.slice(1);
    } else {
        isNumber = (s_nofontspace[0] >= '/' && s_nofontspace[0] <= ':') ? true : false;
    }

    // 去除0
    for (let i = 0; i < s_nofontspace.length; i++) {
        if (s_nofontspace[i] == '0') {
            foot_font_zero += 1;
        } else {
            break
        }
    }


    s_nofontspace = s_nofontspace.slice(foot_font_zero);

    // 记录数字数目
    for (let i = 0; i < s_nofontspace.length; i++) {
        if (s_nofontspace[i] >= '/' && s_nofontspace[i] <= ':') {
            foot_number_space += 1;
        } else {
            break
        }
    }
    s_nofontspace = s_nofontspace.slice(0, foot_number_space);

    if (isNumber == false) {
        return 0
    }

    let num_s = isPositive ? Number(s_nofontspace) : Number(s_nofontspace)
    num_s = isNegative ? -num_s : num_s
    num_s = Math.max(num_s, -2147483648)

    if (num_s > 2147483647) {
        num_s = 2147483647
    }
    return num_s

};

源码:ts/leetcode/8. 字符串转换整数.ts


在 GitHub 上查看题目所在目录:lmliheng/algorithm