Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:

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)的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  3. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  4. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  5. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  6. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

  7. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  8. LeetCode 7. Reverse Integer (倒转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  9. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

随机推荐

  1. POJ 2377 (并查集+sort求最远路)

    Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...

  2. kafka flumn sparkstreaming java实现监听文件夹内容保存到Phoenix中

    ps:具体Kafka Flumn SparkStreaming的使用  参考前几篇博客 2.4.6.4.1 配置启动Kafka (1) 在slave机器上配置broker 1) 点击CDH上的kafk ...

  3. Leetcode 241.为运算表达式设计优先级

    为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...

  4. Laya Tween循环

    Laya Tween循环 @author ixenos 需求:做一个循环的缓动动画 方案: 1)如果只是线性变化,那么直接使用timer或者frameLoop来变化 2)如果需要有非线性变化,那么使用 ...

  5. [luoguP1169] [ZJOI2007]棋盘制作(单调栈)

    传送门 和玉蟾宫差不多 ——代码 #include <cstdio> #include <iostream> using namespace std; ; int n, m, ...

  6. [Usaco2006 Nov] Fence Repair 切割木板

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1356  Solved: 714[Submit][Status][Discuss] Description ...

  7. vs2010 静态使用 opencv 2.46 库

    下载opencv2.46的库,假设解压到OpenCV246,设置如下: 在工程的c++的include目录下添加:OpenCV246\opencv\build\include 在工程的c++的lib目 ...

  8. Codeforces 549C(博弈)

    C. The Game Of Parity time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. webstorm初始化

    1.皮肤设置,重启后Terminal皮肤生效 2.排除目录 2.1全局排除 2.2局部排除 选中文件夹 右击Make Directroy As 选择 Excluded 3.代码自定义 3.1 cons ...

  10. Fractal---POJ2083(dfs)

    http://poj.org/problem?id=2083 一道我认为有点恶心的dfs       刚开始没有初始化  用G++交  一直TLE  后来用了C++竟然是wa   那这肯定是我的问题了 ...