LeetCode之“数学”:Reverse Integer && Reverse Bits

  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和越界的问题。程序如下:

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         if(x == INT_MIN)
 5             return 0;
 6
 7         int num = abs(x);
 8         int last = 0;
 9         long result = 0;
10         while(num != 0)
11         {
12             last = num % 10;
13             result = result * 10 + last;
14             num /= 10;
15         }
16
17         if(result > INT_MAX)
18             return 0;
19
20         return (x > 0) ? result : -result;
21     }
22 };

  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]才是最地位

 1 class Solution {
 2 public:
 3     uint32_t reverseBits(uint32_t n) {
 4         uint32_t one = 1;
 5         bitset<32> bits;
 6         int i = 31;
 7         while(n != 0 && i > -1)
 8         {
 9             bits[i] = n & one;
10             n = n >> 1;
11             i--;
12         }
13
14         return bits.to_ulong();
15     }
16 };
时间: 2024-08-21 01:42:07

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

[LeetCode][JavaScript]Reverse Integer

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: 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 alread

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: 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 alread

leetcode(7): Reverse Integer 源码实现 runtime: 8ms

题目 :Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 题目分析及部分代码解析: 1.需要考虑一位数,比如1,2,3等特殊情况,返回本身. 2.需要考虑0,返回0. 3.需要考虑如123000,45600等末尾有若干零的情况,正确结果应为321.654,不应该出现000321,00654等情况. 4.需要4字节int类型数据的取值范

Leetcode 题目整理-2 Reverse Integer &amp;&amp; String to Integer

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

LeetCode: Reverse Integer 解题报告

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. SOLUTION 1: 注意越界后返回0.先用long来计算,然后,把越界的处理掉. public class Solution { public int reverse(int x) { long ret = 0; while (x !

LeetCode:Palindrome Number,Reverse Integer

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

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

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 若反转的数溢出,直接返回0 可以用计算结果来判断溢出,也可以用因数来判断 Java代码实现: 1 public class ReverseInteger { 2 public static int reverseInt(int x){ 3 if (x == 0) { 4 return

65. Reverse Integer &amp;&amp; Palindrome Number

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 alr

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

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assum