【Leetcode】Bitwise AND of Numbers Range

题目链接:https://leetcode.com/problems/bitwise-and-of-numbers-range/

题目:

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.

For example, given the range [5, 7], you should return 4.

思路:

32位整型,只需判断每一位在m~n之间是否有0存在,若有,该位 在AND操作中一定为0,只需判断32次

算法

[java] view
plain
 copy

  1. public int rangeBitwiseAnd(int m, int n) {
  2. if (m == n)
  3. return m;
  4. int res = 0, i = 0;
  5. while (n != 0 && m != 0) {
  6. // 检查m~n是否有偶数存在时间复杂度O(1)
  7. boolean flag = false;
  8. for (int j = m; j <= n; j++) {
  9. int tmp = j;
  10. if ((tmp & 1) == 0) {// 最后一位为0
  11. flag = true;
  12. break;
  13. }
  14. }
  15. if (flag == false) {// 最低位全是1
  16. res = res | (1 << i); // 将指定为置为1
  17. }
  18. i++;
  19. n = n >> 1;
  20. m = m >> 1;
  21. }
  22. return res;
  23. }
时间: 2024-10-13 12:39:50

【Leetcode】Bitwise AND of Numbers Range的相关文章

【leetcode】Bitwise AND of Numbers Range(middle)

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思路: 先找前面二进制相同的位,后面不相同的位相与一定为0. 比如: 1111001 1110011 从0011 - 1001 中间一定会经

【LeetCode 201】Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 题意: 给定 [m, n] 范围内个数,返回范围内所有数相与的结果. 思路: 如果打着暴力的旗号,那么这个题是会超时的 - -!.最后也是没

[LeetCode#201] Bitwise AND of Numbers Range

Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Analysis: The idea behind this problem is not hard, you could

leetcode 201. Bitwise AND of Numbers Range(位运算,dp)

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 题解:如果m==n,那么答案就是m. 如果m<n,那么二进制最右边一位在最后的结果中肯定是0,那么就可以转化成子问题: rangeBi

Java for LeetCode 201 Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 解题思路: 本题有很多思路,最简单的方法: result就是m和n二进制前面相同的部分!!! JAVA实现如下: public int ra

【Leetcode】:Compare Version Numbers

题目地址:https://oj.leetcode.com/problems/compare-version-numbers/ 题目描述: Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings ar

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(

【LeetCode】165 - Compare Version Numbers

Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte

【Leetcode】2. Add Two Numbers

Problem: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the tw