这三道题目都是一个意思,就是判断一个数是否为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…
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. Example 1: Input: 1 Output: true Example 2: Input: 16 Output: true Example 3: Input: 218 Output: false 给一个整数,写一个函数来判断它是否为2的次方数. 利用计算机用的是二进制的特点,用位操作,此题变得很简单. 2的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…
同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && (((<<) % n == ); } };…
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. 解题思路: 判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0. 代码如下: public class Solution { public boolean isPowerOfTwo(int n) { return (n > 0) && ( n & (n - 1)) == 0; } }…
描述 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&…
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) { ; )…
public boolean isPowerOfTwo(int n) { if(n<1) return false; while(n!=1){ if(n%2!=0) return false; n>>=1; } return true; }…
/* 用位操作,乘2相当于左移1位,所以2的幂只有最高位是1 所以问题就是判断你是不是只有最高位是1,怎判断呢 这些数-1后形成的数,除了最高位,后边都是1,如果n&n-1就可以判断了 如果是2的幂,&的结果是全0 */ if (n<=0) return false; return ((n&(n-1))==0); 划重点: 一个数*2,出相当于左移一位 2.判断是不是3的幂,没啥用的一道题 public boolean isPowerOfThree(int n) { /* 不…
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? Credits:Special thanks to @yukuairoyfor…
一天一道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&(…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. F…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? (二)解题 题目大意:判断一个是不是3…
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(二…
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 推断队列是否为空. 注意:仅仅能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the b…
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): 1:"123" 2:"132"  3 : "213" 4 :&quo…
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…
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? 题意是判断一个数是否是3的幂,最简单的也最容易想到的办法就是递归判断,或者循环除. 有另一种方法就是,求log以3为底n的对数.类似 如果n=9,则…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客 另外,本系列文章已整理并上传至gitbook,网址:点我进 欢迎转载,转载请注明出处! (一)题目 Given an integer array nums, find the sum of the elements b…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客 另外,本系列文章已整理并上传至gitbook,网址:点我进 欢迎转载,转载请注明出处! (一)题目 Reverse bits of a given 32 bits unsigned integer. For examp…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客 另外,本系列文章已整理并上传至gitbook,网址:点我进 欢迎转载,转载请注明出处! (一)题目 Remove all elements from a linked list of integers that hav…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pa…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j]and the differen…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5&q…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in…