1. 232 Implement Queue using Stacks1.1 问题描述 使用栈模拟实现队列。模拟实现如下操作:
push(x). 将元素x放入队尾。
pop(). 移除队首元素。
peek(). 获取队首元素。
empty(). 判断队列是否为空。
注意:只能使用栈的标准操作,push,pop,size和empty函数。1.2 方法与思路 本题和用队列实现栈思路一样,设...
分类:
其他好文 时间:
2015-07-07 11:07:50
阅读次数:
112
Given an integer, write a function to determine if it is a power of two.
题目解析:
求一个数是否是2的幂次方所得。是就返回true,不是就返回false.
方法一:
直接用n%2,结果如果为1,就返回false(n=1除外,因为2的0次幂为1)。
代码如下:
class Solution {
public:
...
分类:
其他好文 时间:
2015-07-07 09:31:52
阅读次数:
132
leetcode 231: Power of Two
python, java, c++...
分类:
其他好文 时间:
2015-07-07 07:07:44
阅读次数:
117
1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 int temp; 5 if(n==0) 6 return false; 7 if(n==1) 8...
分类:
其他好文 时间:
2015-07-07 00:44:43
阅读次数:
97
1。目前主流的无线充电标准有三种:Power Matters Alliance(PMA)标准、Qi标准、Alliance for Wireless Power(A4WP)标准。Power Matters Alliance标准Power Matters Alliance标准是由Duracell Pow...
分类:
其他好文 时间:
2015-07-06 23:08:03
阅读次数:
162
power-of-two
class Solution {
public:
bool isPowerOfTwo(int n) {
return n>=1 && !(n&(n-1));
}
};
n=10000***000,
n&(n-1)=0
是这个方法的核心...
分类:
其他好文 时间:
2015-07-06 21:45:50
阅读次数:
117
Power of TwoGiven an integer, write a function to determine if it is a power of two.Credits:Special thanks to@jianchao.li.fighterfor adding this probl...
分类:
其他好文 时间:
2015-07-06 21:38:27
阅读次数:
159
1、231 Power of Two题目很简单,直接上代码,主要是掌握bitset的使用。 1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 bitset myset(n); 5 if (n...
分类:
其他好文 时间:
2015-07-06 21:26:00
阅读次数:
95
Given an integer, write a function to determine if it is a power of two.
判断一个数是否是2的幂,判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0.
方法1: n & n-1 == 0
class Solution {
public:
bool isPowerOfTwo(int n) {
...
分类:
其他好文 时间:
2015-07-06 19:50:52
阅读次数:
95
Given an integer, write a function to determine if it is a power of two.所有为power of two的整数均为 100000... 的形式,减一则会造成所有位相异,即与的结果为0。这里需要排除当n为0的情况,减一后与的结果仍为...
分类:
其他好文 时间:
2015-07-06 17:38:59
阅读次数:
72