Reverse Interger
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.
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?
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).
分析:我们可以从余数和除数方面考虑,我们一般会想到怎么样才能得到给定的数各个位上的数字,我们就可以用除10取余。知道除数为0为止。
- class Solution {
- public:
- int reverse(int x) {
- int res=;
- while(x!=)
- {
- res=res*+x%;
- x=x/;
- }
- return res;
- }
- };
python:
- class Solution:
- # @return an integer
- def reverse(self, x):
- flag=False
- res=0
- if x<0:
- x=-x
- flag=True
- while x>0:
- res=res*10+x%10
- x=x/10
- if flag:
- res=-res
- return res
Reverse Interger的更多相关文章
- Leetcode--easy系列1
近期開始刷Leetcode题目.花了一个星期先完毕了easy难度级别的题目,easy级别的题目思路比較简单,但不一定就直接AC了,主要是问题要考虑全然.特别是对特殊情况的处理. #6 ZigZag C ...
- HDOJ 1062 Text Reverse
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu 1062 Text Reverse 字符串
Text Reverse Time L ...
- HDOJ/HDU 1062 Text Reverse(字符串翻转~)
Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...
- HDU1062:Text Reverse
Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...
- Problem : (1.2.1) Text Reverse
#include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...
- (reverse) Text Reverse hdu1062
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDUOJ--------Text Reverse
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU——1062Text Reverse(水题string::find系列+reverse)
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- 根据div 标签 查看数组@class=modulwrap 下面的/table/tbody/tr/td
<div class="modulwrap"> <div class="request_title"> <span class=& ...
- If one session has a shared or exclusive lock on record R in an index, another session cannot insert
If one session has a shared or exclusive lock on record R in an index, another session cannot insert ...
- hdu-4612-Warm up(边双连通分量--有重边)
题意:有N 个点,M条边,加一条边,求割边最少.(有重边) 分析:先求双连通分量,缩点形成一个生成树,然后求这个的直径,割边-直径即是答案 因为有的图上可能有重边,这样不好处理.我们记录每条边的标号( ...
- 使用sklearn进行数据预处理 —— 归一化/标准化/正则化
一.标准化(Z-Score),或者去除均值和方差缩放 公式为:(X-mean)/std 计算时对每个属性/每列分别进行. 将数据按期属性(按列进行)减去其均值,并除以其方差.得到的结果是,对于每个属 ...
- cf602B Approximating a Constant Range
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
- cf602A Two Bases
A. Two Bases time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- AngularJS测试二 jasmine测试路由 控制器 过滤器 事件 服务
测试应用 1.测试路由 我们需要检测路由是否在运作,是否找到了,或者是404了.我们要确认路由事件触发了,预期的模板是否真的加载了.既然路由会改变页面的地址(URL)和页面内容,我们需要检测路由是否被 ...
- LINUX curl GET 掉参数解决办法
LINUX curl GET 掉参数解决方法 url 为 http://mywebsite.com/index.php?a=1&b=2&c=3web形式下访问url地址,使用 $_GE ...
- Java 二分查找
public int binarySearch(int[] nums, int target) { int low = 0; int high = nums.length; while (low &l ...
- [置顶] [Android源码分析]inquiry result引起的上层变化分析
在上一篇文章中,我们详细分析了android是如何解析蓝牙反馈上来的搜索到的设备信息,本文将会继续分析这些信息到了上层之后是如何处理. 8.inquiry result引起的上层变化 我们知道inqu ...