leetcode 中等题(2)
50. Pow(x, n) (中等)
double myPow(double x, int n) {
double ans = ;
unsigned long long p;
if (n < ) {
p = -n;
x = / x;
} else {
p = n;
}
while (p) {
if (p & )
ans *= x;
x *= x;
p >>= ;
}
return ans;
}
96. Unique Binary Search Trees(很快)
class Solution {
public:
int numTrees(int n) {
long ans=;
for(int i=n+;i<=*n;i++)
ans = i*ans/(i-n);
return ans/(n+);
}
};
94. Binary Tree Inorder Traversal(很快)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
vector<int> res;
if(root==NULL) return res;
TreeNode* p=root;
while(!s.empty()||p){
if(p){
s.push(p);
p=p->left;
}
else{
p=s.top();
s.pop();
res.push_back(p->val);
p=p->right;
}
}
return res;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> res;
vector<int> inorderTraversal(TreeNode* root) {
if(!root) return res;
inorderTraversal(root->left);
res.push_back(root->val);
inorderTraversal(root->right);
return res;
}
};
class Solution {
public:
vector<int> grayCode(int n) {
int len = << n;
vector<int> res(len,);
for(int i = ;i != len;++i){
res[i] =i ^ (i >> );
}
return res;
}
};
leetcode 中等题(2)的更多相关文章
- leetcode 中等题(1)
2. Add Two Numbers(中等) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- LeetCode中等题(三)
题目一: 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m ...
- LeetCode中等题(一)
题目一: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...
- leetcode中等题
# Title Solution Acceptance Difficulty Frequency 1 Two Sum 44.5% Easy 2 Add Two Number ...
- LeetCode中等题(二)
题目一: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- leetcode刷题指南
转载自:http://blog.csdn.net/lnho2015/article/details/50962989 以下是我个人做题过程中的一些体会: 1. LeetCode的题库越来越大,截止到目 ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
随机推荐
- spring 之 depends-check
我们知道 spring xml 配置中 bean 的子元素 property 是按照 setXxx(SomeType someParam) 方式来设值的,换句话说, 是根据属性的setter 方法, ...
- 零配置使用springboot
1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- iOS设计标注处理方法
如果设计只给3x的设计图 在做2x适配时有几种处理方法: 按逻辑像素,大小不变,比如3x手机上一张图的逻辑像素设为24x24point,那么2x手机上这张图的大小也设为24x24point,一般适用于 ...
- NETIF_F_LLTX 的属性
在bond初始化的时候,我们可以看到如下属性: /* don't acquire bond device's netif_tx_lock when transmitting */ bond_d ...
- Haskell语言学习笔记(74)GADTs
GADTs GADTs(Generalised Algebraic Data Types,广义代数数据类型)是对代数数据类型的一种扩展. 它允许在定义数据类型时明确指定类型参数的类型并使用模式匹配. ...
- LeetCode OJ 77. Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
- 二叉堆复习(包括d堆)
要期中考了……我真的是什么也不会啊,书都没看过TAT. 好吧整理一下二叉堆,这里就以最大堆为例好了. 首先二叉堆其实是一棵CBT,满足父节点的键值大于左右子节点的键值(wikipedia把这个叫键值, ...
- Delphi 不使用自带模板创建服务
program Project1; uses Windows, WinSvc; const ServiceName: pchar = 'SnowWings Service'; DisplayName: ...
- Hibernate学习笔记2.4(Hibernate的Id生成策略)
通过设置告诉id该怎么设置. 1.通过xml方式 1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据 ...
- ArcGIS案例学习笔记-CAD数据自动拓扑检查
ArcGIS案例学习笔记-CAD数据自动拓扑检查 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 功能:针对CAD数据,自动进行拓扑检查 优点:类别:地理建模项目实例 ...