整型反转
来源:互联网 发布:插补算法分为几种 编辑:程序博客网 时间:2024/06/16 07:05
题目:给定一个int值,将其反转输出
例子:
int a = 123 -> 321
int b = -123 -> -321
思路:
- 可以将整型转为字符,然后通过下标来反转,这是最容易想到的办法,但是逼格不够高。
- 通过对数值不停的模10,再*10,不断的相加
陷阱:
反转以后int值有可能会越界
代码:
public class Solution{ public int reverseInteger(int n){ //初始值为0 int temp = 0; while( n!= 0 ){ int res = n % 10; int temp = temp * 10 + res; n /= 10; if(temp%10 != res){ //说明越界了 return 0; } } return temp; }}
阅读全文
0 0
- 整型反转
- reverse-integer(反转整型)
- 数组反转(整型数组)
- 整型反转 如-123反转成-321
- Reverse Integer(反转整型数)
- 7. Reverse Integer(反转整型) —— Java
- 整型
- 整型
- 整型
- 整型
- 整型
- 整型
- 整型
- 反转
- 整型数组
- 整型(int)
- 整型常识
- 整型提升
- 我的学习记录34
- struts国际化
- Eclipse创建Maven Web项目
- 显式锁(三) -- 读写锁ReadWriteLock
- 吸血鬼数(Java实现)
- 整型反转
- 如何让textarea中输入多行的数据在p标签中换行?
- Python-json
- 关于虚拟化(virtualization)的一些知识
- FFmpeg之AVCodec
- IntentFilter的匹配规则
- 机器学习&数据挖掘入门1--环境搭建
- Javaoop2
- Github+Hexo搭建个人Blog