231. Power of Two Given an integer, write a function to determine if it is a power of two. class Solution { public: bool isPowerOfTwo(int n) { ? (n & (n-)) == : false; } }; 342. Power of Four Given an integer (signed 32 bits), write a function to che…
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode.com/problems/power-of-two/description/ 326 Power of Three:https://leetcode.com/problems/power-of-three/description/ 342 Power of Four :https://leetcod…
1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Power of Three: 判断一个整数是否是3的n次方,其中n是非负整数 2. 思路 1)2的n次方 不妨列举几个满足条件的例子. If n = 0: 2 ^ n = 1 If n = 1: 2 ^ n = 2 -> 10(二进制表示) If n = 2: 2 ^ n = 4 -> 100(二…
lc 231 Power of Two 231 Power of Two Given an integer, write a function to determine if it is a power of two. analysation Easy problem. solution bool isPowerOfTwo(int n) { if (n <= 0) return false; while (n%2 == 0) n = n>>1; return (n == 1); }…
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 解析 2的幂只有1个1 仔细观察,可以看出 2 的次方数都只有一个 1 ,剩下的都是 0 .根据这个特点,只需要每次判断最低位是否为 1 ,然后向右移位,最后统计 1 的个数即可判断是否是 2 的次方数. 减一法 如果一个数是 2 的次方数的话,那么它的二进数必然是最高位为1,其它都为 0…
1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的n次幂. 2.思路 如果一个整数是2的n次幂,那么首先其应当是正数,其次该数的二进制表示必定是以1开头,后续若有数字必为0. 3.代码 class Solution { public: bool isPowerOfTwo(int n) { return (!(n&(n-1)))&&n&…
Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是2的整数幂. Analysis: 这道题首先需要注意n为非整数是返回false的情况. 1. 若将n转换为二进制数,如果n为2的整数幂,则转换得到的二进制数只包含一个一,故计算转换过程中1的个数,最终判断. class Solution { public: bool isPowerOfTwo(int…
题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): def isPowerOfTwo(self, n): # """ :type n: int :rtype: bool """ 方法:分析2的幂次方的特点,发现2的任意次方的数,化成二进制时只有首位为1其余位为0,因此我的解决方法如下: class…
题目: Given an integer, write a function to determine if it is a power of two. 链接: http://leetcode.com/problems/power-of-two/ 题解: 验证一个数是否是2的幂次,我们可以使用(n & (n - 1))来决定. 比如 10 & 01, 100 & 011等等. Time Complexity - O(1), Space Complexity - O(1) publi…
题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0. 代码如下: public class Solution { public boolean isPowerOfTwo(int n) { return (n > 0) && ( n & (n - 1)) == 0; } }…
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. Input The input contains exactly one test case. T…
Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. 代码如下: public class Solution { public boolean isPowerOfTwo(int n) { if(n<=0) r…
Given an integer, write a function to determine if it is a power of two. Solution:一个整数如果是2的整数次方,那么它的二进制表示中有且只有一位是1,而其他所有位都是0.把这个整数与这个整数减去1之后进行与运算,那么这个整数当中唯一的1会变为0,这个整数也变为0 class Solution { public: bool isPowerOfTwo(int n) { )return false; )); } };…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an integer, write a function to determine if it is a power of two. (二)解题 题目大意:判断一个数是不是2的n次方. 解题思路:从二进制位运算来看,2的n次方是诸如0001,0010,0100,1000-.等数,所以采用位运算,n&(…
题目链接 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A^2 + A^3 + - + A^k. Input The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 10…
Given an integer, write a function to determine if it is a power of two. 题解:一次一次除2来做的话,效率低.所以使用位运算的方法(左移一位相当于原数乘2)列出在int范围内的所有2的倍数,然后和n异或如果等于0,那么n就是它(两个数的异或为0,那么两个数就相等). 注意:位运算的优先级比==的优先级低,要加括弧. class Solution { public: bool isPowerOfTwo(int n) { ; )…
题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. Input The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30…