当前位置: 首页 > 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.dtcms.com/a/334215.html

相关文章:

  • [系统架构设计师]系统质量属性与架构评估(八)
  • 解锁JavaScript性能优化:从理论到实战
  • 【完整源码+数据集+部署教程】太阳能面板污垢检测系统源码和数据集:改进yolo11-RVB-EMA
  • 地级市+省级气候政策不确定性指数(2000-2023年)-实证数据
  • ollama 自定义模型
  • imx6ull-驱动开发篇27——Linux阻塞和非阻塞 IO(上)
  • 【JS】认识并实现一个chrome扩展程序
  • 如何在 MacOS 上安装 SQL Server
  • MySQL完整重置密码流程(针对 macOS)
  • 硬核北京 | 2025世界机器人大会“破圈”,工业智能、康养科技…… 亦庄上演“机器人总动员”
  • Flink Sql 按分钟或日期统计数据量
  • 中本聪思想与Web3的困境:从理论到现实的跨越
  • 存算分离与云原生:数据平台的新基石
  • 基于Kubernetes亲和性与反亲和性的Pod调度优化实践指南
  • Linux上配置环境变量
  • 从频繁告警到平稳发布:服务冷启动 CPU 风暴优化实践01
  • Trae中`settings.json`文件的Java配置项功能详解(一)
  • Camera相机人脸识别系列专题分析之十九:MTK ISP6S平台FDNode原生代码
  • 【vscode使用说明】
  • Vue中的数据渲染【4】
  • Docker自定义镜像
  • 138-基于FLask的重庆市造价工程信息数据可视化分析系统
  • Chrome腾讯翻译插件transmart的安装
  • RK3588芯片在AR眼镜中的核心技术优势是什么?
  • VS Code配置MinGW64编译ALGLIB库
  • 新字符设备驱动实验
  • pytest tmpdir fixture介绍(tmpdir_factory)(自动在测试开始前创建一个临时目录,并在测试结束后删除该目录)
  • c# WebAssembly,在网页上能运行多线程,异步,锁,原子加,减等代码吗
  • springboot集成websocket
  • css实现圆角+边框渐变+背景半透明