/** * 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) { if(head==NULL) return false; ListNode *fast_node=head…
题目描述: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 说明:不允许修改给定的链表. 分析 给出示意图: 对符号的一些说明: 公式演算: 很容易得到: m=x+y(环的长度两种表示形式); 快指针走两步,慢指针走一步.所以快指针的速度是慢指针的速度的二倍,所以相同时间内,快指针走的长度也是慢指针走的长度的二倍: 有:…
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { if(head==NULL) return NULL; ListNode *fast_no…
class Solution { public: void sort_list(ListNode* head1, ListNode* head2,int len)//在原链表上进行排序 { ListNode* cur_node1 = head1; ListNode* cur_node2 = head1; while (cur_node2->next != head2) cur_node2 = cur_node2->next; if (cur_node1->val > cur_nod…
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* rotateRight(ListNode* head, int k) { if(head==NULL)//特殊情况-空 return NULL; if…
class LRUCache { private: unordered_map<int, list<pair<int,int>>::iterator> _m; // 新节点或刚访问的节点插入表头,因为表头指针可以通过 begin 很方便的获取到. list<pair<int,int>> _list; int _cap; public: LRUCache(int capacity) : _cap(capacity) {} // O(1) // ha…
给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ bool hasCycle(struct ListNode *head) { struct ListNode *pfast,*pslow; if(NULL == head || head->next == NULL…
题目描述: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Map集合解法 思路: 创建一个map集合,key为节点,value为地址值,因为ListNode没有重写toString()方法,所以用toString()方法返回的内容作为value. 如果map中存在当前节点的toString()方法返回的内容,则存在环. /** * Definition for…
有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少只猪? 回答这个问题,并为下列的进阶问题编写一个通用算法. 进阶: 假设有 n 只水桶,猪饮水中毒后会在 m 分钟内死亡,你需要多少猪(x)就能在 p 分钟内找出“有毒”水桶?n只水桶里有且仅有一只有毒的桶. class Solution(object): def poorPigs(self, bu…
JavaScript,封装库--DOM加载 DOM加载,跨浏览器封装DOM加载,当网页文档结构加载完毕后执行函数,不等待图片音频视频等文件加载完毕 /** dom_jia_zai()函数,DOM页面加载函数,等待页面结构加载完毕后就执行函数,不需要等待页面音频视频等文件加载完毕,提高加载速度 * 参数是页面结构加载完毕后要执行的函数 * 一般前写前台js文件时,使用此方法加载DOM页面后执行代码,提高速度 **/ function dom_jia_zai(fn){ var isReady = f…