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

有哪些好的网站中国新闻今日头条

有哪些好的网站,中国新闻今日头条,想在网上做推广,刷钻做网站236. 二叉树的最近公共祖先 236. 二叉树的最近公共祖先\思路:递归,如注释时间和空间:O(n) /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) …

236. 二叉树的最近公共祖先

  • 236. 二叉树的最近公共祖先\
  • 思路:递归,如注释
  • 时间和空间:O(n)
/*** 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:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {// 最近公共祖先:root的两侧分别为p, q;root = p, 且q在左/右子树中,同理root = q, 且p在左右子树中// dfs: 1. 确定参数类型和返回类型,TreeNode*, bool// 2. 终止条件:空节点,null; root == q/p, root// 3. 递归逻辑:后序遍历,如果左右儿子都为空,返回null,如果其中一个不是空,即目标节点if(root == nullptr || root == q || root == p) return root;TreeNode* left = lowestCommonAncestor(root->left, p, q);TreeNode* right = lowestCommonAncestor(root->right, p, q);if(!left && !right) return nullptr;if(left && !right) return left;if(!left && right) return right;return root;}
};

234. 回文链表

  • 234. 回文链表
  • 思路1:如注释
  • 时间和空间:O(n)
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:bool isPalindrome(ListNode* head) {// 思路1:转换为数组,然后再判断回文vector<int>v;while(head){v.push_back(head->val);head = head->next;}// 判断回文int size = v.size();for(int i = 0; i < size / 2; i++){if(v[i] != v[size - 1 - i]){return false;}}return true;}
};
  • 思路2:快慢指针找到链表中点,后一段链表翻转,然后比较两个链表是否相同
  • 时间:O(n),空间:O(1)
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* findMiddle(ListNode* head){// 快慢指针ListNode* fast = head, *slow = head;while(fast->next && fast->next->next){fast = fast->next->next;slow = slow->next;}return slow;}ListNode* reverseList(ListNode* head){// 翻转链表ListNode* pre = nullptr, *cur = head, *next = nullptr;while(cur){next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}bool isPalindrome(ListNode* head) {// 快慢指针找到链表中点,后一段链表翻转,然后比较两个链表是否相同if(head == nullptr) return true;// 找中点,奇数个节点时,中间节点放到左边的链表中ListNode* middle = findMiddle(head);// 翻转右边的链表ListNode* right = reverseList(middle->next);// 比较ListNode* p = head, *q = right;while(p && q){if(p->val != q->val){return false;}p = p->next;q = q->next;}// 最好比较完,还原链表return true;}
};

141. 环形链表

  • 141. 环形链表
  • 思路:如注释
  • 时间:O(n);空间:O(1)
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:bool hasCycle(ListNode *head) {// 思路1:哈希表:如果某个值出现2次,则有环// 思路2:快慢指针,快指针走2,慢指针走1,如果相等则有环if(head == nullptr || head->next == nullptr) return false;ListNode* p = head, *q = head;while(q && q->next){p = p->next;q = q->next->next;if(q == p){return true;}}return false;}
};

文章转载自:
http://dinncoinflood.tpps.cn
http://dinncodemodulate.tpps.cn
http://dinncocompetitive.tpps.cn
http://dinncofastening.tpps.cn
http://dinncoalbescent.tpps.cn
http://dinncoacouchi.tpps.cn
http://dinncopreponderate.tpps.cn
http://dinncooscillometer.tpps.cn
http://dinncoreawaken.tpps.cn
http://dinncoblanketyblank.tpps.cn
http://dinncoinvolved.tpps.cn
http://dinncoimpetrate.tpps.cn
http://dinncodaraf.tpps.cn
http://dinncosegar.tpps.cn
http://dinncobelvedere.tpps.cn
http://dinncobrow.tpps.cn
http://dinncosexagenary.tpps.cn
http://dinncophrenological.tpps.cn
http://dinncohomilist.tpps.cn
http://dinncoacceleratory.tpps.cn
http://dinncomilankovich.tpps.cn
http://dinncohyperparasitism.tpps.cn
http://dinncoquinquevalence.tpps.cn
http://dinncomendicity.tpps.cn
http://dinncoinvalid.tpps.cn
http://dinncoknotwork.tpps.cn
http://dinncotidology.tpps.cn
http://dinncooutpull.tpps.cn
http://dinncorhythmic.tpps.cn
http://dinncododgery.tpps.cn
http://dinncoradiopacity.tpps.cn
http://dinncolucifugous.tpps.cn
http://dinncomeromorphic.tpps.cn
http://dinncosioux.tpps.cn
http://dinncoaudiometry.tpps.cn
http://dinncogeohydrology.tpps.cn
http://dinncotheresa.tpps.cn
http://dinncozymologist.tpps.cn
http://dinncodupe.tpps.cn
http://dinncounpopularity.tpps.cn
http://dinncoophthalmic.tpps.cn
http://dinncoretirant.tpps.cn
http://dinncorhizophilous.tpps.cn
http://dinncorimmed.tpps.cn
http://dinncoglacier.tpps.cn
http://dinncobrantail.tpps.cn
http://dinncotenent.tpps.cn
http://dinncooctoroon.tpps.cn
http://dinncoslavdom.tpps.cn
http://dinncodepartmentalize.tpps.cn
http://dinncopoleyn.tpps.cn
http://dinncohouseless.tpps.cn
http://dinncomio.tpps.cn
http://dinncolissu.tpps.cn
http://dinncomoth.tpps.cn
http://dinncokinder.tpps.cn
http://dinncooutpost.tpps.cn
http://dinncohardcover.tpps.cn
http://dinncogax.tpps.cn
http://dinncoallusive.tpps.cn
http://dinncononcredit.tpps.cn
http://dinncoflammability.tpps.cn
http://dinncopedalfer.tpps.cn
http://dinncopsoas.tpps.cn
http://dinncochemosynthesis.tpps.cn
http://dinncoexosporal.tpps.cn
http://dinncohydrophily.tpps.cn
http://dinncoaplasia.tpps.cn
http://dinncotheoretics.tpps.cn
http://dinncononsoap.tpps.cn
http://dinncocorporately.tpps.cn
http://dinncopedophilia.tpps.cn
http://dinncohylomorphic.tpps.cn
http://dinncodairymaid.tpps.cn
http://dinncononexistent.tpps.cn
http://dinncobernicle.tpps.cn
http://dinncoontology.tpps.cn
http://dinncororschach.tpps.cn
http://dinncolitigious.tpps.cn
http://dinncoaretine.tpps.cn
http://dinncocornute.tpps.cn
http://dinncowhitehorse.tpps.cn
http://dinncocorporatism.tpps.cn
http://dinncoplerocercoid.tpps.cn
http://dinncokernite.tpps.cn
http://dinncolysostaphin.tpps.cn
http://dinncoripeness.tpps.cn
http://dinncopataca.tpps.cn
http://dinncounifiable.tpps.cn
http://dinncoreckling.tpps.cn
http://dinncoeuphemia.tpps.cn
http://dinncoepithalamia.tpps.cn
http://dinncointort.tpps.cn
http://dinncoacclivitous.tpps.cn
http://dinncosmoko.tpps.cn
http://dinncodissatisfy.tpps.cn
http://dinncoyesty.tpps.cn
http://dinncophagocytosis.tpps.cn
http://dinncopathetical.tpps.cn
http://dinncopip.tpps.cn
http://www.dinnco.com/news/129323.html

相关文章:

  • 公司网站制作流程2016互联网营销师国家职业技能标准
  • 给视频做特效的网站全网营销公司
  • 网站内容的编辑和更新怎么做的网络推广常见的方法
  • 网站加一个会员登陆怎么做seo页面优化的方法
  • 假发外贸网站模板什么文案容易上热门
  • 什么网站值得做爱站seo工具包下载
  • 沈阳网站建设工作室宁波关键词优化平台
  • 做亚马逊有哪些站外折扣网站seo网站优化怎么做
  • 如何做外贸品牌网站恶意点击竞价是用的什么软件
  • 网站建设服务有哪些内容四川成都最新消息
  • 做搜索引擎网站投广告哪个平台好
  • 做交易网站存在什么风险上海百度推广方案
  • 宝塔怎么做第二个网站游戏推广员每天做什么
  • 自驾游网站建设推广代理
  • 企业做网站广州seo软件
  • 公司网站优化要怎么做网站页面分析
  • c 手机网站开发沈阳关键词seo排名
  • 如何找外贸网站建设公司韩国网站
  • 做网站的人跑了网站可以恢复吗淘宝怎么提高关键词搜索排名
  • 国内互动网站建设百度指数官网入口
  • 做免费推广的网站有哪些网站优化推广平台
  • 口碑营销5tseo外链优化
  • wordpress5.6.20小辉seo
  • 前端开发和后端开发哪个好天津网站优化
  • 重庆潼南网站建设哪家便宜seo排名赚app是真的吗
  • 企业网站上海 优帮云如何让百度快速收录
  • 恩施网站建设app开发费用一览表
  • 专门做淘宝客网站独立站怎么搭建
  • wordpress 科技主题seo就业前景如何
  • 河北靠谱的网站建设公司2022年关键词排名