随笔分类 -  LeetCode

上一页 1 ··· 3 4 5 6 7 8 下一页
摘要:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.思考:先进行单链表复制,在定义random指针,每个结点的random指针都要遍历一次链表,时间复杂度为O(n2)。/** * Definition for singly-linked list with a random pointer. * struct RandomLi 阅读全文
posted @ 2013-11-17 03:14 七年之后 阅读(248) 评论(0) 推荐(0)
摘要: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?思考:第一步:设环长为len,快慢指针q、p相遇时,q比p多走了k*len。 第二部:p先走k*len步,p,q一起走每次一步,相遇时p比q多走了一个环距离,此时q就是环开始结点。通过分析AC的感觉真好!class Solution {public: ListNode *detectCycle(... 阅读全文
posted @ 2013-11-16 12:07 七年之后 阅读(274) 评论(0) 推荐(0)
摘要:Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?思考:快慢指针,快指针一次走两步,慢指针一次一步。若快指针跟慢指针指向同一个结点,则有环。若快指针到达链表末尾即指向NULL,说明没有环。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : ... 阅读全文
posted @ 2013-11-16 11:20 七年之后 阅读(241) 评论(0) 推荐(0)
摘要:Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example, Given {1,2,3,4}, reorder it to {1,4,2,3}.思考:[微软原题点击]。O(n2)方法这里就不贴了。因为每次要插入的结点都是尾结点,从头结点开始寻找时间都花费在查询上。题意说只可以改变.next,这算是一个提示吧。我们可以翻转待插入链表结点 阅读全文
posted @ 2013-11-16 10:49 七年之后 阅读(299) 评论(0) 推荐(0)
摘要:Given a binary tree, return the postorder traversal of its nodes' values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively?思考:原思路是为每个节点增加一个标记。这个思路先序遍历不要标记,因为当前访问结点直接出栈,不用看是否访问过。不过此思路redefinition of 'struct... 阅读全文
posted @ 2013-11-15 12:27 七年之后 阅读(261) 评论(0) 推荐(0)
摘要:Given a binary tree, return the preorder traversal of its nodes' values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial, could you do it iteratively?思考:不可以递归,那么用栈存储结点。先用递归法做,算是加深记忆。需要注意ret需要清空,不然会记录上一次测试用例,所以将先序遍历单独写出来。class Solutio... 阅读全文
posted @ 2013-11-14 22:04 七年之后 阅读(154) 评论(0) 推荐(0)
摘要:Sort a linked list using insertion sort.思考:画图帮助理解,考虑以下情况:空链表,单结点链表,两个结点(第二个结点大于或小于第一个),多个结点。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *insertionSortList(Li... 阅读全文
posted @ 2013-11-14 17:17 七年之后 阅读(270) 评论(0) 推荐(0)
摘要:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.思考:复习KMP算法。class Solution {public: char *strStr(char *haystack, char *needle) { // IMPORTANT: Please reset any member data you declared, as // the same Solut... 阅读全文
posted @ 2013-11-14 13:41 七年之后 阅读(177) 评论(0) 推荐(0)
摘要:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't mat... 阅读全文
posted @ 2013-11-13 11:11 七年之后 阅读(166) 评论(0) 推荐(0)
摘要:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for... 阅读全文
posted @ 2013-11-13 10:21 七年之后 阅读(170) 评论(0) 推荐(0)
摘要:Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.思考:考虑几种情况:空链表,单节 阅读全文
posted @ 2013-11-12 21:08 七年之后 阅读(161) 评论(0) 推荐(0)
摘要:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思考:合并双链表的基础上增加循环。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *... 阅读全文
posted @ 2013-11-12 12:47 七年之后 阅读(149) 评论(0) 推荐(0)
摘要:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((... 阅读全文
posted @ 2013-11-11 17:59 七年之后 阅读(196) 评论(0) 推荐(0)
摘要:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&qu 阅读全文
posted @ 2013-11-11 11:13 七年之后 阅读(172) 评论(0) 推荐(0)
摘要:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note: Given n will always be valid. Try to do this in one p 阅读全文
posted @ 2013-11-10 20:00 七年之后 阅读(153) 评论(0) 推荐(0)
摘要:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephon... 阅读全文
posted @ 2013-11-10 19:07 七年之后 阅读(385) 评论(0) 推荐(0)
摘要:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)The solution set must not contai 阅读全文
posted @ 2013-11-09 20:53 七年之后 阅读(449) 评论(0) 推荐(0)
摘要:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers.... 阅读全文
posted @ 2013-11-09 19:07 七年之后 阅读(188) 评论(0) 推荐(0)
摘要:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of ... 阅读全文
posted @ 2013-11-09 18:02 七年之后 阅读(213) 评论(0) 推荐(0)
摘要:const string roman[]={ "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", & 阅读全文
posted @ 2013-11-08 18:11 七年之后 阅读(171) 评论(0) 推荐(0)

上一页 1 ··· 3 4 5 6 7 8 下一页