leetcode 中等题(2)
2024-08-28 19:40:37
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;
}
};
最新文章
- for 循环打印图形
- ReactNative新手学习之路01-创建项目开始
- Bootstrap简介
- Printf()输出格式控制(转)
- Codeforces 749D:Leaving Auction(set+二分)
- .net 获取当前周及根据年和周获取起始结束时间
- ldconfig deferred processing now taking place
- MongoDB源码概述——内存管理和存储引擎
- Odoo constraints 使用教程
- 64位操作系统下用Microsoft.Jet.OLEDB.4.0出现未注册错误
- ThinkPHP中处理验证码不显示问题
- EularProject 43: 带条件约束的排列组合挑选问题
- 基于开源CA系统ejbca community 6.3.1.1构建私有CA管理数字证书
- 通用查询设计思想(2)- 基于ADO.Net的设计
- 魅族5.0以上设备(亲测有效)激活Xposed框架的流程
- JavaScript 基本包装类型,包装对象
- hadoop:如何运行自带wordcount
- php 设置中文 cookie, js获取
- #C++初学记录(算法3)
- asp.net mvc发送邮件