power of two
2015-07-06 21:00
295 查看
power-of-two
n=10000***000,
<=> n&(n-1)=0
是这个方法的核心
https://leetcode.com/discuss/40202/only-push-others-using-queue-combination-shared-solutions
这题有一个好的方法,是用链表实现的队列,来模拟栈,只有push是on, 其他是o1操作的
记住几个关键的递归式解,可以快速报出答案,免得被主定理,或者plugin 展开计算
T(n)=T(n/2)+O(1) 二分 logn
T(n)=2T(n/2)+O(n) 快排,归并 nlogn
T(n)=2T(n/2)+O(1) n 分治等于没治
class Solution { public: bool isPowerOfTwo(int n) { return n>=1 && !(n&(n-1)); } };
n=10000***000,
<=> n&(n-1)=0
是这个方法的核心
https://leetcode.com/discuss/40202/only-push-others-using-queue-combination-shared-solutions
这题有一个好的方法,是用链表实现的队列,来模拟栈,只有push是on, 其他是o1操作的
class MyStack { Queue<Integer> queue; public MyStack() { this.queue=new LinkedList<Integer>(); } // Push element x onto stack. public void push(int x) { queue.add(x); for(int i=0;i<queue.size()-1;i++) { queue.add(queue.poll()); } } // Removes the element on top of the stack. public void pop() { queue.poll(); } // Get the top element. public int top() { return queue.peek(); } // Return whether the stack is empty. public boolean empty() { return queue.isEmpty(); } }自己写了一个C++版本,其实不需要链式栈,发现了这个方法更好的实现了
class Stack { public: // Push element x onto stack. void push(int x) { q.push(x); int qsize=q.size(); for(int i=0;i<qsize-1;i++){ q.push(q.front()); q.pop(); } } // Removes the element on top of the stack. void pop() { q.pop(); } // Get the top element. int top() { return q.front(); } // Return whether the stack is empty. bool empty() { return q.empty(); } private: queue<int> q; };
记住几个关键的递归式解,可以快速报出答案,免得被主定理,或者plugin 展开计算
T(n)=T(n/2)+O(1) 二分 logn
T(n)=2T(n/2)+O(n) 快排,归并 nlogn
T(n)=2T(n/2)+O(1) n 分治等于没治
相关文章推荐
- Java实现链式存储的二叉查找树(递归方法)
- 用AOP改善javascript代码
- 径向基函数(RBF)神经网络
- KMP算法中next数组的构建
- 求两个数的最大公约数 C++实现
- jQuery源码分析
- 也许可以通过这种方式读取配置文件
- css笔记17:盒子模型加强版的案例
- 黑马程序员——java基础---集合(第17天)
- Markdown使用小结
- 对.net framework的理解
- LeetCode39:Combination Sum
- print语句默认每行添加一个换行符 来自2.13
- jQuery cdn使用介绍
- Java中的System.in,System.out,System.err
- Observer观察者模式
- Mysql执行Update操作时会锁住表
- 《开源框架那些事儿21》:巧借力与借巧力
- 百度 Alexand(亚历山大)简单使用教程
- favicon.ico 的作用(收藏夹的图标文件)