【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
总结:处理整数溢出的方法
①用数据类型转换long 或 long long
②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好)
③整体恢复,看数字是否相等。
思路:注意30000这样以0结尾的数字,注意越界时返回0.
我检查越界是通过把翻转的数字再翻转回去,看是否相等。
int reverse(int x) {
int xx = abs(x);
int ans = ;
int zeros = ; //如果x = 2000 这种后面有很多0 那翻转后是2 再翻转还是2 需要乘以1000才能恢复成2000
while(xx != )
{
int r = xx % ;
xx /= ;
ans = ans * + r;
if(ans == ) zeros *= ;
}
ans = (x < ) ? -ans : ans;
//检测是否溢出 思路把数字重新翻转回去,看结果是否相同
int anss = abs(ans), check = ;
while(anss != )
{
int r = anss % ;
anss /= ;
check = check * + r;
}
check = (x < ) ? -check : check;
check *= zeros;
if(check != x) //溢出了
return ;
else
return ans;
}
基于②的判断:
public int reverse(int x)
{
int result = ; while (x != )
{
int tail = x % ;
int newResult = result * + tail;
if ((newResult - tail) / != result)
{ return ; }
result = newResult;
x = x / ;
} return result;
}
基于①的判断:
int reverse(int x) {
long num = abs((long)x);
long new_num = ;
while(num) {
new_num = new_num* + num%;
num /= ;
}
if (new_num > INT_MAX) {
return ;
}
return (x< ? -*new_num : new_num);
}
【leetcode】Reverse Integer(middle)☆的更多相关文章
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Dungeon Game (middle)
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- EF上下文管理
- CentOS系统操作mysql的常用命令
MySQL名字的来历MySQL是一个小型关系型数据库管理系统,MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了 ...
- 第四天 rxcocoa
HackerNewsReaderDemo HackerNewsAPI.sharedApi.newStories() .observeOn(ConcurrentDispatchQueueSchedule ...
- C和指针 第一章 字符串处理程序
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_COL 20 #def ...
- java自定义标签 权限
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java ...
- android socket编程用Bufferreader读取的一个失败教训
由于我的手机需要用笔记本开的wifi,躺在床上玩手机时需要关电脑或者是要让电脑放歌的时候总是不想下床,于是我想能不能用一个APP,然后通过局域网实现在手机上对电脑进行操控呢?说干就干. 我在电脑上用的 ...
- CSU 1120 病毒(DP)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- 开源多线程性能测试工具-sysbench
导读 sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试.数据库目前支持MySQL/Oracle/PostgreSQL.本文主要演示Mysql测试 ...
- C#高级编程笔记 Day 2, 2016年8月 31日 构造函数
1.构造函数: 实例构造函数(只要创建了对象,就会执行)一般使用 this 关键字区分成员字段和同名的参数.可以把构造函数定义为private 或 protected .这样不相关的类也不能访问他们. ...