LeetCode 简单操作 | 7. Reverse Integer
2017-03-02 11:21
447 查看
/* * Leetcode7. Reverse Integer * Funtion: Reverse digits of an integer. * Example: x = 123, return 321 x = -123, return -321 * Note:The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. * Author: LKJ * Date: 2017/2/28 * Hint: */ #include <iostream> #include <vector> #include <string> #include <cmath> #include <algorithm> using namespace std; class Solution { public: int reverse(int x) { int sign = 0; if(x < 0) {sign = 1;x =-x;} uint32_t y = 0; int z; while(x){ if((y > 214748364)) return 0; y = y*10; y = y + (x%10); if((y > 2147483647)) return 0; x= x/10; } if(sign == 1) z = -(int)y; else z = (int)y; return z; } }; /* public int reverse(int x) { int result = 0; while (x != 0) { int tail = x % 10; int newResult = result * 10 + tail; if ((newResult - tail) / 10 != result) //good { return 0; } result = newResult; x = x / 10; } return result; } */ int main(){ int myin = 1534236469; int myout; Solution SA; myout = SA.reverse(myin); cout << myout << endl; return 0; }
相关文章推荐
- leetcode -- Text Justification -- string操作的题目,思路简单,但是难以写对
- LeetCode 简单操作 | 191. Number of 1 Bits
- LeetCode 简单操作 | 476. Number Complement
- LeetCode 简单操作 | 12. Integer to Roman
- leetcode--最长公共前缀--简单的字符串操作
- LeetCode 简单操作 | 136. Single Number
- LeetCode 简单操作 | 169. Majority Element
- LeetCode 简单操作 | 168. Excel Sheet Column Title
- LeetCode 简单操作 | 13. Roman to Integer
- LeetCode 简单操作 | 8. String to Integer (atoi)
- LeetCode 简单操作 | 17. Letter Combinations of a Phone Number
- LeetCode 简单操作 | 171. Excel Sheet Column Number
- LeetCode 简单操作 | 461. Hamming Distance
- LeetCode 简单操作 | 389. Find the Difference
- JavaWeb中的一些简单操作(四) 页面间的跳转的本质以及url乱码问题解决
- win32简单串口操作
- MFC 简单文件读写操作
- git相关操作(简单篇)
- C#:XML操作(简单)
- 使用三层架构实现简单的MVC登陆操作!并实现基本的增删改查功能!!