题目: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. canvas基本使用

    1.什么是CANVAS canvas是html5新增的画布元素,可以通过javascript来在画布上绘制图形,图标以及任何视觉性的图像. 2.canvas的用途 替代flash,做各种动态效果,做小 ...

  2. Drupal 8 提供REST服务实例

    drupal8 的核心模块已经支持REST服务. 这样的话使用drupal 对外提供web service 变的简单了. 测试一下d8 的webservice : extend 中的 依赖模块:全部启 ...

  3. python将图片转base64,前端显示

    https://blog.csdn.net/u013055678/article/details/71406746 <img src='xxxxxxx'> 跟这样的效果是一样的吧?     ...

  4. python基础之类的isinstance与issubclass、反射

    一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo: pass o ...

  5. Visualizing mathematical functions by generating custom meshes using FireMonkey(很美)

    Abstract: This article discusses how you can generate your own 3-dimensional mesh for visualizing ma ...

  6. js中的整除运算

      Math.ceil(count / pagesize); //向上整除 4/3=2;   Math.floor(count / pagesize); //向下整除 4/3=1; Math.roun ...

  7. 我的Android进阶之旅------>Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换)

    Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换) 有时候要做出如下所示的展示文件大小的效果时候,需要对文件大小进行转换,然后再进行相关的代码逻辑编写. 下面是一个Java ...

  8. 算法分析之——heap-sort堆排序

    堆排序是一种原地排序算法,不使用额外的数组空间,运行时间为O(nlgn).本篇文章我们来介绍一下堆排序的实现过程. 要了解堆排序.我们首先来了解一个概念,全然二叉树. 堆是一种全然二叉树或者近似全然二 ...

  9. &lt;context-param&gt;与&lt;init-param&gt;的差别与作用

    <context-param>的作用: web.xml的配置中<context-param>配置作用 1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件 ...

  10. JSP页面退出时清除会话Session

    我们用一个quit.jsp来处理用户退出系统的操作,quit.jsp负责注销session,及时释放资源. 注销session. 关闭浏览器窗口. 其代码如下所示: <%@ page conte ...