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. sum,filter和map参数里面的玄机

    首先是sum函数. 最常见的用法似乎是: >>> sum([1,2,3]) 6 但其实这是默认首个元素是数字0.我们可以指定其他数字: >>> sum([1,2,3 ...

  2. 百度map 3.0初探

    1.简介 在使用百度地图SDK为您提供的各种LBS能力之前,您需要获取百度地图移动版的开发密钥,该密钥与您的百度账户相关联.因此,您必须先有百度帐户,才能获得开发密钥.并且,该密钥与您创建的过程名称有 ...

  3. 移动开发测试工具——Bugtags的集成

    移动开发测试工具--Bugtags 官网:https://bugtags.com/ 注册开发者账号 注册账号并激活邮箱,都会就不多做介绍了. 创建应用 创建完账号以后会提示添加应用,点击添加 添加应用 ...

  4. Team Foundation Server 2015 Update 2.1 发布日志

    微软在 2016年5月5日发布了Visual Studio Team Foundation Server 2015 update 2.1. 下面我们来看看Update2.1中给我们带来了哪些新功能. ...

  5. Android核心安全机制(一)

    Android六种核心安全机制-加密.密钥.签名与证书 对于移动开发,程序猿很容易会忘记一些安全问题,如一个MD5的加密,大部分人都知道怎么去使用,但是其中的一些加密原理,加密方式却只有少部分会去了解 ...

  6. 14 fragment 创建

    静态展示 注意 静态的开始进入界面的生命周期和动态的不同 详情:14 fragment注意点 步骤一:创建一个类继承 Fragment 代码类型一: package com.fmy.demo1; im ...

  7. acm入门搜索-石油数目

    题意:给出一个N*M的矩形区域和每个区域的状态--有/没有石油,(定义)如果两个有石油的区域是相邻的(水平.垂直.斜)则认为这是属于同一个oil pocket. 求这块矩形区域一共有多少oilpock ...

  8. android之.9.png详解

    .9.PNG是安卓开发里面的一种特殊的图片,这种格式的图片通过ADT自带的编辑工具生成,使用九宫格切分的方法,使图片支持在android 环境下的自适应展示. PNG,是一种非失真性压缩位图图形文件格 ...

  9. ubuntu中安装samba

    为了方便的和Windows之间进行交互,samba必不可少. 当然,他的安装使用也很简单: 安装: sudo apt-get install samba sudo apt-get install sm ...

  10. UNIX网络编程——UDP缺乏流量控制(改进版)

    现在我们查看无任何流量控制的UDP对数据报传输的影响.首先我们把dg_cli函数修改为发送固定数目的数据报,并不再从标准输入读.如下,它写2000个1400字节大小的UDP数据报给服务器. 客户端程序 ...