题目:Reverse Integer

难度:Easy

题目内容

Given a 32-bit signed integer, reverse digits of an integer.

Note:
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.

翻译:给定一个32位签名整数,一个整数的反向数字。

注意:

假设我们正在处理一个只能容纳32位的整形数值。出于这个问题的目的,当反转整数溢位时函数返回0。

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

我的思路:用List将数值从低位依次取出到高位,用list的第一个记录此整数的符号。最后输出的时候用一个boolean来判断是否前面全为零,然后跳过此零。

我的代码

     public int reverse(int x) {
List<Integer> ans = new ArrayList<Integer>();
if (x < 0) {
ans.add(0);
x = -x;
} else if (x > 0) {
ans.add(1);
} else {
return 0;
}
while (x != 0) {
ans.add(x%10);
x = x / 10;
}
int y = 0;
boolean tag = false;
for (int i = 1;i < ans.size(); i++) {
if (tag == false && ans.get(i) == 0) {
continue;
} else {
tag = true;
}
y += ans.get(i);
if (i < ans.size() - 1) y *= 10;
}
y = ans.get(0) == 1 ? y : -y;
return y;
}

结果:1027 / 1032 test cases passed.

Input:1534236469
Output:1056389759
Expected:0
 
意思是反转之后需要判断是否溢出。弄了半天都没思绪,因为反转后一位一位乘以10后就直接溢出然后变为另外一个值,不好比较是否越界。。。。
原谅我这个笨脑阔吧

LeetCode第[7]题(Java):Reverse Integer 标签:数学的更多相关文章

  1. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第[15]题(Java):3Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  3. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

  4. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  6. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  7. leetCode练题——7. Reverse Integer

    1.题目:   7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...

  8. LeetCode专题-Python实现之第7题:Reverse Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

随机推荐

  1. 穿透Session 0 隔离(一)

    服务(Service)对于大家来说一定不会陌生,它是Windows 操作系统重要的组成部分.我们可以把服务想像成一种特殊的应用程序,它随系统的“开启-关闭”而“开始-停止”其工作内容,在这期间无需任何 ...

  2. 在Web工程中,普通Java类如何读文件

    我们在以前讨论过在Web工程不能采用Java工程原先的读取方式,即创建一个文件流(FileInputStream)并给出一个文件目录(从src开始找一直找到你要读取文件的目录),这种的方式是不可行的. ...

  3. Powershell&.NET数值取整处理

    如何取一个数的整数值? 使用类型强制转换 Powershell的强制转换有2种方式,一种是直接类型强制转换,另一种是通过-as运算符进行转换 PS F:\> [int] (3 / 2) # 直接 ...

  4. LinkedList基本用法

    https://blog.csdn.net/i_lovefish/article/details/8042883

  5. MySQL(多表的表记录的查询)

    一.外键约束 1.创建外键 --- 每一个班主任会对应多个学生 , 而每个学生只能对应一个班主任 ----主表 CREATE TABLE ClassCharger( id TINYINT PRIMAR ...

  6. Angular学习笔记—基础(转载)

    创建简单组件 新建组件 $ ng generate component simple-form --inline-template --inline-style # Or $ ng g c simpl ...

  7. Jenkins+maven+Tomcat+SVN一键自动打包部署应用到服务器

    今天请教了大神,终于把jenkins给搞明白了 现在做下笔记,防止自己老年痴呆又忘了怎么配置 (截图可能不够清晰,有不清楚的随时评论打call) 机器配置: 安装配置规划 机器 192.168.169 ...

  8. likely(x)与unlikely(x) __builtin_expect

    本文讲的likely()和unlikely()两个宏,在linux内核代码和一些应用中可常见到它们的身影.实质上,这两个宏是关于GCC编译器内置宏__builtin_expect的使用. 顾名思义,l ...

  9. appium API java

    原创内容,未经允许,禁止转载! driver.close();//关闭 driver.closeApp();//关闭应用,其实就是按home键把应用置于后台 driver.currentActivit ...

  10. 第六课 GDB调试 (上)

    1序言: 1.初学者经过学习前面的Makefile知识,信心满满,内心觉得应该要好好学习不单掌握语言的编写,也要学会相对应的工具调高开发效率.有时我们写出来的代码经过执行结果却跟我们预期不一样那怎么办 ...