leetcode7
public class Solution {
public int Reverse(int x) {
int fuhao = ;
if (x < )
{
fuhao = -;
}
try
{
x = Math.Abs(x);
}
catch (Exception e)
{
return ;
}
int i = ;
var list = new List<long>();
do
{
long num = x % Convert.ToInt64(Math.Pow(, i + )) / Convert.ToInt64(Math.Pow(, i));
list.Add(num);
i++;
}
while (x / Convert.ToInt64(Math.Pow(, i)) != );
var length = list.Count;
long result = ;
for (int j = ; j < length; j++)
{
try
{
result += list[j] * Convert.ToInt64(Math.Pow(, length - j - ));
}
catch (Exception e)
{
result = ;
break;
}
}
result *= fuhao;
int res = ;
int.TryParse(result.ToString(), out res);
//Console.WriteLine(res);
return res;
}
}
https://leetcode.com/problems/reverse-integer/#/description
补充一个python的实现:
class Solution:
def reverse(self, x: int) -> int:
if x == :
return
operation =
if x < :
operation = -
sx = str(x)
if sx[] == '-':
sx = sx[:]
n = len(sx)
result = []
for i in range(n-,-,-):
result.append(sx[i])
r = int(''.join(result))
r *= operation
if r >= ** or r < (-) ** :
r =
return r
leetcode7的更多相关文章
- 【LeetCode7】Reverse Integer★
题目描述: 解题思路: 反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法. Java代码: 方法一: 判断溢出方法:在执行完int newResult=result*10+tail语句后, ...
- LeetCode7:Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- [Swift]LeetCode7. 反转整数 | Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- leetcode7:反转整数
给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...
- leetcode7. 整数反转
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: ...
- LeetCode7.反转整数
给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...
- leetcode7 Rerver Integer
题意:数字反转 思路:醉了,提交了好几次,难点不在于怎么反转,而是判断是否益处,原题中给的是int,4个字节,32位,开始不知道怎么判断.现在知道了是limits.h中的INT_MIN和INT_MAX ...
- LeetCode7.整数反转 JavaScript
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: ...
- LeetCode-7.reverse-integer 【翻转字符串】【数学】
PS: 第一次写文章好累啊,没想到这么短的文章写完这么累,大家给我点反馈,多给我留言啊.
随机推荐
- Sort An Unsorted Stack
Given a stack of integers, sort it in ascending order using another temporary stack. Examples: Input ...
- 【java编程】java对象copy
实现java对象Copy的三种方式 一.克隆 implements Cloneable 二.序列化 implements Serializable 三.利用反射机制copy apache的BeanUt ...
- LG3804 【模板】后缀自动机
题意 给定一个只包含小写字母的字符串\(S\), 请你求出 \(S\) 的所有出现次数不为 \(1\) 的子串的出现次数乘上该子串长度的最大值. 对于\(100\%\) 的数据,\(|S| \leq ...
- laravel5.3 源码分析 Passport
laravel5.3,密码模式的授权认证过程.我会通过两部分内容分享以及查看passport的认证流程分享出来 第一部分:根据官方文档,通过Composer安装Passport 文档地址:http:/ ...
- Linux内核电源管理综述
资料:http://blog.csdn.net/bingqingsuimeng/article/category/1228414http://os.chinaunix.net/a2006/0519/1 ...
- watchtower 自动更新容器的工具
watchtower 自动更新容器的工具 安装 使用docker docker run -d \ --name watchtower \ -v /var/run/docker.sock:/var/ru ...
- Oracle:Decode在时间范围中的使用
做查询的时候需要下一个sql,需要select test_time出来,如果test_Time的HH24:Mi:SS在7:00:00和19:00:00返回白班,否则返回夜班 select case w ...
- win7上搭建Android环境及调试
工欲善其事必先利其器,好记性不如烂笔头.要学习一门新的语言,首先必须得先搭环境,否则没法实践.如果之前按照网上的提示,搭建过环境,而且环境比较复杂的话,我相信隔很长一段时间后,就会忘记,到真正用的时候 ...
- Juery 实现淡出 淡现效果
HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- Python的数据库连接池DBUtils
DBUtils是Python的一个用于实现数据库连接池的模块. 此连接池有两种连接模式: 模式一:为每个线程创建一个连接,线程即使调用了close方法,也不会关闭,只是把连接重新放到连接池,供自己线程 ...