leetcode 剑指offer重复题目
来源:互联网 发布:西方意识形态 知乎 编辑:程序博客网 时间:2024/06/01 07:58
226. Invert Binary Tree 28
class Solution {public: TreeNode* invertTree(TreeNode* root) { if(root==nullptr) return root; invert(root); return root; } void invert(TreeNode* root){ if(root==nullptr) return; if(root->left==nullptr&&root->right==nullptr) return; TreeNode*temp=root->left; root->left=root->right; root->right=temp; invert(root->left); invert(root->right); }};
263. Ugly Number
class Solution {
public:
bool isUgly(int num) {
if(num==0) return false;
while(num%2==0)
num/=2;
while(num%3==0)
num/=3;
while(num%5==0)
num/=5;
return num==1;
}
};
105. Construct Binary Tree from Preorder and Inorder Traversal 7
155. Min Stack 30
class MinStack {public: /** initialize your data structure here. */ MinStack() { } void push(int x) { mains.push(x); if(fuzhu.size()==0||fuzhu.top()>x) fuzhu.push(x); else { fuzhu.push(fuzhu.top()); } } void pop() { mains.pop(); fuzhu.pop(); } int top() { return mains.top(); } int getMin() { return fuzhu.top(); } stack<int> fuzhu; stack<int> mains; };
225. Implement Stack using Queues 9 相关
解答:http://blog.csdn.net/m0_37693059/article/details/76422264
三种方法
232. Implement Queue using Stacks 9
class MyQueue {public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { stack1.push(x); } /** Removes the element from in front of queue and returns that element. */ int pop() { if(stack2.empty()) { while(!stack1.empty()) { stack2.push(stack1.top()); stack1.pop(); } } int temp=stack2.top(); stack2.pop(); return temp; } /** Get the front element. */ int peek() { if(stack2.empty()) { while(!stack1.empty()) { stack2.push(stack1.top()); stack1.pop(); } } return stack2.top(); } /** Returns whether the queue is empty. */ bool empty() { return stack1.empty()&&stack2.empty(); } stack<int> stack1; stack<int> stack2;};
238. Product of Array Except Self 66
572. Subtree of Another Tree 26
/** * 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: bool isSubtree(TreeNode* s, TreeNode* t) { bool result=false; if(s&&t) { if(s->val==t->val)//入口点 result=hasSub(s,t); if(result==false) result=isSubtree(s->left,t); if(result==false) result=isSubtree(s->right,t); } return result; } bool hasSub(TreeNode* owner,TreeNode* sub){ if(owner==nullptr&&sub==nullptr) return true; if(owner==nullptr||sub==nullptr) return false; if(owner->val!=sub->val) return false; return hasSub(owner->left,sub->left)&&hasSub(owner->right,sub->right); }};
233 43
http://blog.csdn.net/m0_37693059/article/details/76468223
阅读全文
0 0
- leetcode 剑指offer重复题目
- 《剑指Offer》面试题3及LeetCode中相似题目
- 剑指offer题目集
- 剑指offer题目记录
- 剑指offer题目概括
- 剑指offer题目
- 【剑指offer】题目4
- 【剑指offer】题目10
- 剑指offer题目分类
- 剑指offer题目1
- 剑指offer题目练习一
- 剑指offer题目1-10
- 剑指offer题目汇总(一)
- 剑指offer题目汇总(二)
- 剑指offer题目汇总(三)
- 剑指offer题目思想汇总
- 剑指offer题目及答案
- c++知识点(剑指offer题目)
- 解决myeclise对js的报错问题
- 剑指offer算法题之单链表的删除结点操作--面试题13:在O(1)时间删除链表结点
- odoo10 基本字段
- 2017.07.30 总结
- 2017.7.30
- leetcode 剑指offer重复题目
- dump
- 摄像机标定学习笔记(5) 关于张正友平面标定方法
- day05内存管理技术
- 凸包的直径——旋转卡壳
- git
- C++11新特性之 std::future and std::async
- Linux进程
- MyBatis入门教程(一)