342. Power of Four

这篇博客介绍了一个高效的算法来判断给定的整数是否为4的幂。通过位运算,可以检查一个数是否为2的幂,进一步判断它是否为4的幂。关键在于,4的幂在二进制表示中只有一个1,位于奇数位。通过n&(n-1)为0来验证2的幂,结合n&1431655765不为0来确认奇数位有1,从而确定结果。这种方法避免了递归和循环,提高了效率。

342. Power of Four

Easy

1533288Add to ListShare

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4x.

Example 1:

Input: n = 16
Output: true

Example 2:

Input: n = 5
Output: false

Example 3:

Input: n = 1
Output: true

Constraints:

  • -231 <= n <= 231 - 1

Follow up: Could you solve it without loops/recursion?

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        """
        解题思路:常规做法,如果n<=0,肯定不是;
        接着不断判断取余除不除得尽
        
        参考别人的思路:n小于等于0肯定不是的
        首先判断是否是2的次方,即二进制位只有一个1,假设n是2的次方,
        那么 n & (n-1) = 0,  是充分必要条件
        那么判断是否是4的次方,即二进制位奇数位是1,且和上面一样,只有一个1
        只有一个位置是1可用2的次方这个判断 n & (n-1) = 0
        奇数位是1可用 32位的奇数位全为1的数去与,不为0即存在奇数位为1
        
        """
        
        # if n <= 0:
        #    return False
        # if n == 1:
        #    return True
        # if n % 4 != 0:
        #    return False
        # return self.isPowerOfFour(n / 4)
        
        # sign = 0b01010101010101010101010101010101
        # print(sign)
        return n > 0 and not (n & (n - 1)) and n & 1431655765

ADC Reset Command The ADC reset command RESET [0000 0110] resets the ADC to the default values. 3.4 ADC Programming Sequence – Power Up At power-up it is necessary to initialize all the ADC registers. The sequence is: 1. Set the CS_EE to high to disable EEPROM communication. 2. Set the CS_ADC to low to enable ADC communication. 3. Initialize all four configuration registers to the default values in the EEPROM’s Relative addresses 61, 63, 65 and 67 (see the MSB bytes in see Section 3.0) by sending a WREG command to address 0 [0100 0011] followed by the four bytes of data: • Send the the Reset command (06h) to make sure the ADC is properly reset after powerup • Write the respective register configuration using the WREG command (Example: 43h, 0Ah, 84h, 40h, and 00h) Both a temperature and an uncompensated pressure reading are necessary to calculate a compensated value (see Section 3.5). 3.5 ADC Programming and Read Sequence – Temperature Reading (see Figure 3-2 and Table 3-3) 1. Set the CS_ADC low to enable ADC communication. 2. Configure the sensor to temperature mode and the desired data rate by setting configuration register 1 by sending a WREG command to address 1, [0100 0100] followed by the single configuration byte. Bit 1 (TS) of the configuration register should be set to 1. 3. Send 08h command to start data conversion on ADC. 4. The sensor will start to output the requested data on DOUT at the first SCLK rising edge after the command byte is received
最新发布
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值