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;
}
};
 89. Gray Code(很快)
 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)的更多相关文章

  1. leetcode 中等题(1)

    2. Add Two Numbers(中等) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...

  2. LeetCode中等题(三)

    题目一: 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m ...

  3. LeetCode中等题(一)

    题目一: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  4. leetcode中等题

    # Title Solution Acceptance Difficulty Frequency     1 Two Sum       44.5% Easy     2 Add Two Number ...

  5. LeetCode中等题(二)

    题目一: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  6. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  7. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  8. leetcode刷题指南

    转载自:http://blog.csdn.net/lnho2015/article/details/50962989 以下是我个人做题过程中的一些体会: 1. LeetCode的题库越来越大,截止到目 ...

  9. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

随机推荐

  1. asp.net Log4Net错误日志个人总结

    1)创建Global.asax protected void Application_Start(object sender, EventArgs e) { log4net.Config.XmlCon ...

  2. 高级js--(面向对象js,arguments,闭包,自调)

    1. Arguments对象    l检测参数个数 1.在函数代码中,使用特殊对象 arguments,开发者无需明确指出参数名,就能访问它们. function howManyArgs() { al ...

  3. 使用STM32CubeMX生成USB_HOST_HID工程

    使用开发板为STM32F105开发板. 原本想将具体步骤给写出来.时间有限.直接将STM32F105_USBH_HID.IOC上传files.cnblogs.com/files/libra13179/ ...

  4. 查看进程中的socket状态和数量

    程序运行时查看,结果是这样子的 C:\Users\Administrator>netstat -ano|findstr TCP TIME_WAIT TCP TIME_WAIT TCP TIME_ ...

  5. Excel快捷生成想要的xml或text

    1.新建一个xml格式的A.xml文本,里面有4条以上的内容,注意哦,里面的元素与Excel里面的标题列数是一直的,不然会少了对应的某列数据.如: 2.打开Excel——>新建一个sheetl— ...

  6. SAFESEH 映像是不安全的

    1.打开该项目的“属性页”对话框 2.然后单击“链接器”--“命令行”,出现如下界面 3.将 /SAFESEH:NO 复制到“其它选项(D)”框中,然后点击应用 返回VS2013,重新编译运行程序即可 ...

  7. git 常用的命令总结

    下载gitlab上的代码: git clone ssh://git@47.xx.xx.xx:4xx/xxx.git 查看git代码状态: git status 不提交的代码文件: git checko ...

  8. 服务发现 - consul 的介绍、部署和使用(转)

    什么是服务发现 相关源码: spring cloud demo 微服务的框架体系中,服务发现是不能不提的一个模块.我相信了解或者熟悉微服务的童鞋应该都知道它的重要性.这里我只是简单的提一下,毕竟这不是 ...

  9. 剑指offer例题——裴波那契数列

    编程题:大家都知道裴波那契数列,现在要求输入一个整数n,请你输出裴波那契数列的第n项(从0开始,第0项为0).n<=39 public class Solution { public int F ...

  10. java.util.Arrays$ArrayList addAll报错

    执行下面代码时报错: List<String> centerList = WebConstants.SUPPORT_BIG_CENTERS_LIST; // WebConstants.SU ...