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. BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)

    无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...

  2. Codeforces Round #342 (Div 2) 解题报告

    除夕夜之有生之年CF第一场 下午从奶奶家回到姥姥家,一看还有些时间,先吃点水果陪姥姥姥爷聊了会儿,再一看表,5:20....woc已经开场20分钟了...于是抓紧时间乱搞.. **A. Guest F ...

  3. Android Studio学习笔记

    转:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1 背景 相信大家对Android Studio已经不陌生了,An ...

  4. appium跑demo简单实例讲解

    安装appium,设置 demo.pyfrom appium import webdriver #要装webdriver,方法查看http://www.cnblogs.com/sincoolvip/p ...

  5. Linux 内核通知链机制的原理及实现

    一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子 系统,Linux内核提供了通知链的机制.通 ...

  6. mysql 一个典型的数据库建表建用户过程

    cmd 命令不接 ";" mysql 命令后接 ";" 1.以管理员身份登录mysql mysql -u root –p 2.选择mysql数据库 use my ...

  7. wifi共享小工具

    MainForm.cs: using System;using System.Collections.Generic;using System.ComponentModel;using System. ...

  8. 面向对象分析设计-------02UML+UML各种图形及作用

    一.UML是什么?UML有什么用? 二.UML的历史 三.UML的上层结构(Superstructure) 四.UML建模工具 五.UML的图(重点) 1.用例图(use case diagram) ...

  9. C函数tolower,与toupper

    tolower    将大写转换成小写.  非字母字符不做出处理.   这个函数用法有点特殊他是处理字符的,而不是处理字符串的. 所谓的不能处理字符串不是说他不能处理字符串,他处理的时候对字符串必须是 ...

  10. DS_Store 是什么文件

    转自:http://blog.csdn.net/benbenxiongyuan/article/details/9010653 在xcode中,进行svn管理的时候,会发现一个DS_Store文件,这 ...