LeetCode 7. Reverse Integer (JS)
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321Example 2:
Input: -123
Output: -321Example 3:
Input: 120
Output: 21Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
周二太忙了...没时间刷题...趁着周三中午有时间赶忙刷一波
先Po代码:
var reverse = function(x) {
var sum = 0,
flag = 0;
const MAXINIT = Math.abs((1<<31)-1);
if (x < 0) {
x = -x;
flag = 1;
}
while (x > 0) {
sum = sum * 10 + x % 10;
x = Math.floor(x / 10);
}
if (sum > MAXINIT) {
sum = 0;
}
return flag ? -sum : sum;
};
题解:
一开始真真没考虑到32位int溢出的问题...
然后WA了如下错误:
Input:
1534236469
Output:
9646324351
Expected:
0
即,如果溢出,那么返回0
由于我这里使用的是绝对值,所以不需要考虑下溢,int32位最大值,即2的31次方-1,(因为如果对任何数字做位运算,JS都会将其转为32位有符号整形,而1<<31,把1移到了符号位,使其为-2147483648)
这个问题自己研究了一下,发现了几点
(1<<31)-1; //首先,进行位运算要带括号,不然优先计算31-1=30,再1<<30;
1<<31;//-2147483648,因为转为了有符号整形,所以1<<31位溢出,把1移到了符号位。
以上,这个题还是比较easy的......(自己本来也就刷的是easy题,囧)
收获就是JS位运算的相关知识了......
LeetCode 7. Reverse Integer (JS)的更多相关文章
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
随机推荐
- 如何用scanf读入一个string
#include <stdio.h> #include <string> using namespace std; int main() { string a; a.resiz ...
- 【02】一个实现h5的拖放的整个过程-魔芋
[02]拖放的整个过程-魔芋 01,创建2个元素,一个为拖放元素dragEle,一个是存放的元素targetEle.添加一些样式. <div class="dragEle&qu ...
- luoguT21778 过年
差分一下上线段树 #include <iostream> #include <cstdio> #include <vector> using namespace s ...
- HDU1754-I Hate It,线段数水题~~2016.4.11
I Hate It ...
- oracle如何向空表中添加一个类型为clob的非空列
一般的添加非空列的步骤是:先add可以为空的列,然后update该列为一个值(比如0),最后modify该列的类型 但是遇到类型为clob的就不行了.在modify这步时报错:ORA-22296:in ...
- 禁止ScrollView在子控件的布局改变时自动滚动的的方法
重写scrollview中的如下方法,并将其返回值设为0即可. @Override protected int computeScrollDeltaToGetChildRectOnScreen(Re ...
- 【BZOJ1717&POJ3261】Milk Patterns(后缀数组,二分)
题意:求字符串的可重叠的k次最长重复子串 n<=20000 a[i]<=1000000 思路:后缀数组+二分答案x,根据height分组,每组之间的height>=x 因为可以重叠, ...
- 深入理解计算机操作系统——第11章:全球IP英特网
全球IP英特网 (1)每台英特网主机都运行实现TCPIP协议的软件. (2)英特网的客户端和服务器混合使用套接字接口函数和Unix IO函数来进行通信. (3)套接字函数典型的是作为陷入内核的系统调用 ...
- windows 下安装Apache httpd 只需三步
1.下载 Apache 官网地址:http://httpd.apache.org/docs/current/platform/windows.html#down 找到这个, 看到这几个选项: Apac ...
- windows7 下安装使用Redis
Redis 安装使用 本地环境:Windows7 64位web环境:wamp集成环境,php版本:PHP Version 7.1.17 学习参考网站: RUNOOB.COM官网 http://www ...