1. Reverse Integer

  题目链接

  题目要求:

  Reverse digits of an integer.

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

  click to show spoilers.

  Have you thought about this?

  Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

  If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

  Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

  For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

  这道题主要要注意末尾为0和越界的问题。程序如下:

 class Solution {
public:
int reverse(int x) {
if(x == INT_MIN)
return ; int num = abs(x);
int last = ;
long result = ;
while(num != )
{
last = num % ;
result = result * + last;
num /= ;
} if(result > INT_MAX)
return ; return (x > ) ? result : -result;
}
};

  2. Reverse Bits

  题目链接

  题目要求:

  Reverse bits of a given 32 bits unsigned integer.

  For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).

  Follow up:
  If this function is called many times, how would you optimize it?

  Related problem: Reverse Integer

  Credits:
  Special thanks to @ts for adding this problem and creating all test cases.

  下边的程序中用到了bitset数据结构,需要注意的是,bits[0]才是最低位

 class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t one = ;
bitset<> bits;
int i = ;
while(n != && i > -)
{
bits[i] = n & one;
n = n >> ;
i--;
} return bits.to_ulong();
}
};

LeetCode之“数学”:Reverse Integer && Reverse Bits的更多相关文章

  1. leetcode第七题Reverse Integer (java)

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

  2. leetcode:Reverse Integer 及Palindrome Number

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

  3. LeetCode: Reverse Integer 解题报告

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

  4. Leetcode 题目整理-2 Reverse Integer && String to Integer

    今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...

  5. 65. Reverse Integer && Palindrome Number

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

  6. Reverse Integer - 反转一个int,溢出时返回0

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

  7. LeetCode第[7]题(Java):Reverse Integer 标签:数学

    题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...

  8. [LeetCode] Reverse Integer 翻转整数

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

  9. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

随机推荐

  1. 理解性能的奥秘——应用程序中慢,SSMS中快(3)——不总是参数嗅探的错

    本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 接上文:理解性能的奥秘--应用程序中慢,SSMS中快(2)--SQL Server如何编译存储过程 在我们开始深入研究如何处理 ...

  2. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  3. [lua]写个简单的Lua拓展-sleep函数

    这几天在做一个小项目,其中用到了一些基本的API, 例如sleep,获取当前目录等等,lua标准库中没有提供这些接口,虽然所第三方库中也都有实现,但是要用的就那么几个函数,在一个嵌入式系统中安装那么多 ...

  4. 实现一个最简单的VIM文本编辑器(可能有bug,随便写了一个)

    简单的写了一个文本编辑器,功能很简单,但足以把文件IO相关的操作熟悉了,可能功能或者分配的大小还不够完善.请参考参考: #include <stdio.h> #include <co ...

  5. antlr v4 使用指南连载3——g4文件概览

    g4文件概览        在深入介绍之前,有必要先给大家了解一下g4文件的结构,以便对如何编写语法规则文件有个全局的认识,我想这是大有禆益的.因为这样我们就可以很清晰地知道需要的东西写在哪里,或者哪 ...

  6. UNIX网络编程——select函数的并发限制和 poll 函数应用举例

    一.用select实现的并发服务器,能达到的并发数,受两方面限制 1.一个进程能打开的最大文件描述符限制.这可以通过调整内核参数.可以通过ulimit -n来调整或者使用setrlimit函数设置,  ...

  7. androidpn-server笔记及BUG修改

    上篇讲了androidpn的client端,这篇该讲一下我使用androidpn-server端的笔记了. 这里我使用的androidpn是tomcat版的,由不知哪位大神移植并修复了部分bug的版本 ...

  8. Android读取网络图片到本地的简约的实现

    今天在网上看到了一个关于读取网络文件的小视频,觉得不错,拿来与大家分享 思路 具体的思路比较的简单,但是思想非常的单纯.那就是输入一个网址,点击按钮,将从网络上获取的一张图片显示到一个ImageVie ...

  9. 最简单的基于FFmpeg的AVDevice例子(屏幕录制)

    =====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...

  10. Android图片色彩变幻

    最近在做图片相关的应用,所以就各方积累到一些常用的操作,一般来说会有多种方式来实现这一功能,比如 采用色度变换 采用ColorMatrix颜色矩阵 采用对像素点的直接操作 等等,今天就复习一下第一种方 ...