代码改变世界

随笔档案-2014年03月

[数字技巧]子集问题(寻找给定集合的所有子集)

2014-03-24 19:50 by 庸男勿扰, 2779 阅读, 收藏, 编辑
摘要: 我们定义该问题如下: 给定一个集合C,找出所有的集合C',使得C'包含于C。一、无重复元素的集合 我们首先来考虑一种简单的情形,C中的数都是各不相同的,这就意味着所产生的子集不会有重复的。 直观来说,求一个集合的子集,无非就是对每个元素进行枚举,枚举两种状态”选“还是”不选“。例如,对一个集合C,当对cur这个位置的元素进行枚举时,对剩余的元素可以递归调用这个枚举的过程,当cur为数组长度n时,代表枚举结束。 子集的解空间又叫子集树,其叶子节点所代表的状态即为所有的子集。例如,集合{1,3,5},其子集树如下所示: 如上图所示,共有8个叶子节点,代表8个子集,有了子集树,要求出 阅读全文

[LeetCode]Subsets II

2014-03-24 19:49 by 庸男勿扰, 237 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/subsets-ii/题意描述:Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,2], a solution is:[ [2]. 阅读全文

[LeetCode]Subsets

2014-03-23 23:34 by 庸男勿扰, 258 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/subsets/题意描述:Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3],... 阅读全文

[LeetCode]Pascal's Triangle

2014-03-15 23:23 by 庸男勿扰, 124 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/pascals-triangle/题意描述:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]题解: 两端为1,中间为v[i][j] = v[i-1][j-1]+v[i-1][j]; 1 class Solution { 2 public: 3 vector > ... 阅读全文

[LeetCode]Reverse Integer

2014-03-14 23:53 by 庸男勿扰, 166 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/reverse-integer/题意描述:Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought thro 阅读全文

[LeetCode]Linked List Cycle II

2014-03-14 17:12 by 庸男勿扰, 177 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/linked-list-cycle-ii/题意描述:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?题解: 这道题的v1版本是判断一个单链表是否有环,当时我们采用的是快慢指针的方法。传送门:http://www.cnblogs.com/codershell/p/3600100.. 阅读全文

[LeetCode]Binary Tree Postorder Traversal

2014-03-14 16:14 by 庸男勿扰, 118 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/binary-tree-postorder-traversal/题意描述:Given a binary tree, return thepostordertraversal 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?题解: 二叉树的后序遍历,... 阅读全文

[LeetCode]Minimum Path Sum

2014-03-14 16:06 by 庸男勿扰, 134 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/minimum-path-sum/题意描述:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.题解: 这是提到基本的动态规划题,转移方程为:dp[i][j] . 阅读全文

[LeetCode]Search Insert Position

2014-03-14 11:13 by 庸男勿扰, 127 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/search-insert-position/题意描述:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[ 阅读全文

[LeetCode]Linked List Cycle

2014-03-14 11:07 by 庸男勿扰, 253 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/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?题解: 判断一个单链表中是否有环,这是一道典型的快慢指针的题,具体概念参见百度:http://baike.baidu.com/link?url=6vrn7yMJjWonSHrzGNLZA16JoJZxrLkFuRtJH3oP3PHCBakSKKM2UQH-5xt. 阅读全文

[LeetCode]Binary Tree Preorder Traversal

2014-03-14 00:20 by 庸男勿扰, 182 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/题意描述:Given a binary tree, return thepreordertraversal 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?题解: 裸的二叉树遍历,没什么... 阅读全文

[LeetCode]Longest Common Prefix

2014-03-11 20:04 by 庸男勿扰, 126 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/longest-common-prefix/题目描述:Write a function to find the longest common prefix string amongst an array of strings.题解: 依然是一道分治法解的题,类似的还有http://www.cnblogs.com/codershell/p/3592992.html 1 class Solution { 2 public: 3 string lcp(string str1,string str2){ 4 ... 阅读全文

[LeetCode]Merge k Sorted Lists

2014-03-10 23:42 by 庸男勿扰, 348 阅读, 收藏, 编辑
摘要: 原题链接:http://oj.leetcode.com/problems/merge-k-sorted-lists/题目描述:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.题解: 这是个典型的分治题,用递归,不断平分,解决merge两个有序链表的小问题即可。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ... 阅读全文

[LeetCode]Reverse Words in a String

2014-03-09 19:02 by 庸男勿扰, 308 阅读, 收藏, 编辑
摘要: 原题链接: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".click to show clarification.Clarification:What constitutes a word?A sequence of non-space ch 阅读全文
点击右上角即可分享
微信分享提示