怎么创建网站 免费的,网站怎么做商家定位,租用网络服务器的价格,php建站系统哪个好1--环形链表II 主要思路#xff1a; 快慢指针#xff0c;快指针每次走两步#xff0c;慢指针每次走一步#xff1b; 第一次相遇时#xff0c;假设慢指针共走了 f 步#xff0c;则快指针走了 2f 步#xff1b; 假设起点到环入口结点的长度为 a#xff08;不包括入口结点…1--环形链表II 主要思路 快慢指针快指针每次走两步慢指针每次走一步 第一次相遇时假设慢指针共走了 f 步则快指针走了 2f 步 假设起点到环入口结点的长度为 a不包括入口结点环的结点数为 b快指针比慢指针多走的步数肯定全在环中则有 2f - f f nb则慢指针共走了 nb 步 由于慢指针共走了 nb 步而起点到环入口结点的步数为 a则实际慢指针在环内走了 nb - a 步 此时让慢指针从起点重新出发走 a 步每次走 1 步快指针从相遇的地方出发每次也走 1 步快慢指针必然在环入口结点相遇因此快指针相当于也走了 a 步恰好与 nb - a 步互补构成完整圈数的 nb 环 #include iostream
#include vectorstruct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};class Solution {
public:ListNode *detectCycle(ListNode *head) {if(head nullptr) return head;ListNode *slow head;ListNode *fast head;while(fast ! NULL fast-next ! NULL){fast fast-next-next;slow slow-next;if(fast slow) break; // 第一次相遇}if(fast NULL || fast-next NULL) return NULL; // 没有环// 从头开始走slow head;while(slow ! fast){slow slow-next;fast fast-next;}// 第二次相遇就是环的入口return slow;}
};int main(int argc, char *argv[]) {// head [3, 2, 0, -4], pos 1ListNode *Node1 new ListNode(3);ListNode *Node2 new ListNode(2);ListNode *Node3 new ListNode(0);ListNode *Node4 new ListNode(-4);Node1-next Node2;Node2-next Node3;Node3-next Node4;Node4-next Node2;Solution S1;ListNode* res S1.detectCycle(Node1);if(res ! nullptr) std::cout res-val std::endl;else std::cout nullptr std::endl;return 0;
}
2--LRU缓存 主要思路 基于双向链表和哈希表 访问元素时若元素不存在则返回若元素存在则将元素记录并将元素移动到双向链表头部确保访问热度最高的元素放在双向链表头部访问热度最低的元素放在双向链表尾部 插入元素时若元素不存在当容量已满时先移除双向链表尾部的元素再将新元素插入到双向链表头部若元素存在则取出元素并更新元素的值将更新后的元素插入到双向链表头部 #include iostream
#include unordered_mapclass LRUCache {
public:struct ListNode{ListNode(int key, int val){this-key key;this-val val;}ListNode* pre nullptr;ListNode* next nullptr;int val 0;int key 0;};LRUCache(int capacity) {this-cap capacity; // 容量head new ListNode(-1, -1);tail new ListNode(-1, -1);head-next tail;tail-pre head;}int get(int key) {if(hash.count(key) 0) return -1; // 元素不存在ListNode* ptr hash[key]; // 取出元素remove(ptr); // 从双向链表中删除元素insert(ptr); // 将元素插入到双向链表头部return ptr-val; // 返回元素的值}void put(int key, int value) {if(hash.count(key) 0){ // 元素不存在if(hash.size() cap){ // 容量已满ListNode* ptr tail-pre;remove(ptr); // 去除尾部节点hash.erase(ptr-key);delete(ptr);}ListNode* new_node new ListNode(key, value); // 新建节点insert(new_node); // 插入新节点到头部hash[new_node-key] new_node;return;}else{ // 元素存在ListNode* ptr hash[key]; // 取出元素ptr-val value; // 更新元素remove(ptr); // 先删除元素insert(ptr); // 再将元素插入到头部return;}}void remove(ListNode* ptr){// 取出前驱和后驱元素ListNode* a ptr-pre;ListNode* b ptr-next;// 更新前驱和后驱元素的指向a-next b;b-pre a;ptr-pre ptr-next nullptr;}void insert(ListNode* ptr){ListNode* tmp head-next; // 头指针的下一个元素// 将元素插入到双向链表头部ptr-pre head;head-next ptr;ptr-next tmp;tmp-pre ptr;}
private:int cap 0; // 容量std::unordered_mapint, ListNode* hash; // 哈希表ListNode* head; // 双向链表哨兵头节点ListNode* tail; // 双向链表哨兵尾节点
};int main(int argc, char argv[]){return 0;
} 文章转载自: http://www.morning.cwpny.cn.gov.cn.cwpny.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.sgrwd.cn.gov.cn.sgrwd.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.mwmxs.cn.gov.cn.mwmxs.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.lpzqd.cn.gov.cn.lpzqd.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.wfqcs.cn.gov.cn.wfqcs.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.jfbbq.cn.gov.cn.jfbbq.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.lxhny.cn.gov.cn.lxhny.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.4r5w91.cn.gov.cn.4r5w91.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.smmrm.cn.gov.cn.smmrm.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.qkbwd.cn.gov.cn.qkbwd.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.tmjhy.cn.gov.cn.tmjhy.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.xxwfq.cn.gov.cn.xxwfq.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.nzfyx.cn.gov.cn.nzfyx.cn http://www.morning.ylklr.cn.gov.cn.ylklr.cn http://www.morning.nfqyk.cn.gov.cn.nfqyk.cn http://www.morning.kkrnm.cn.gov.cn.kkrnm.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.litao4.cn.gov.cn.litao4.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn