综合
溺水的鱼xu
这个作者很懒,什么都没留下…
展开
-
已知前序和中序,重建二叉树
已知前序和中序,重建这颗二叉树前序:1 2 4 8 9 5 10 3 6 7中序: 8 4 9 2 10 5 1 6 3 7后序:8 9 4 10 5 2 6 7 3 1# include<iostream>using namespace std;int pre[100]={1 ,2, 4, 8, 9, 5, 10, 3, 6, 7};int in[100]={8 ,4 ...原创 2019-12-01 16:23:35 · 250 阅读 · 0 评论 -
NC118 数组中的逆序对
NC118 数组中的逆序对直接暴力,超时。class Solution {private: const int kmod = 1000000007;public: int InversePairs(vector<int> data) { int ret = 0; int n = data.size(); for (int i = 0; i < n; ++i) { for (int j = i +原创 2021-07-29 19:38:49 · 144 阅读 · 0 评论 -
NC38 螺旋矩阵
注意只有一行或者只有一列的情况,或者只有一个数字的情况。因此,这个题错误出现可能最少的情况就是上下包含第一个和最后一个,左右边的去除第一个和最后一个。同时在下和左这两部分加一个限定条件,避免和上右这两部分重复了。class Solution {public: int a[1000][1000]; vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<i原创 2021-07-13 13:58:47 · 78 阅读 · 0 评论 -
NC97 字符串出现次数的TopK问题
字符串出现次数的TopK问题优先队列+重载小于号+class结构体 class Node{ public: string s; int ans; friend bool operator <(const Node & a,const Node & b) { if(a.ans!=b.ans) {原创 2021-07-13 17:38:20 · 89 阅读 · 0 评论 -
NC90 包含min函数的栈
NC90 包含min函数的栈简单的双栈法常规的做法是额外添加一个同步栈(min栈),以保存记录之前所有的min值,相当于是使用了n个辅助变量,所以空间复杂度是O(n)。这里要注意min函数中必须是数据,不能是一个函数。class Solution {public: stack<int> st1; stack<int> Min; void push(int value) { st1.push(value);原创 2021-07-22 10:19:57 · 119 阅读 · 0 评论 -
NC100 将字符串转化为整数
NC100 将字符串转化为整数class Solution {public: int atoi(const char *str) { long long ans=0; int flag=0; for(int i=0;str[i]!='\0';i++) { //去空格 if(str[i]=='+'||str[i]==' ') con原创 2021-07-22 14:14:02 · 129 阅读 · 0 评论 -
LRU和LRU总结
NC93 设计LRU缓存结构list+map实现,无需自己实现链表get元素时,先判断在不在,不存在返回-1,存在的话更新list,即删除节点在原来在链表中的位置,然后在放到链表头部。set时,也是判断在不在,存在,更新一下map中的值,然后更新list,删除原理的放到list头部。class Solution {public: vector<int> LRU(vector<vector<int> >& operators, int k)原创 2021-07-22 20:43:40 · 297 阅读 · 0 评论 -
剑指 Offer 31. 栈的压入、弹出序列
剑指 Offer 31. 栈的压入、弹出序列原创 2021-07-22 22:44:19 · 54 阅读 · 0 评论 -
NC137 表达式求值
NC137 表达式求值中缀表达式转后缀表达式,只不过在转后缀表达式的过程中符号弹出的话直接计算出结果。符号的优先级大于符号栈栈顶符号,直接压入,优先级小于或等于符号栈栈顶符号,不断弹出符号栈元素class Solution {public: stack<int> st1; stack<char> st2; map<char,int> mp; int jisuan(char c) {原创 2021-07-23 00:14:15 · 104 阅读 · 0 评论