1 Star 0 Fork 0

lu / 力扣

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
字符串转整数.html 1018 Bytes
一键复制 编辑 原始数据 按行查看 历史
CN-njlupingshang01 提交于 2023-07-17 13:56 . 字符串 整数 相互转换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body></body>
<script>
// 输入:s = " -42"
// 输出:-42
// 输入:s = "4193 with words"
// 输出:4193
//1.判断是否为" "空字符串 2.判断+ -号 3.判断是否为数字
var myAtoi = function (s) {
let res = 0;
let len = s.length;
let sy = 1;
for (let i = 0; i < len; i++) {
let d = s.charAt(i);
let r = d - "0";
if (d == "+" || d == "-") {
sy = d == "-" ? -1 : 1;
}
if (d !== " " && (r >= 0 || r <= 9)) {
res = res * 10 + r
}
}
if (res < Math.pow(-2, 31) || res > Math.pow(2, 31) - 1) {
return 0;
}
return res * sy;
};
console.log(myAtoi(" -42 dsd "));
</script>
</html>
1
https://gitee.com/ma-nong-hong-qigong/likou.git
git@gitee.com:ma-nong-hong-qigong/likou.git
ma-nong-hong-qigong
likou
力扣
master

搜索帮助