2013年7月4日
摘要: 题目:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"解法:leetcode上的解法很赞。 其实这也是利用的递归的分支。构建了一树状结构并遍历,叶子节点就是valid 阅读全文
posted @ 2013-07-04 17:56 lichen782 阅读(1087) 评论(0) 推荐(0) 编辑
摘要: 题目:Given a linked list, remove thenthnode 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.就是让你删除单链表倒数第n个节点,同时希望能只遍历一次。解法一: 如果不遍历完所有节点,怎么知道倒数第n个在哪里呢? 阅读全文
posted @ 2013-07-04 15:50 lichen782 阅读(547) 评论(0) 推荐(0) 编辑
摘要: 题目:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2)额外的要求是不能返回重复的triplets,返回的a,b,c的顺序要是非递减的。解法一:首先想一下,三个数相加,要为0的话... 阅读全文
posted @ 2013-07-04 11:47 lichen782 阅读(1785) 评论(0) 推荐(0) 编辑