当前位置: 首页 > news >正文

8.16 pq

 

 

lc993 找堂兄弟

bfs记录信息

int xDepth = -1, yDepth = -1;

TreeNode* xParent = nullptr, * yParent = nullptr;

 

class Solution {

public:

    bool isCousins(TreeNode* root, int x, int y)

  {

        queue<TreeNode*> q;

        q.push(root);

 

        int xDepth = -1, yDepth = -1;

        TreeNode* xParent = nullptr, * yParent = nullptr;

 

        int depth = 0;

        while (!q.empty()) {

            int sz = q.size();

            while (sz--) {

                TreeNode* node = q.front();

                q.pop();

 

                if (node->left) {

                    q.push(node->left);

                    if (node->left->val == x) {

                        xDepth = depth + 1;

                        xParent = node;

                    } else if (node->left->val == y) {

                        yDepth = depth + 1;

                        yParent = node;

                    }

                }

 

                if (node->right) {

                    q.push(node->right);

                    if (node->right->val == x) {

                        xDepth = depth + 1;

                        xParent = node;

                    } else if (node->right->val == y) {

                        yDepth = depth + 1;

                        yParent = node;

                    }

                }

            }

            depth++;

 

            if (xDepth != -1 && yDepth != -1) {

                return xDepth == yDepth && xParent != yParent;

            }

        }

        return false;

    }

};

 

lc374.二分

/** 
* Forward declaration of guess API.
* @param  num   your guess
* @return          -1 if num is higher than the picked number
*                  1 if num is lower than the picked number
*               otherwise return 0
* int guess(int num);
*/

class Solution {
public:
int guessNumber(int n) {
int l = 1;
int r = n;
while(l <= r){
int mid = l + (r - l)/2;
if(guess(mid) == 0){
return mid;
}
if(guess(mid) == 1){
l = mid + 1;
}else{
r = mid - 1;
}
}
return 0;
}
};

 

lc17.13

dp / dfs memo

class Solution {
public:
int respace(vector<string>& dictionary, string sentence) {
unordered_set<string> dict(dictionary.begin(), dictionary.end());
int n = sentence.size();
// dp[i] 表示前 i 个字符未识别的最少字符数
vector<int> dp(n + 1, n); 
dp[0] = 0; 
for (int i = 1; i <= n; ++i) {
// 先假设当前字符不匹配,未识别字符数加 1
dp[i] = dp[i - 1] + 1; 
for (int j = 0; j < i; ++j) {
if (dict.count(sentence.substr(j, i - j))) {
// 如果从 j 到 i - j 的子串在词典中,更新未识别字符数
dp[i] = min(dp[i], dp[j]); 
}
}
}
return dp[n]; 
}
};

memo

 

 

lc17.15

dfs+tmp set

另外单词组成的检验

for(int i=1;i<=s.size();i++)
{
if (words.count(s.substr(0, i)) && isCombinated(s.substr(i), words))

              return true;
}

class Solution {
public:
string longestWord(vector<string>& words) {
unordered_set<string> allWords(words.begin(), words.end());
string ans;


for(auto word:words)
{
auto tmpCollects = allWords;
tmpCollects.erase(word);


if(isCombinated(word,tmpCollects))
{
if (word.size() > ans.size())

                              ans = word;
if (word.size() == ans.size())

                              ans = min(ans, word);
}
}
return ans;
}
private:


bool isCombinated(string s,unordered_set<string>& words)
{
if (s.size() == 0)return true;
for(int i=1;i<=s.size();i++)
{
if (words.count(s.substr(0, i)) && isCombinated(s.substr(i), words))

              return true;
}
return false;
}
};

 

lc17.14  前k小元素

大顶堆--递减堆

class Solution {
public:
vector<int> smallestK(vector<int>& arr, int k) {
if (k == 0) return {};
priority_queue<int> pq; //默认递减堆
vector<int> ans(k);

        for (int i = 0; i < arr.size(); i ++) {
if (pq.size() < k)

          {
pq.push(arr[i]);
}
else{
if (pq.top() > arr[i])  //递减堆

               {
pq.pop();
pq.push(arr[i]); //换更小

}
}

int idx = 0;
while (! pq.empty()) {
ans[idx ++] = pq.top();
pq.pop();
}
return ans;
}
};

 

lc17.17

//以每个位置为起始,开始测试
int k = 0;
while (k < sz && s[k] == big[j + k]) 
k++;
if (k == sz) 
ret[i].push_back(j);

class Solution {
public:
vector<vector<int>> multiSearch(string big, vector<string>& smalls) 
{
int m = big.size(), n = smalls.size();
vector<vector<int>> ret(n);
for (int i = 0; i < n; i++)

         {
string s = smalls[i];
int sz = s.size();
// 如果短字符串为空,直接跳过(根据题目提示,可能不需要,但处理更鲁棒)
if (sz == 0) continue; 


for (int j = 0; j <= m - sz; j++)

          { 

           //以每个位置为起始,开始测试
                int k = 0;
while (k < sz && s[k] == big[j + k])

               { 
k++;
}
if (k == sz) 
ret[i].push_back(j);

}
}
return ret;
}
};

 

http://www.xdnf.cn/news/1310887.html

相关文章:

  • 从 Windows 到 Linux 服务器的全自动部署教程(免密登录 + 压缩 + 上传 + 启动)
  • 嵌入式硬件篇---运算放大器
  • 要想在Trae运行Java程序,该怎样配置Java环境?
  • TOGAF八步一法笔记2
  • TexStudio中的Latex,PDFLatex,XeLatex和LuaLatex的区别
  • RocketMq面试集合
  • 暴雨服务器:以定制化满足算力需求多样化
  • 小白挑战一周上架元服务——元服务开发06
  • 肖臻《区块链技术与应用》第20-22讲 - 以太坊难度调整、权益证明和智能合约
  • 415. 字符串相加
  • Java设计模式之《工厂模式》
  • 【Java web】HTTP 协议详解
  • HTTP 1.0, 2.0 和 3.0 有什么区别?
  • OpenAI TTS API + Web 前端 AudioContext 实战方案
  • (论文速读)ViDAR:视觉自动驾驶预训练框架
  • leetcode-139. 单词拆分-C
  • 中本聪思想与Web3的困境:从理论到现实的跨越
  • 从依赖到自研:一个客服系统NLP能力的跃迁之路
  • 昇腾AI自学Day2-- 深度学习基础工具与数学
  • Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
  • 异构数据库兼容力测评:KingbaseES 与 MySQL 的语法・功能・性能全场景验证解析
  • linux设备驱动之字符设备驱动
  • Python代码规范与静态检查(ruff/black/mypy + pyproject.toml + Makefile)自动化工具链介绍
  • 【LeetCode 热题 100】70. 爬楼梯——(解法二)自底向上
  • 在鸿蒙应用中快速接入地图功能:从配置到实战案例全解析
  • ISO27001 高阶架构 之 支持 -2
  • PHP域名授权系统网站源码/授权管理工单系统/精美UI/附教程
  • 广东省省考备考(第七十八天8.16)——资料分析、判断推理(强化训练)
  • Spring AMQP如何通过配置文件避免硬编码实现解耦
  • Linux -- 文件【下】