Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

解题思路:
将数字翻转并不难,可以转成String类型翻转,也可以逐位翻转,本题涉及到的主要是边界和溢出问题,使用Long或者BigInteger即可解决。

题目不难:

JAVA实现如下:

public class Solution {
static public int reverse(int x) {
if(x==0||x==-2147483648)return 0;
boolean isNagetive=false;
if(x<0){
isNagetive=true;
x=-x;
}
long result=0;
while(x!=0){
result*=10;
result+=x%10;
x/=10;
}
final int INT_MAX=0x7fffffff;
if((result-INT_MAX)>0)
return 0; if(isNagetive)result=-result;
return (int)result;
}
}

C++实现如下:

 #include<algorithm>
using namespace std;
class Solution {
public:
int reverse(int x) {
if (x == INT_MIN)
return ;
bool isNeg = x < ;
x = abs(x);
long res = ;
while (x) {
res = res * + x % ;
if (res > INT_MAX)
return ;
x /= ;
}
return isNeg ? -(int)res: (int)res;
}
};

【JAVA、C++】LeetCode 007 Reverse Integer的更多相关文章

  1. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  7. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  8. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. 【JAVA、C++】LeetCode 001 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

随机推荐

  1. 图解Android - Looper, Handler 和 MessageQueue

    Looper, Handler 和 MessageQueue 是Android 的异步消息处理机制

  2. jeecms内容显示条数

    1.按照1.2.3.4.5顺序显示 <div class="index-news"> [@cms_channel id='1'] <h2><span& ...

  3. 【BZOJ-1406】密码箱 约数 + 乱搞 + set?

    1406: [AHOI2007]密码箱 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1143  Solved: 677[Submit][Status][ ...

  4. TCP/IP详解 学习七

    静态选路的前提: 1)         网络比较小 2)         网络之间单点连接 3)         网络之间没有多余的路由 动态选路协议,用于路由器之间的通信,有以下几种: 1)     ...

  5. Notions of Flow Networks and Flows

    这篇随笔是对算法导论(Introduction to Algorithms, 3rd. Ed.)第26章 Maximum Flow的摘录. ------------------------------ ...

  6. tp框架表单验证

    之前的表单验证都是用js写的,这里也可以使用tp框架的验证.但是两者比较而言还是js验证比较好,因为tp框架验证会运行后台代码,这样运行速度和效率就会下降. 自动验证是ThinkPHP模型层提供的一种 ...

  7. 可输入自动匹配Select——jquery ui autocomplete

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  8. hdu 2111 Saving HDU

    解题思路: 首先做本题,要清楚题意的要求. 1.读取数据到结构体数组中,然后按其价值降序(价值最大的放在最上面). 2.比较给定的M (包裹容量),如果大于当前宝物的体积,则计算总价值+= 宝物的总价 ...

  9. serialization机制

    首先说明一下序列化的知识: java中的序列化(serialization)机制能够将一个实例对象的状态信息写入到一个字节流中,使其可以通过socket进行传输.或者持久化存储到数据库或文件系统中:然 ...

  10. Lua函数之二

    Lua函数之二 Lua中函数的两个重要特性: 1.函数和其他类型(如number.string)一样,可以存放在变量中,也可以存放在table中,可以作为函数的参数,还可以作为函数的返回值. 2.嵌套 ...