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- ...
随机推荐
- Websphere修改web.xml不生效的解决办法(转)
在websphere下部署了一个java工程后,如果修改了web.xml文件,重新启动这个java工程发现websphere并没有自动加载web.xml文件,即修改后的web.xml并不起作用,除非重 ...
- 围绕一个 volatile 关键字居然可以问出来 16 个问题
对于 Java 每次面试就会想到多线程,多线程问题基本跑不了要问一下 volalite 关键字,可是我万万没想到居然一个 volatile 关键字可以连续问题出来 16 个问题!看下你能回答出来几个? ...
- 题解 洛谷 P4112 【[HEOI2015]最短不公共子串】
给定两个字符串\(A\)和\(B\),我们需要找出一个串,其在\(A\)中出现且不在\(B\)中出现,这个串为子串或者子序列,求在每种情况下,该串的最短长度. 考虑到后缀自动机可以识别一个字符串的所有 ...
- CCNA - Part11 - 隔离广播域的 VLAN 来了
之前在对交换机的介绍中,我们知道交换机的作用就是隔离广播域,在不需要跨网段传输时,在同一子网中转发数据包从而进行通信.实现的核心原理就是在交换机中拥有一张 MAC 表,记录了对应终端设备和接口之间的关 ...
- vue学习(十六) 自定义私有过滤器 ES6字符串新方法 填充字符串
<div id="app"> <p>{{data | formatStr('yyyy-MM-dd')}}</p></div> //s ...
- windows系统远程修改密码
1.需求:公司需要短时间.批量修改一些windows系统的管理员密码: 2.准备工作: a.下载软件:链接:https://pan.baidu.com/s/1kV52DqE1_4siPuxS5Mosc ...
- Pyramid attention networks for image restoration
paper:https://arxiv.org/abs/2004.13824 code: https://github.com/SHI-Labs/Pyramid-Attention-Networks ...
- 使用Openresty构建认证网关
[入门]使用Openresty构建认证网关 lwhile关注 0.5092017.10.07 16:00:03字数 1,330阅读 4,112 在单体应用中, 我们可以通过 cookie + sess ...
- STL set
写在前面:STL大法好! 容器set,可以实现排序,插入元素不能重复(所以可能插入失败) 接下来我们看一下set的基本用法 begin() 返回set容器的第一个元素的地址 end() 返回s ...
- MAVEN无法下载com.oracle:jdbc14:jar解决办法
原文链接:https://www.cnblogs.com/gqzdev/p/11742999.html 第一步,下载ojdbc14jar包: 链接:ojdbc14jar 提取码: 2m59 第二步,下 ...