【JAVA、C++】LeetCode 007 Reverse Integer
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的更多相关文章
- 【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 ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【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 ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【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 ...
随机推荐
- struts2理解
(1) Struts2(一)---struts2的环境搭建及实例 (2) struts2(二)---ModelDriven模型驱动 (3) Struts2属性驱动与模型驱动 (4)
- Android如何让真机显示debug log的调试信息
真机默认是不开启debug log调试功能的,以前我一直用模拟器,模拟器默认是开启debug log调试功能的,那么如何让真机开启呢? 我用华为Ascend P6为例: 1.进入拨号界面,输入*#*# ...
- The C Programming Language (second edition) 实践代码(置于此以作备份)
1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...
- Deep Learning in a Nutshell: Core Concepts
Deep Learning in a Nutshell: Core Concepts This post is the first in a series I’ll be writing for Pa ...
- LESSCSS的几点摘要
字符串插值 变量可以用像 @{name} 这样的结构,以类似 ruby 和 php 的方式嵌入到字符串中: @base-url: "http://assets.fnord.com" ...
- TCP的那些事儿(下)
TCP的那些事儿(下) 这篇文章是下篇,所以如果你对TCP不熟悉的话,还请你先看看上篇<TCP的那些事儿(上)> 上篇中,我们介绍了TCP的协议头.状态机.数据重传中的东西.但是TCP要解 ...
- python之字符聊天小工具
server side: # coding: gb2312#socket server端#获取socket构造及常量from socket import *#''代表服务器为localhostmyHo ...
- C++_Eigen函数库用法笔记——Advanced Initialization
The comma initializer a simple example join and block initialize join two row vectors together ini ...
- 说说Java中的枚举(一)
在实际编程中,往往存在着这样的“数据集”,它们的数值在程序中是稳定的,而且“数据集”中的元素是有限的.例如星期一到星期日七个数据元素组成了一周的“数据集”,春夏秋冬四个数据元素组成了四季的“数据集”. ...
- absolute布局和css布局释疑
jqueryui也不是万能的, 有时候, 也需要自己写一些, 由 css 和jquery结合的一些东西, 如: banner中, 依次播放的div等 ## 关于jquery设计的一些思想和理念?but ...