Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

字符串转换整数 (atoi) #132

Open
sisterAn opened this issue Nov 26, 2020 · 7 comments
Open

字符串转换整数 (atoi) #132

sisterAn opened this issue Nov 26, 2020 · 7 comments

Comments

@sisterAn
Copy link
Owner

请你来实现一个 atoi 函数,使其能将字符串转换成整数。

首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。接下来的转化规则如下:

如果第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字字符组合起来,形成一个有符号整数。
假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成一个整数。
该字符串在有效的整数部分之后也可能会存在多余的字符,那么这些字符可以被忽略,它们对函数不应该造成影响。
注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换,即无法进行有效转换。

在任何情况下,若函数不能进行有效的转换时,请返回 0 。

提示:

本题中的空白字符只包括空格字符 ' ' 。
假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231,  231 − 1]。如果数值超过这个范围,请返回  INT_MAX (231 − 1) 或 INT_MIN (−231) 。

示例 1:

输入: "42"
输出: 42

示例 2:

输入: "   -42"
输出: -42
解释: 第一个非空白字符为 '-', 它是一个负号。
     我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。

示例 3:

输入: "4193 with words"
输出: 4193
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。

示例 4:

输入: "words and 987"
输出: 0
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
     因此无法执行有效的转换。

示例 5:

输入: "-91283472332"
输出: -2147483648
解释: 数字 "-91283472332" 超过 32 位有符号整数范围。 
     因此返回 INT_MIN (−231) 。

leetcode

@violetrosez
Copy link

暴力api

  function transStr2Int(str) {
            let arr = str.split(/\s+/);
            const INT_MAX = Math.pow(2, 31) - 1
            const INT_MIN = Math.pow(-2, 31)
            for (let index = 0; index < arr.length; index++) {
                if (arr[index] == "") continue
                if (isNaN(arr[index])) {
                    return 0
                }
                if (parseInt(arr[index]) > INT_MAX) {
                    return INT_MAX
                }
                if (parseInt(arr[index]) < INT_MIN) {
                    return INT_MIN
                }
                return parseInt(arr[index])
            }

        }

@sisterAn
Copy link
Owner Author

sisterAn commented Nov 29, 2020

解答:parseInt

parseInt(string, radix)   解析一个字符串并返回指定基数的十进制整数, radix 是2-36之间的整数,表示被解析字符串的基数。

如果 parseInt 遇到的字符不是指定 radix 参数中的数字,它将忽略该字符以及所有后续字符,并返回到该点为止已解析的整数值。 parseInt 将数字截断为整数值。 允许前导和尾随空格。

parseInt 可以理解两个符号。+ 表示正数,- 表示负数(从ECMAScript 1开始)。它是在去掉空格后作为解析的初始步骤进行的。如果没有找到符号,算法将进入下一步;否则,它将删除符号,并对字符串的其余部分进行数字解析。

如果第一个字符不能转换为数字,parseInt会返回 NaN

为了算术的目的,NaN 值不能作为任何 radix 的数字。你可以调用isNaN函数来确定parseInt的结果是否为 NaN。如果将NaN传递给算术运算,则运算结果也将是 NaN
-- 来自MDN

而题目的主要规则可以概况为:

  • 忽略开头空格
  • 忽略整数部分后的字符
  • 返回有符号整数
  • 范围在 32 位内
  • 函数不能进行有效的转换时,请返回 0

所有的条件 parseInt 都满足,除了

  • 范围在 32 位内(含)
  • 函数不能进行有效的转换:parseInt 返回的是 NaN

代码实现:

var myAtoi = function(s) {
    // parseInt
    const number = parseInt(s);
    
    // 判断 parseInt 的结果是否为 NaN,是则返回 0
    if(isNaN(number)) {
        return 0;
    } else if (number < Math.pow(-2, 31) || number > Math.pow(2, 31) - 1) {
        // 超出
        return number < Math.pow(-2, 31) ? Math.pow(-2, 31) : Math.pow(2, 31) - 1;
    } else {
        return number;
    }
}

