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 ...
随机推荐
- 第5章 IDA Pro
5.1 加载一个可执行文件 默认情况下IDA Pro的反汇编代码中不包含PE头或资源节,可以手动指定加载. 5.2 IDA Pro接口 5.2.1 反汇编窗口模式 二进制模式/图形模式: 图形模式:红 ...
- C# 二进制文件操作(内容搜索、数据截取)
private void button2_Click(object sender, EventArgs e) { var list = new List<Frame>(); byte[] ...
- MAC OSX--docker
http://www.cnblogs.com/yjmyzz/p/docker-install-tutorial.html http://www.cnblogs.com/yjmyzz/p/docker- ...
- Unicode 和 UTF-8 是什么关系?
2015-10-14 10:08 评论: 9 收藏: 4 转载自: http://huoding.com/2015/10/13/472作者: 火丁笔记本文地址:https://linux.cn/ ...
- ES6通过WeakMap解决内存泄漏问题
一.Map 1.定义 Map对象保存键值对,类似于数据结构字典:与传统上的对象只能用字符串当键不同,Map对象可以使用任意值当键. 2.语法 new Map([iterable]) 属性 size:返 ...
- 【转】Spring中@Component的作用
今天在写程序的时候看见一个以前没有见过的注解(@Component),在网上查找过后,经过实践,决定把它记录下来. 1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中 ...
- numpy.random.random & numpy.ndarray.astype & numpy.arange
今天看到这样一句代码: xb = np.random.random((nb, d)).astype('float32') #创建一个二维随机数矩阵(nb行d列) xb[:, 0] += np.aran ...
- Python3+Selenium3自动化测试-(一)
完成环境的安装并测试之后,我们对Selenium有了一定的了解了,接下来我们继续驱动浏览器做一些基本操作: 窗口尺寸设置.网页截图.刷新.前进和后退 窗口尺寸设置 在测试过程中,我们可能会要求打开浏览 ...
- Python+Django+Bootstrap 框架环境搭建
1.安装python和pip(python.pip安装自行百度,pip是一个安装和管理 Python 包的工具) 2.配置python环境变量(python和scripts目录都需要配置) 3.安装D ...
- 企业级web nginx服务优化
1.1)隐藏nginx header 内版本号信息 [root@aliyun ~]# vi /application/nginx/conf/nginx.conf http{ …… server_tok ...