LeetCode第[7]题(Java):Reverse Integer 标签:数学
题目: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.
LeetCode第[7]题(Java):Reverse Integer 标签:数学的更多相关文章
- 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 + ...
- 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 ...
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...
- 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 ...
- 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 ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- leetCode练题——7. Reverse Integer
1.题目: 7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 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 ...
随机推荐
- Vmware VsPhere下的VM安装Hyper-v服务
问题:Vmware VsPhere下的VM无法安装Hyper-v服务 解决方案:打开vmware vm的安装目录,然后找到.vmx的文件,然后进行修改 打开w-c-w2012.vmx文件 添加如下 h ...
- Java Json API:Gson使用简单入门
GSON是Google开发的Java API,用于转换Java对象和Json对象.本文讨论并提供了使用API的简单代码示例.更多关于GSON的API可以访问:http://sites.google.c ...
- Redis几个认识误区(转)
add by zhj: 文章很老了,2010年的,注意,下面几点是作者认为的误区 原文:http://timyang.net/data/redis-misunderstanding/ 前几天微博发生了 ...
- redis中默认有多少个哈希槽?
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余 ...
- C++和JAVA实例化对象的区别
JAVA: A a = new A(); 为A对象创建了一个实例,但在内存中开辟了两块空间:一块空间在堆区,存放new A()这个对象:另一块空间在堆栈,也就是栈,存放a,a的值为new A()这个对 ...
- filebeat 简介安装
Filebeat is a lightweight shipper for forwarding and centralizing log data. Installed as an agent on ...
- sql server升级打补丁
原本:https://blog.csdn.net/kk185800961/article/details/72436415 sql server升级 sql server打补丁 sql server升 ...
- django重写用户模型
重写一个UserProfile继承自带的AbstractUser # -*- coding: utf-8 -*- from __future__ import unicode_literals fro ...
- 吴超老师课程--Hive的执行语句
为什么选择Hive? (1)基于Hadoop的大数据的计算/扩展能力(2)支持SQL like查询语言(3)统一的元数据管理(4)简单编程 一:Hive的数据类型(1)基本数据类型tinyint/sm ...
- go——结构体(二)
Go语言是一种静态类型的编程语言.这意味着,编译器需要在编译时知晓程序里每个值的类型. 如果提前知道类型信息,编译器就可以确保程序合理的使用值. 这有助于减少潜在的内存异常和bug,并且使编译器有机会 ...