1 Star 0 Fork 0

lu / 力扣

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
整数反转.html 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
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>
// 输入:x = 123
// 输出:321
// 示例 2:
// 输入:x = -123
// 输出:-321
// 示例 3:
// 输入:x = 120
// 输出:21
// 示例 4:
// 输入:x = 0
// 输出:0
/**
* @param {number} x
* @return {number}
*/
//用取模 取出最后一位
// 除10取整 保证 取到最后一位
//结果 * 10 实现反转
//整数溢出 是 2的31次方
var reverse = function (x) {
let res = 0;
while (x != 0) {
let num = x % 10;
x = parseInt(x / 10);
res = res * 10 + num;
}
if (res < Math.pow(-2, 31) || res > Math.pow(2, 31) - 1) {
return 0;
}
return res;
};
console.log(reverse(1534236469));
</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

搜索帮助