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 ...
随机推荐
- 【】关闭QQ右下角各种弹框
[]关闭QQ右下角弹框 一: 二: 超级会员设置过滤(屏蔽)广告后可以过滤哪些广告? 1.可以过滤QQ客户端好友聊天对话框右侧出现的Flash广告.左下角的文案广告: 如图: 2.可以 ...
- python013 Python3 条件控制
Python3 条件控制Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块.可以通过下图来简单了解条件语句的执行过程: if 语句Python中if语句的一 ...
- 【multimap的应用】D. Array Division
http://codeforces.com/contest/808/problem/D #include<iostream> #include<cstdio> #include ...
- java中filter的用法
filter过滤器主要使用于前台向后台传递数据是的过滤操作.程度很简单就不说明了,直接给几个已经写好的代码: 一.使浏览器不缓存页面的过滤器 Java代码 import javax.servlet ...
- 字符串常量与const常量内存区(——选自陈皓的博客)
1. 一个常见的考点: char* p = "test"; 那么理利用指针p来改变字符串test的内容都是错误的非法的. 例如: p[0] = 's'; strcpy(p, &qu ...
- linux 报错:E: Package 'libmemcached' has no installation candidate
linux 报错:E: Package 'libmemcached' has no installation candidate 网上查资料说是软件安装源没有这个软件,需要添加软件源. 1.备份源列表 ...
- HDU 5360 【优先队列+贪心】
题意: 给定N个无序区间. 对合法区间的定义是: 在这个区间之前已经选出了至少l个合法区间,最多选出了r个合法区间.则该区间为合法区间. 输出最多能挑选出多少个合法区间,并输出合法区间的数量. 思路: ...
- Spring Boot使用Spring Data Redis操作Redis(单机/集群)
说明:Spring Boot简化了Spring Data Redis的引入,只要引入spring-boot-starter-data-redis之后会自动下载相应的Spring Data Redis和 ...
- Visual studio 2017 中的Javascript智能提示与调试
1.智能提示 对于JS文件中的API,你若需要让那个JS文件中的方法能够在你写的那个JS文件中能够智能显示的话,直接把它拉进你的JS文件中就好了. 举个例子:你想 在你正在写的a.js文件中引用b.j ...
- CheckStyle: 解决Unicode导致LineLength出错的问题
在checkstyle.xml中,加上如下代码: <?xml version="1.0" encoding="UTF-8"?> <module ...