LeetCode--007--整数反转(java)
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123
输出: 321
示例 2:
输入: -123
输出: -321
示例 3:
输入: 120
输出: 21
解法烂的一批,直接看第二个吧
class Solution {
public int reverse(int x) {
boolean isNeg = false;
if(x<0){
x = Math.abs(x);
isNeg = true;
}
String s = String.valueOf(x);
Stack<Character> stack= new Stack();
for(int i=0;i<s.length();i++){
stack.push(s.charAt(i));
}
s="";
while(!stack.isEmpty()){
s+=stack.pop();
}
int res=0;
try{
res = Integer.parseInt(s);
}catch(Exception e){
System.out.println(0);
return 0;
}
if(isNeg){
res = -1 * res;
}
System.out.println(res);
return res;
}
}
方法2:
int from -2147483648 to 2147483647
为了避免反转越界,指定res为long类型。
class Solution {
public int reverse(int x) {
long res = 0;
while(x != 0){
res = res * 10 + x % 10;
x = x / 10;
if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) return 0;
}
return (int)res;
}
}
LeetCode--007--整数反转(java)的更多相关文章
- 前端与算法 leetcode 7. 整数反转
目录 # 前端与算法 leetcode 7. 整数反转 题目描述 概要 提示 解析 解法 算法 传入测试用例的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 7. 整数反转 题 ...
- Java实现 LeetCode 7整数反转
7. 整数反转 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: ...
- 详解 LeetCode_007_整数反转(Java 实现)
目录 LeetCode_007_整数反转 题目描述 总体分析 解决方案 小结 LeetCode_007_整数反转 题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示 ...
- [LeetCode]7. 整数反转(Java)
原题地址: reverse-integer 题目描述: 给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果. 如果反转后整数超过 32 位的有符号整数的范围 [−2^31, ...
- Leetcode刷题——007.整数反转
上代码: #include <cmath> class Solution { public: int reverse(int x) { ; long long tx=llabs(x); ) ...
- Leetcode 7. 整数反转(待整理)
1.题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: ...
- 【LeetCode】整数反转【不能借助辅助空间,需要处理溢出】
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: ...
- 【一起刷LeetCode】整数反转
前言&絮叨 别人都忙着参加年会晒奖品,我却忙着写代码.每逢年底都要安排几个紧急项目,我什么时候能摆脱这种宿命. 在忙也不能忘记刷LeetCode,毛毛向前冲!!! 题目描述 给出一个 32 位 ...
- [LeetCode] 7. 整数反转
题目链接:https://leetcode-cn.com/problems/reverse-integer/ 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 ...
- LeetCode:整数反转(Reserve Integer)
public class ReserveInteger { public int reverse(int x) { //用于接收个位数(10的余数) int remainder; //是否负数 int ...
随机推荐
- openwrt源码下的feeds.conf.default有何作用?
答: 可以往openwrt下添加一些最新的软件包,这样make menuconfig后就可以发现很多软件包了. 该文件中的内容格式如下例: src-git packages https://git.l ...
- newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解
题目描述 筱玛是个快乐的男孩子. 寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险. 迷阵可以看做一个n×nn×n的矩阵A,每个格子上有一个有一个数Ai,j. 入口在左上角的(1,1)处,出口在右下 ...
- C++中的string常用函数用法
标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而 ...
- Optical Flow related Tutorials
Optical Flow related Tutorials 2017-04-01 10:50:55 Reference: 1. http://blog.csdn.net/carson2005/art ...
- 【ASP.NET】System.Web.Routing - RouteCollection Class
Provides a collection of routes for ASP.NET routing. The RouteCollection class provides methods that ...
- Ubuntu14.04下 安装p4c
参考: Github p4c README Ubuntu14.04下 安装p4c 这里提供一个直接安装p4c的脚本:install_p4c.sh. 1.git clone下来p4c: $ git cl ...
- jquery选择器扩展之样式选择器
https://github.com/wendux/style-selector-jQuery-plugin http://blog.csdn.net/duwen90/article/details/ ...
- mybatis(错误一) 项目启动时报“Result Maps collection already contains value forxxx”的解决方案
Result Maps collection already contains value for xyx.dsw.dao.mapper.admin.quotationwish.TempTestTab ...
- 【Django】【问题集锦】
参考:http://www.linuxidc.com/Linux/2013-03/80649.htm 1. Django的shell模式下,如果报warning,则再执行一次,也许就好了 2. Dja ...
- 转 代码修改buildoption
using System;using System.IO;using UnityEditor;using UnityEngine; public class BuildPlayer : Scripta ...