随笔分类 - 《剑指offer》
摘要:class Solution { public: vector<int> res; void bfs(TreeNode* root) { queue<TreeNode*> q; q.push(root); while(q.size ()) { auto p=q.front(); q.pop(); r
阅读全文
摘要:class Solution { public: vector<vector<int>> res; void bfs(TreeNode* root) { queue<TreeNode*> q; q.push(root); while(q.size ()) { int size=q.size(); v
阅读全文
摘要:class Solution { public: bool isPopOrder(vector<int> pushV,vector<int> popV) { stack<int> st; int n=pushV.size(),m=popV.size(); if(n!=m) return false;
阅读全文
摘要:class MinStack { public: stack<int> st;//普通栈 stack<int> stMin;//单调栈 /** initialize your data structure here. */ MinStack() { } void push(int x) { st.p
阅读全文
摘要:class Solution { public: int turn,n,m; static const int N=410; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};//右下左上 bool st[N][N]; bool check(int i
阅读全文
摘要:class Solution { public: void mirror(TreeNode* root) { if(root==NULL) return; mirror(root->left); mirror(root->right); TreeNode* tmp=root->left; root-
阅读全文
摘要:class Solution { public: bool dfs(TreeNode* l,TreeNode* r) { if(l==NULL&&r==NULL) return true; else if(l&&r) return l->val==r->val&&dfs(l->left,r->rig
阅读全文
摘要:class Solution { public: bool check(TreeNode* r1, TreeNode* r2) { if(r2==NULL) return true;//如果r2为空,无论r1,都匹配成功 if(r1&&r2) { if(r1->val!=r2->val) retur
阅读全文
摘要:class Solution { public: ListNode* merge(ListNode* l1, ListNode* l2) { ListNode* dummy=new ListNode(-1),*tail=dummy; while(l1&&l2) { if(l1->val>l2->va
阅读全文
摘要:#递归 class Solution { public: ListNode* reverseList(ListNode* head) { if(head==NULL||head->next==NULL) return head; //先反转head后面的链表,最后将head接在新链表最后即可 aut
阅读全文
摘要:#方法1,遍历一次,使用额外空间 哈希直接存储指针出现的次数,如果重复出现,直接返回即可 class Solution { public: unordered_map<ListNode*,int> hashmap;//记录指针及其出现的次数+1 ListNode *entryNodeOfLoop(L
阅读全文
摘要:class Solution { public: ListNode* findKthToTail(ListNode* head, int k) { auto i=head; auto j=head; int len=0; for (; len < k&&j!=NULL; len ++ ) j=j->
阅读全文
摘要:类比快排思想 class Solution { public: void reOrderArray(vector<int> &q) { if(!q.size()) return; int l=-1,r=q.size(); while(l<r) { do l++;while(l<r&&q[l]&1);
阅读全文
摘要:class Solution { public: ListNode* deleteDuplication(ListNode* head) { ListNode* dummy=new ListNode(1),*tail=dummy; dummy->next=NULL; for(auto i=head,
阅读全文
摘要:将下一个节点的值复制到当前节点,然后将下一个节点删除 class Solution { public: void deleteNode(ListNode* node) { node->val=node->next->val; auto p=node->next; node->next=node->n
阅读全文
摘要:class Solution { public: double Power(double base, int e) { typedef long long LL; bool flag=false; if(e<0) flag=true; double res=1;//存储base的2的倍数次幂 //计
阅读全文
摘要:class Solution { public: int NumberOf1(int n) { int res=0; for (int i = 0; i < 32; i ++ ) res+=(n>>i)&1; return res; } };
阅读全文
摘要:#数学方法 class Solution { public: int maxProductAfterCutting(int n) { //特判一下n≤3的情况 if(n<=3) return 1*(n-1); int res=1; if(n%3==1) n-=4,res*=4;//拆出4 else
阅读全文
摘要:class Solution { public: int cnt=0; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; bool st[55][55]; void dfs(int x,int y,int k,int rows, int cols)
阅读全文
摘要:class Solution { public: int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; bool st[5][5]; bool dfs(vector<vector<char>>& matrix,int i,int j,int u,stri
阅读全文