非 parseInt 解答:

var myAtoi = function(s) {
  let res = 0
  s = s.trimStart()
  match = s.match(/[+|-]?\d*/)[0]
  if(match === '+' || match === '-' || match === '') {
  	return  0
  } 
  res = +match
  if(res > 2 ** 31 - 1) res = 2 ** 31 -1
  if(res <  -(2 ** 31)) res = -(2 ** 31)
  return res
}

@sunsmeil
Copy link

sunsmeil commented Dec 1, 2020

var myAtoi = function(s) {
    const INT_MAX = 2147483647    
    const INT_MIN = -2147483648
    const numberStr = s.trim().match(/^[\+\-]?\d+/g)
    if (!numberStr){
        return 0
    }else{
        return Math.max(Math.min(numberStr[0],INT_MAX),INT_MIN)
    }
};

@fxwing
Copy link

fxwing commented Dec 7, 2020

function atoi(str) {
  const intMin = Math.pow(2, -31);
  const intMax = Math.pow(2, 31) - 1;
  const val = parseInt(str);
  if (isNaN(val)) {
    return 0
  } else if (val <= intMin) {
    return intMin
  } else if (val >= intMax) {
    return intMax
  } else {
    return val
  }
}

@JohnApache
Copy link

JohnApache commented Apr 28, 2021

没有使用Js自带的 parseInt api, 实现了效果

const INT_MAX = Math.pow(2, 31) - 1;
const INT_MIN = -Math.pow(2, 31);

const atoi = (str: string) => {
    let i = 0;
    let char = str.charAt(i);

    while (char === ' ') {
        i++;
        char = str.charAt(i);
    }
    if (!(/[-,+,0-9]/).test(char)) return 0;

    // 第一个字符
    let numStr = char;
    i++;
    char = str.charAt(i);
    while (char) {
        if (!(/[0-9]/).test(char)) break;
        numStr += char;
        i++;
        char = str.charAt(i);
    }

    const len = numStr.length;
    let num = 0;
    for (let j = 0; j < len; j++) {
        const val  = numStr.charAt(len - j - 1);
        if (j === len - 1) {
            if (val === '+') break;
            if (val === '-') {
                num *= -1;
                break;
            }
        }
        num += +val * Math.pow(10, j);
    }

    if (num > INT_MAX) num = INT_MAX;
    if (num < INT_MIN) num = INT_MIN;
    return num;
};

console.log(atoi('   -42'));
console.log(atoi('42'));
console.log(atoi('4193 with words'));
console.log(atoi('-91283472332'));
//-42
//42
//4193
//-2147483648

@BlueOrgreen
Copy link

const atoi = (str) => {
const trimStr = str.replace(/(^\s*)|(\s*$)/g, '')
if(!/^[\d|-]/g.test(trimStr)) return 0
const matchStr = /(-)?\d+/g.exec(trimStr)
const result = parseInt(matchStr)
if(result < 0 && result < -(2 ** 31)) return 'INT_MIN (−231)'
if(result > 0 && result > 2 ** 31 - 1) return 'INT_MAX (231 − 1)'
return parseInt(matchStr)
}

@AlexZhang11
Copy link

function getMostNumber(str){
    let reg = /^[+-]?[0-9]*/
    str =  str.trimStart()
    let temp =str.match(reg)
    console.log(reg.exec(str),str.match(reg),temp,'temp')
    let max =Math.pow(2,31)
    let min  = -max-1
    if(temp<min){
        return min
    }
    if(temp>max){
        return max
    }
    if(!temp){
        return 0
    }
    return temp[0]
}

console.log(getMostNumber('42'))
console.log(getMostNumber("   -42"))
console.log(getMostNumber("4193 with words"))
console.log(getMostNumber("words and 987"))
console.log(getMostNumber("-91283472332"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants