[算法练习]Reverse Integer
题目说明:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return –321
程序代码:
#include <gtest/gtest.h>
using namespace std; int reverse2(int x)
{
int result[20] = {0};
int resultIdx = 0;
int sign = 1;
long long val = x;
if (val < 0)
{
sign = -1;
val = -val;
} while (val)
{
result[resultIdx++] = val % 10;
val /= 10;
} long long newResult = 0;
long long base = 1;
for (int i = resultIdx-1; i >= 0; i--)
{
newResult += result[i] * base;
base *= 10;
} if (newResult > 0x7FFFFFFF)
{
newResult = 0;
} newResult *= sign; return (int)newResult;
} int reverse(int x)
{
int result[20] = {0};
int resultIdx = 0; while (x)
{
result[resultIdx++] = x % 10;
x /= 10;
} long long newResult = 0;
long long base = 1;
for (int i = resultIdx-1; i >= 0; i--)
{
newResult += result[i] * base;
base *= 10;
} if ((newResult > 0x7FFFFFFF) || (newResult < -0x7FFFFFFF))
{
newResult = 0;
} return (int)newResult;
} TEST(Pratices, tReverse)
{
// 123 -> 321
ASSERT_EQ(reverse(123),321);
// -123 -> -321
ASSERT_EQ(reverse(-123),-321);
// 0 -> 0
ASSERT_EQ(reverse(0),0);
// -0 -> 0
ASSERT_EQ(reverse(-0),0);
// 0x1FFFFFFF -> 0x7FFFFFFF
ASSERT_EQ(reverse(-2147483648),0);
// -0x1FFFFFFF -> -0x7FFFFFFF
//ASSERT_EQ(reverse(-0x1FFFFFFF),-0x7FFFFFFF);
}
[算法练习]Reverse Integer的更多相关文章
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- Reverse Integer 2015年6月23日
题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- Python字符串倒序-7. Reverse Integer
今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
随机推荐
- 最新版IntelliJ IDEA2019.1破解教程(2019.04.08更新)
[原文链接]:https://www.tecchen.xyz/idea-crack.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力有限,对文章 ...
- The Annoying Bug
log里看不出问题,直接客户端就disconnected. gdb 挂了也不会停住,继续跑得跟正常人似的 再连根本不正常的了. 硬件: a , 主板CPU更换过 b,USB3.0 软件: 无有更换,但 ...
- 04-树5 Root of AVL Tree (25 分)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 20190416 OSX系统使用VMware Fusion安装CentOS7踩的那些坑
一.创建虚拟机 (1)在虚拟机资源库中点击[+添加]按钮,选择“新建...”选项 (2)选择创建自定义虚拟机 (3)选择系统类型为CentOS (4)选择虚拟磁盘类型 (5)选择虚拟机存储位置:点击[ ...
- Python+USB+Vnet+FTP传输文件开发记录
做一个Python+USB+Vnet+FTP传输文件开发记录
- Struts2方法调用的三种方式(有新的!调用方法的说明)
在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="heroAction" class="com.ABC ...
- 《Algorithms算法》笔记:元素排序(4)——凸包问题
<Algorithms算法>笔记:元素排序(4)——凸包问题 Algorithms算法笔记元素排序4凸包问题 凸包问题 凸包问题的应用 凸包的几何性质 Graham 扫描算法 代码 凸包问 ...
- 阅读The Java® Language Specification需要知道的术语
Null Pointer Exception,简称NPE 在java中,static final修饰的是常量.根据编译器的不同行为,常量又可分为编译时常量和运行时常量. 举例说明吧 public st ...
- js中in关键字的用法
1. 在For...In 声明用于对数组或者对象的属性进行循环/迭代操作. 例子:var a = new Array; for(x in a){ console.log(x); } 2. 判断对象是否 ...
- PHP之string之chr()函数使用
chr (PHP 4, PHP 5, PHP 7) chr - Return a specific character chr - 返回指定的字符 Description string chr ( i ...