C#LeetCode刷题之#7-反转整数(Reverse Integer)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3838 访问。
给定一个 32 位有符号整数,将整数中的数字进行反转。
输入: 123
输出: 321
输入: -123
输出: -321
输入: 120
输出: 21
注意:
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231 − 1]。根据这个假设,如果反转后的整数溢出,则返回 0。
Given a 32-bit signed integer, reverse digits of an integer.
Input: 123
Output: 321
Input: -123
Output: -321
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3838 访问。
public class Program {
private const string NEGATIVE_CHAR = "-";
public static void Main(string[] args) {
var x = 123;
var res = Reverse(x);
Console.WriteLine(res);
x = -567;
res = Reverse2(x);
Console.WriteLine(res);
Console.ReadKey();
}
private static int Reverse(int x) {
int.TryParse(ReverseString(x.ToString()), out int res);
return res;
}
private static string ReverseString(string text) {
var negative = text.StartsWith(NEGATIVE_CHAR);
var arr = text.Replace(NEGATIVE_CHAR, "").ToCharArray();
Array.Reverse(arr);
return (negative ? NEGATIVE_CHAR : "") + new string(arr);
}
private static int Reverse2(int x) {
var res = 0L;
while(x != 0) {
res = res * 10 + x % 10;
x = x / 10;
}
if(res > int.MaxValue || res < int.MinValue) {
res = 0;
}
return (int)res;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3838 访问。
321
-765
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#7-反转整数(Reverse Integer)的更多相关文章
- 说了你可能不信leetcode刷题局部链表反转D92存在bug,你看了就知道了
一.题目描述 找出数组中重复的数字 > 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...
- LeetCode刷题笔记-递归-反转二叉树
题目描述: 翻转一棵二叉树. 解题思路: 1.对于二叉树,立马递归 2.先处理 根节点,不需改动 3.处根的左子树和右子树需要交换位置 4.递归处理左子树和右子树.步骤见1-3步 Java代码实现: ...
- Leetcode 7 反转整数Reverse Integer
给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: ...
- [Swift]LeetCode7. 反转整数 | Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 7. 反转整数(Reverse Integer) C++
知识点: 内置变量 INT_MAX INT_MIN 运算结果是否溢出的判断 判断pop>7即pop>INT_MAX%10 判断pop<-8即pop<INT_MIN%10 c ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
- leecode刷题(12)-- 整数反转
leecode刷题(12)-- 整数反转 整数反转 描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: - ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
随机推荐
- 三、Python系列——Pandas数据库读取数据
Pandas主要先读取表格类型的数据,然后进行分析. import pandas as pd# 由于是用pandas模块操作数据,因此不用在路径前加open,否则就是python直接打开文件,可能还会 ...
- Python Ethical Hacking - Malware Analysis(1)
WRITING MALWARE Download file. Execute Code. Send Report. Download & Execute. Execute & Repo ...
- 集训作业 洛谷P1433 吃奶酪
嗯?这题竟然是个绿题. 这个题真的不难,不要被他的难度吓到,我们只是不会计算2点之间的距离,他还给出了公式,这个就有点…… 我们直接套公式去求出需要的值,然后普通的搜索就可以了. 这个题我用的深搜,因 ...
- TeamX
介绍 TeamX 是基于 SolonJT 平台构建的团队管理小工具,主要功能有: Wiki(团队词条,用于写MD格式接口文档也行...) Planned(项目计划 和 个人日志) Issues(问题管 ...
- centos7中防火墙转为iptables
1.关闭firewall systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止fir ...
- 带你快速了解 MongoDB 分布式集群
在分布式应用系统中,mongodb 已经成为 NoSQL 经典数据库.要想很好的使用 mongodb,仅仅知道如何使用它是不够的.只有对其架构原理等有了充分认识,才能在实际运用中使其更好地服务于应用, ...
- WEB前端常见受攻击方式及解决办法
一个网站建立以后,如果不注意安全方面的问题,很容易被人攻击,下面就讨论一下几种漏洞情况和防止攻击的办法. 一.SQL注入 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的 ...
- Salt组件(二)
四.配置管理States 是SaltStack中的配置语言,在日常进行配置管理时需要编写大量的States文件.比如我们需要安装一个包,然后管理一个配置文件,最后保证某个服务正常运行.这里就需要我们编 ...
- Python os.fdatasync() 方法
概述 os.fdatasync() 方法用于强制将文件写入磁盘,该文件由文件描述符fd指定,但是不强制更新文件的状态信息.高佣联盟 www.cgewang.com 如果你需要刷新缓冲区可以使用该方法. ...
- PHP strrpos() 函数
实例 查找 "php" 在字符串中最后一次出现的位置: <?php高佣联盟 www.cgewang.comecho strrpos("I love php, I l ...