原题:
Reverse digits of an integer.
=>反转一个整数的数字。例子如下:
Example1: x = 123, return 321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
=>在做题的时候请仔细思考一下下面这些方面。
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
=>假如最后一位是0,那么结果会是什么样的呢?比如10,100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
=>你有考虑过反转后的溢出问题嘛?假如是32bit的整数,1000000003反转后就会溢出。你怎么处理这样的情况?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
=>抛出一个异常?很好,假如不能抛出异常呢?其实可以重新定义这个函数(比如加一个参数)
class Solution {
public:
int reverse(int x) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
};
晓东分析:
这个题目就反转本身而言是很简单的,晓东就不多分析了。所以,我主要来说一下溢出的问题,我个人的思路就是在得到反转的值的时候,先不乘上最高位,留着进行比较。
所以总的来说,还是不复杂的。
代码实现:
class Solution {
public:
int reverse(int x) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int max_first_bit = 2; //default for 4
int max_remain_num = 147483647;
int num = 0;
int temp = abs(x);
while(temp >= 10){
num = num * 10 + temp % 10;
temp /= 10;
}
switch(sizeof(int)){
case 1:
max_first_bit = 1;
max_remain_num = 27;
break;
case 2:
max_first_bit = 3;
max_remain_num = 2767;
break;
case 4:
max_first_bit = 2;
max_remain_num = 147483647;
break;
case 8:
max_first_bit = 9;
max_remain_num = 223372036854775807;
break;
}
if(x > 0){
if (temp < max_first_bit)
return num * 10 + temp % 10;
else if(num <= max_remain_num)
return num * 10 + temp % 10;
else
throw x;
}else{
if (temp < max_first_bit)
return 0 - (num * 10 + temp % 10);
else if(num <= max_remain_num + 1)
return 0 - (num * 10 + temp % 10);
else
throw x;
}
}
};
执行结果:
1020 / 1020 test cases passed.
|
Status:
Accepted
|
Runtime: 28 ms
|
希望大家有更好的算法能够提出来,不甚感谢。
若您觉得该文章对您有帮助,请在下面用鼠标轻轻按一下“顶”,哈哈~~·
分享到:
相关推荐
位有符号整数,反转整数的数字。 注意:假设我们正在处理的环境只能存储 32 位有符号整数范围内的整数:[−231, 231 − 1]。 出于此问题的目的,假设您的函数在反转整数溢出时返回 0。 示例 1: Input: 123 Output: ...
第321题Reversed_Integer Java中的LeetCode逆整数问题解决方案 问题 - 给定一个有符号的 32 位整数 x,返回 x 其数字颠倒。 如果反转 x 导致值超出有符号的 32 位整数范围 [-231, 231 - 1],则返回 0。 假设环境不...
整数反转 9 Palindrome Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove ...
整数反转 8. String to Integer (atoi) 字符串转换整数 (atoi) 9. Palindrome Number 回文数 10. Regular Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman ...
Leetcode\ReverseInteger\ReverseInteger.cs 问题: 业绩报告: 回文数 代码: Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中删除重复项 代码: Leetcode\RemoveDuplicates\RemoveDuplicates....
java lru leetcode Leetcode-Java Use Java to solve Leetcode&JianZhiOffer problems. Leetcode Num Title Difficulty Tag 1 Two ...Reverse Integer (整数反转) Easy Math 8 String to Integer (ato
整数反转 Reverse Integer.cpp 9 回文数 Palindrome Number.cpp 12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 ...
出于此问题的目的,假设您的函数在反转整数溢出时返回 0。 ##LRU 缓存设计并实现最近最少使用 (LRU) 缓存的数据结构。 它应该支持以下操作:get 和 set。 get(key) - 如果键存在于缓存中,则获取键的值(将始终为正...
#Reverse Integer & Palindrome Number 先将整数转为字符串:s=str(i) 然后使用 Python inbulit 方法对字符串进行反转:[::-1] 然后将字符串转为整数:i=int(s) 注意 Python 有Int 类型的默认范围:-2147483647 到 ...
Integer(整数反转) 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 Palindrome Number(回文数) 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数...
位整数的反转。 使用的技术- Python3。 来源- leetcode.com ->eq_world_map2.py : 此代码使用数据可视化库 - plotly 显示地震的 30 天数据。 数据源是eq_data_30_day_m1.json 。 使用的技术- Python3。 使用的模块...
LeetCode判断字符串是否循环 Leetcode刷题笔记 1. 题目描述 给定一个整数数组nums和一个目标值target,请你在该数组中找出和为目标值...代码:reverse_integer.py 字符串切片及翻转 >>> a = '0123456' >>> a[1:5] '123
400 题LeetCode solutions in Java 8 and Python3.NO.Title中文名SolutionNoteDifficultyTag0EasyMapping1MediumLinkedList2MediumMapping3Median...Java PythonNoteMedium6Reverse Integer反转整数Java PythonNoteE
./7_reverse_integer.c : 反转整数 ./string2int.c : 字符串到整数 ./search_number_appeared_more_than_half.c : 找出数组中出现次数超过了多少 ./152_maximum_product_subarray.c : 最大积子./152_maximum_product...
1. 将10进制正整数num转换为n进制 private String conversion(int num, int n) { MyStack<Integer> myStack = new MyArrayStack<Integer>(); Integer result = num; while (true) { // 将余数入栈 myStack....
数字反转(Reverse) 回文数(PalindromeNumer) 罗马数字转整数(RomanToInteger) 两数之和(sumInArray) 有序数组的平方(squaresOfASortedArray) 卡牌分组(xOfAKindInADeckOfCards) 排序数组去除重复项...
第 一 章 概述 1-1 简述计算机程序设计语言的发展历程。 解: 迄今为止计算机程序设计语言的发展经历了机器语言、汇编语言、高级语言等阶段,C++语言是一种面向对象的编程语言,也属于高级语言。...