LeetCode之7_Reverse Integer
来源:互联网 发布:python入门经典pdf下载 编辑:程序博客网 时间:2024/06/06 11:39
题目原文:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Subscribe to see which companies asked this question
分析:将整数倒序,符号不变,需要注意的是当改变后的值超出整数的可表示范围后,返回0.
代码:
//Reverse digits of an integer.////Example1: x = 123, return 321//Example2: x = -123, return -321 #include <iostream>using namespace std;class Solution {public:int reverse(int x) {int nFlag = 1;int nRet = 0;int nLeft =0;int nOld = 0;if (x < 0){x *= -1;nFlag = -1;}while (x != 0){nLeft = x % 10;x = x/10;nOld = nRet;nRet = nRet *10 + nLeft;}if (nOld != nRet / 10){return 0;}nRet *= nFlag;return nRet;}};
1 0
- LeetCode之7_Reverse Integer
- LeetCode 7_Reverse Integer
- LeetCode题目_Reverse Integer
- leetcode第一刷_Reverse Integer
- Leetcode第七题_Reverse Integer
- LeetCode之7 --- Reverse Integer
- LeetCode之186_Reverse Words in a String 字符串词序反转算法
- leetcode第一刷_Reverse Linked List II
- LeetCode 25_Reverse Nodes in k-Group
- leetcode之Reverse Integer
- LeetCode 之 Reverse Integer
- LeetCode之Reverse Integer
- LeetCode之Reverse Integer
- LeetCode之Reverse Integer
- Leetcode之Reverse Integer
- leetcode之Reverse Integer
- LeetCode之Reverse Integer
- 【Leetcode】之Reverse Integer
- 神经网络BP反向传播算法Error Backpropagation
- 坎巴拉太空计划MOD:kRPC0.2.2中文文档
- 内部类和匿名内部类的学习心得
- spring和mybatise的整合
- 文件及文件夹的新建与删除
- LeetCode之7_Reverse Integer
- 文件的判断
- 文件重命名
- Muduo网络库源码分析(二) 定时器TimeQueue,Timer,TimerId
- 关于使用微信登录第三方APP的实现(Android版)
- 杭电2612
- 无参构造方法和有参构造方法的调用
- 字符串存成excel表范例
- yii2入口文件