[Swift]LeetCode201. 数字范围按位与 | 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.

Example 1:

Input: [5,7]
Output: 4

Example 2:

Input: [0,1]
Output: 0


给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。

示例 1:

输入: [5,7]
输出: 4

示例 2:

输入: [0,1]
输出: 0

44ms
 1 class Solution {
 2     func topDigit(_ n: Int) -> Int {
 3         var n = n
 4         while (n & (n-1)) != 0 {
 5             n = (n & (n-1))
 6         }
 7         return n
 8     }
 9     func rangeBitwiseAnd(_ m: Int, _ n: Int) -> Int {
10         guard m > 0 && n > 0 else {
11             return 0
12         }
13         var m2 = topDigit(m)
14         var n2 = topDigit(n)
15
16         if n2 != m2 {
17             return 0
18         }
19
20         return m2 | rangeBitwiseAnd(m-m2, n-m2)
21     }
22 }


48ms

 1 class Solution {
 2     func rangeBitwiseAnd(_ m: Int, _ n: Int) -> Int {
 3         if m == 0 {
 4             return 0
 5         }
 6
 7         var m = m
 8         var n = n
 9         var factor = 1
10
11         while m != n {
12             m >>= 1
13             n >>= 1
14             factor <<= 1
15         }
16
17         return m * factor
18     }
19 }


52ms

 1 class Solution {
 2     func rangeBitwiseAnd(_ m: Int, _ n: Int) -> Int {
 3         /*
 4         0  - 0000
 5         1  - 0001
 6         2  - 0010
 7         3  - 0011
 8         4  - 0100
 9         5  - 0101
10         6  - 0110
11         7  - 0111
12         8  - 1000
13         9  - 1001
14         10 - 1010
15
16         8..10  =>  1000
17         */
18
19         var m = m
20         var n = n
21         var i = 0
22         while m != n {
23             m >>= 1
24             n >>= 1
25             i += 1
26         }
27         return m << i
28     }
29 }


52ms

1 class Solution {
2     func rangeBitwiseAnd(_ m: Int, _ n: Int) -> Int {
3         return (n > m) ? (rangeBitwiseAnd(m / 2, n / 2) << 1) : m
4
5     }
6 }


56ms

 1 class Solution {
 2     func rangeBitwiseAnd(_ m: Int, _ n: Int) -> Int {
 3         var plus = 1
 4         var result = m & n
 5         while m + plus < n {
 6             result &= m + plus
 7             plus += plus
 8         }
 9         return result
10     }
11 }


64ms

 1 class Solution {
 2     func rangeBitwiseAnd(_ m: Int, _ n: Int) -> Int {
 3         // 101011101
 4         // 101111001
 5
 6         var m = m
 7         var n = n
 8         var res = 0
 9         var t = 1
10         for i in 0..<31 {
11             if m % 2 == 0 || n % 2 == 0 || m < n {
12                 m = m >> 1
13                 n = n >> 1
14             } else {
15                 res += t
16                 m = m >> 1
17                 n = n >> 1
18             }
19
20             t *= 2
21         }
22
23         return res
24     }
25 }

原文地址:https://www.cnblogs.com/strengthen/p/10184197.html

时间: 2024-10-27 20:24:14

[Swift]LeetCode201. 数字范围按位与 | Bitwise AND of Numbers Range的相关文章

区间数字的按位与 Bitwise AND of Numbers Range

2018-08-13 22:50:51 问题描述: 问题求解: 首先如果m 和 n不相等,那么必然会有至少一对奇偶数,那么必然末尾是0. 之后需要将m 和 n将右移一位,直到m 和 n相等. 本质上,本题就是求m 和 n的最长preSubNum. public int rangeBitwiseAnd(int m, int n) { if (m == 0) return 0; int moveFactor = 1; while (m != n) { m >>= 1; n >>= 1;

【LeetCode】201. Bitwise AND of Numbers Range

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. Credits:Special thanks to @amrsaqr for a

leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so t

leetcode_201题——Bitwise AND of Numbers Range(位操作)

Bitwise AND of Numbers Range Total Accepted: 9698 Total Submissions: 35771My Submissions Question Solution Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given th

LeetCode201 Bitwise AND of Numbers Range Java 题解

题目: 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. 解答: 假如说5:101  7:111  连续几个数相与的规律:一,只要是相同的位置的数字不相同最后那个位置的结果一定是0 .二,如

[LeetCode] 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. Credits:Special thanks to @amrsaqr for adding this problem and creatin

Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 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. 题意:

[LeetCode-JAVA] 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. 思路:开始一位一位做的TLE了,这是网上很简洁很好的一个方法. 很容易理解,因为是2进制,不一样则相与为0,如果第i位一样,i-1位

[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