7.Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123

Output: 321

Example 2:

Input: -123

Output: -321

Example 3:

Input: 120

Output: 21

Note:

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

没有想出来, 仿照(或者照搬?)http://www.cnblogs.com/grandyang/p/4125588.html来写

注意点:

反转溢出问题, 解决方法: 使用比该数字范围大的类型定义返回值

版本一: 拆分每个位的思想是我的, 其他细节参考上述博客

class Solution {
public:
int reverse(int x) {
long long res = 0;
vector<int> bit; while (0 != x) {
bit.push_back(x % 10);
x /= 10;
} for (int i = 0; i < bit.size(); i++) {
res =res * 10 + bit[i];
} return (res < INT_MIN || res > INT_MAX) ? 0 : res;
}
};

版本二: 半个自己写的, 考虑符号, 虽然多余了

class Solution {
public:
int reverse(int x) {
long long ret = 0;
// int positive = 1;
long long positive = 1; if (x < 0) {
positive = -1;
x *= positive;
} while (x != 0) {
ret = ret * 10 + x % 10;
x /= 10;
} ret *= positive; return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};
class Solution {
public:
int reverse(int x) {
long long int ret = 0; while(0 != x) {
ret = ret*10 + x%10;
x /= 10;
} //return (ret > INT_MIN || ret < INT_MAX)? ret : 0;
return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};

7_Reverse Integer的更多相关文章

  1. 9_Palindrome Number

    9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...

  2. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  3. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  4. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  5. [LeetCode] Integer Replacement 整数替换

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  6. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  7. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  8. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. Nuget管理自己的项目库

    Nuget是什么 Nuget 是一种 Visual Studio 扩展工具,它能够简化在 Visual Studio 项目中添加.更新和删除库(部署为程序包)的操作.(官方地址)相信大家对这个应该还是 ...

  2. matlab中axis的用法

    来源:https://ww2.mathworks.cn/help/matlab/ref/axis.html?searchHighlight=axis&s_tid=doc_srchtitle a ...

  3. Talk is cheap. Show me the code的由来

    Date: Fri, 25 Aug 2000 11:09:12 -0700 (PDT) From:Linus Torvalds Subject Re: SCO: "thread creati ...

  4. tuple的增删改查

    dict = {"k1": "v1", "k2": "v2", "k3": "v3&quo ...

  5. CPU 执行程序的秘密,藏在了这 15 张图里

    前言 代码写了那么多,你知道 a = 1 + 2 这条代码是怎么被 CPU 执行的吗? 软件用了那么多,你知道软件的 32 位和 64 位之间的区别吗?再来 32 位的操作系统可以运行在 64 位的电 ...

  6. NOIP提高组2013 D2T3 【华容道】

    某王  老师给我们考了一场noip2013的真题...心态爆炸! 题目大意: 有一个n*m的棋盘,每个格子上都有一个棋子,有些格子上的棋子能够移动(可移动的棋子是固定的),棋盘中有一个格子是空的,仍何 ...

  7. 2017年 实验四 B2C模拟实验

    实验四 B2C模拟实验                [实验目的] 掌握网上购物的基本流程和B2C平台的运营 [实验条件] ⑴.个人计算机一台 ⑵.计算机通过局域网形式接入互联网. (3).奥派电子商 ...

  8. Django基础之Ajax

    知识预览 Ajax前戏:json Ajax简介 Jquery实现的ajax JS实现的ajax Ajax前戏:json 什么是json? 定义: JSON(JavaScript Object Nota ...

  9. solr之functionQuery(函数查询)【转】

    函数查询 让我们可以利用 numeric域的值 或者 与域相关的的某个特定的值的函数,来对文档进行评分. 怎样使用函数查询 这里主要有两种方法可以使用函数查询,这两种方法都是通过solr http 接 ...

  10. 第二十章 nginx常见问题

    一.Nginx常见问题 一.nginx多server优先级 在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中的每个server_name进行匹配, ...