题目: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. Pycharm如何取消自动换行

    1.只对当前文件有效的操作是: 菜单栏->View -> Active Editor -> Use Soft Wraps (不选中) 2.要是想对所有文件都起到效果,就要在setti ...

  2. pro-select-like

    DELIMITER | drop procedure if exists pro_query; CREATE PROCEDURE pro_query ( cname VARCHAR() ) BEGIN ...

  3. windows 全角 怎么切换到半角

    windows 全角 怎么切换到半角 :shift+空格键

  4. MIS货物拆包销售的问题

    就是不能拆包装销售.比如一箱香烟要一包包的卖,一箱里面有50条,一条里面有10包,而是,要一包一包的卖. 解决方案:入库的时候,记录下包装总量(自动改成数量×50),再附加2条说明字段,第一条说明是一 ...

  5. docker的安装以及jdk和tomcat的环境配置

    准备工作:需要Linux kernel 3.8支持查看linux内核的版本:root@ubuntu-dev:~# cat /proc/version查看linux版本:root@ubuntu-dev: ...

  6. Convolutional Neural Networks for Visual Recognition

    http://cs231n.github.io/   里面有很多相当好的文章 http://cs231n.github.io/convolutional-networks/ Table of Cont ...

  7. Ubuntu 16.04 安装 JDK 及 Eclipse 详细步骤(转发:https://blog.csdn.net/bluish_white/article/details/56509446)

    2017.3.1更新 修正了一些命令,现在按照文章步骤配置不会出现问题了. JDK 安装及配置 参考来源:http://www.linuxidc.com/Linux/2017-02/140908.ht ...

  8. Oracle Shared Pool 原理

    Oracle Shared Pool 原理 由于shared pool中最重要的是library cache,所以本文主要讲解Library cache的结构,library cache latch, ...

  9. Redis五大数据类型及操作

    目录: 一.redis的两种链接方式 二.redis的字符串操作(string) 三.redis的列表操作(list) 四.redis的散列表操作(类似于字典里面嵌套字典) 五.redis的集合操作( ...

  10. Web框架简介

    Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...