【LeetCode OJ】Linked List Cycle II】的更多相关文章

Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detecting the loop using faster/slower pointers. Finding the begining of the loop. After two pointers meet in step 1, keep one pointer and set anther to th…
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space?   Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Fol…
Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster pointer goes two steps each iteration, and the slower one goes one step. If the two pointers meet some time, it follows that there is a loop; otherwise,…
Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element of the i-th row in the triangle, for 0 <= j <= i, i = 0, 1, ... And we have the recursive function for T[i][j] T[i][j] = 1, if j == 0 or j == i T[i][j…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? 这个题还是蛮考验数学推理的,不过在前一个题的基础上还是能推出结果的.这是英文一段解释,非常有帮助. First Step: A…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? 和前面那题类似,只不过这里要找到的是环开始的节点,看下面这张图(图是盗的): 当fast以及slow指针在z处相遇的时候,可以…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 思路:由[Leetcode]Linked List Cycle可知.利用一快一慢两个指针可以推断出链表是否存在环路. 如果两个指针相遇之前slow走了s步,则fast走了2s步.而且fast已经在长…
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. 思路:想法是利用两指针,一个每次移动一步,另一个每次移动两步,如果存在环则这两个指针一定会相遇(这里可以在纸上画一下,因为后一个指针移动比前一个指针快,当后一个指针在环中来到前一个指针的…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 思路 这题是Linked List Cycle的进阶版 Given a linked list, determine if it has a cycle in it. bool hasCycle(Li…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 解题: 寻找单链表中环的入口,如果不存在环则返回null. 假设单链表有环,先利用判断单链表是否有环(Linked List Cycle)的方法,找到快慢指针的交点. 假设环的入口在第K个结点处,那么此时快慢指针的交点,距离环入口,还差k个距离. 快慢指针相交后,在链表头位置同时启动另一个指针target,…
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return t…
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". LeetCode OJ supports Python now! The s…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 思路: 做过,就当复习了. 先用快慢指针判断相交,关键是环开始点的获取. 用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 判断一个链表是否有环. 题解: 设置两个指针p1和p2: p1每次走一步,p2每次走两步,如果在这个过程中,p2为空,则没有环:否则两个指针必然相遇,则有环: 接下来找环的起点,将p1挪动到链表起始,…
这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? 起初我在做这道题的时候,以为挺简单的,以为循环链表都是已头节点为循环头,结果... ~~~~(>_<)~~~~ 没考虑到链中任一个节点都可能是循环头的头节点. 一开始比较贪心,就只设置的一个指针p来判断是否循环,结果思考不充分,没考虑到第二种特殊的情况,导致错了很多次. 然后经过多次测试后终于成…
Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is asking for flatterning a binary tree to linked list by the pre-order, therefore we could flatten tree from the root. For each node, we link it with its n…
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from the tree root to traverse the tree level by level. The python code is as follows. # Definition for a binary tree node # class TreeNode: # def __init__(s…
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Path Sum. However, since the problem is asking for all possible root-to-leaf paths, so we should use BFS but not DFS. The python code is as follows. # D…
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... Exactly same to Populating Next Right Pointers in Each Node.…
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this problem using Greedy Algorithm, which only scan the prices list once. The worst-case running time is O(n). According to the problem description, we know t…
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Word Ladder I, which uses a double-direction BFS. However, the difference is that we need to keep track of all paths during the double-direction BFS in o…
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by using Dynamic Programming. Optimal Sub-structure Assume a string S has the palindrome minimum cuts n, and S = W1 + W2 + ... + Wn where Wi is a palindro…
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Number. Suppose we have following (3m+1) numbers in the array A: x0, x1, x1, x1, ..., xm, xm, xm We are asked to find out the value of x0. However we ca…
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the word break problem, so the solution is based on the discussion in Word Break. We also use DP to solve the problem. In this solution, A[i] is not a bool…
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 思路: 第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b. 因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!). 我们发现L=b+c=a+b,也就是说,从一开始到二者第一次相遇,…
Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a auxilar function that convert a linked list to a node with following properties: The node is the mid-node of the linked list. The node's left child i…
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list with random pointer, realy simple problem, solved in three steps using a hash map: Create a new head, duplicating the head (return NULL for case head=…
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个子串都是回文串 [思路] 求一个字符串的所有子串是否是回文串,O(n^2) dp从后往前推 vector<vector<bool> > p(len,vector<bool>(len)); ;i<len-;i++){ ;j<len-;j++){ if(j!=i)…
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the tree, and for each node we check if current_node.val > prev_node.val. The code is as follows. # Definition for a binary tree node # class TreeNode: #…
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder traversal of a binary search tree should be a sorted array. Therefore, we can compare each node with its previous node in the inorder to find the two…