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. 【HIHOCODER 1604】股票价格II(堆)

    描述 小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN. 在小Hi的策略中,每天可以在下列三种操作中选取一种: 1.什么也不做: 2.按照当天的价格买进一个单位的 ...

  2. POJ 1949 Chores(DAG上的最长路 , DP)

    题意: 给定n项任务, 每项任务的完成用时t和完成每项任务前需要的k项任务, 求把所有任务完成的最短时间,有当前时间多项任务都可完成, 那么可以同时进行. 分析: 这题关键就是每项任务都会有先决条件, ...

  3. LeetCode01--寻找两个有序数组的中位数

    ''' 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nu ...

  4. MySQL之federated

    由于夸服务器查询的限制,federated能够使得所有的表像是在同一台服务器上查询 (show engines-->no-->在my.ini里面添加fedrated) 经过测试,在开启fe ...

  5. Android开发——遍历读写U盘、SD卡等外部存储

    1.首先需要得到挂载在手机上的有哪些盘符 String[] result = null; StorageManager storageManager = (StorageManager)getSyst ...

  6. 活动预告丨易盾CTO朱浩齐将出席2018 AIIA大会,分享《人工智能在内容安全的应用实践》

    本文来自网易云社区 对于很多人来讲,仿佛昨天才燃起来的人工智能之火,转眼间烧遍了各个角落,如今我们的生活中,处处渗透着人工智能.10月16日,2018年 AIIA人工智能开发者大会在苏州举办,网易云易 ...

  7. Fidder详解-抓取HTTPS清求(Web/App)抓包分析(靠谱篇)

    为什么要学Fidder抓包? 学习接口,必须要学http协议,不要求您对协议的掌握有多深.只是希望你能够了解什么是协议.协议的报文.状态码等等!本文通过抓包工具Fidder带你进入接口的大门.我们通过 ...

  8. 大数据学习——securecrt同时向多个tab窗口发送相同的命令

    右键选中 然后在下面空白窗口写命令就可以了

  9. LR手动关联参数化问题总结

    所谓的关联就是把脚本中某些写死的代码(hard-coded)数据,转变成截取自服务器所送的.动态的.每次都不一样的数据. 一般情况下,比较聪明的服务器在每个浏览器第一次跟它要数据时,都会在数据中夹带一 ...

  10. CodeForces 22、23部分题解

    CodeForces 22A 找严格第二小的...注意只有一种情况,可以sort排序然后unique输出. int a[N]; int main() { int n; while(~scanf(&qu ...