排序:
默认
按更新时间
按访问量

[leetcode] Combination Sum IV

思路:记忆化搜索,定义函数查找以index开始,值为target的个数,顺便对nums排序减少不必要的搜索。 代码如下: class Solution { public: int combinationSum4(vector& nums, int target) { ...

2016-07-26 01:34:19

阅读数:253

评论数:0

[leetcode] Single Number III

同Single Number,异或后按照得到的结果中bit位为1的将数组分为两组,结果必然各存在于这两组中,分别异或得到结果。代码如下: vector singleNumber(vector& nums) { int tmp = 0; vector res...

2016-03-21 15:01:48

阅读数:175

评论数:0

[leetcode] Word Pattern

注意一一对应即可: class Solution { public: bool wordPattern(string pattern, string str) { int i = -1, pos = 0; unordered_map mp; ...

2016-03-19 00:15:05

阅读数:150

评论数:0

[leetcode] Self Crossing

class Solution { public: bool isSelfCrossing(vector& x) { /*solution 1: only in three cases when cross /* i-2 ...

2016-03-18 15:13:38

阅读数:158

评论数:0

[leetcode] Invert Binary Tree

和最近比较火的一条Twitter相关,题目本身其实并不难...递归和非递归代码如下: /*if(!root) return root; TreeNode* lf = root->left; root->left = invert...

2015-06-14 15:18:48

阅读数:291

评论数:0

[leetcode] Maximal Square

动态规划,dp[i][j]代表以i,j为右下角的正方形的最长边长。代码如下: int maximalSquare(vector>& matrix) { int result = 0; if(matrix.empty()) ret...

2015-06-03 16:40:36

阅读数:1072

评论数:0

[leetcode] Combination Sum III

简单题,和前面两个类似。代码如下: class Solution { public: vector> combinationSum3(int k, int n) { vector > result; vector num; df...

2015-06-03 15:49:24

阅读数:342

评论数:0

[leetcode] House Robber II

和I类似,II变成了环形,即首尾只能取一个。调用两次即可。代码如下: int rob(vector& nums) { if(nums.empty()) return 0; if(nums.size() == 1) ...

2015-05-23 18:40:53

阅读数:225

评论数:0

[leetcode] Add and Search Word - Data structure design

字典树,代码如下: class TrieNode { public: // Initialize your data structure here. TrieNode() { for(int i = 0; i < 26; ++i) n...

2015-05-23 17:46:31

阅读数:313

评论数:0

[leetcode] Word Search

dfs,代码如下: class Solution { public: bool exist(vector > &board, string word) { const int row = board.size(); const int col...

2015-05-23 17:28:29

阅读数:252

评论数:0

[leetcode] Word Search II

直接用word search暴力果断超时,根据提示可对words先构建字典树,然后dfs。代码如下: class TrieNode { public: // Initialize your data structure here. TrieNode() { for...

2015-05-23 17:27:48

阅读数:649

评论数:0

[leetcode] Course Schedule II

和Course Schedule类似,记录下结果即可。代码如下: vector findOrder(int numCourses, vector>& prerequisites) { vector result; vector into(numCou...

2015-05-15 14:41:10

阅读数:208

评论数:0

[leetcode] Implement Trie (Prefix Tree)

实现字典树基本功能,包括查找key和前缀。代码如下: class TrieNode { public: // Initialize your data structure here. TrieNode() { for(int i = 0; i < 26; +...

2015-05-08 13:59:16

阅读数:190

评论数:0

[leetcode] Reverse Linked List

链表简单题,遍历头插法即可。代码如下: ListNode* reverseList(ListNode* head) { if(!head) return head; ListNode dummy(-1); dummy.nex...

2015-05-07 14:56:48

阅读数:195

评论数:0

[leetcode] Course Schedule

图算法,判断有向图是否存在回路。拓扑排序即可,代码如下: bool canFinish(int numCourses, vector>& prerequisites) { vector into(numCourses, 0); for(int i =...

2015-05-07 14:52:21

阅读数:561

评论数:0

[leetcode] Isomorphic Strings

简单题,map记录下即可~代码如下: bool isIsomorphic(string s, string t) { if(s.length() != t.length()) return false; unordered_map mp1;...

2015-04-30 15:29:48

阅读数:239

评论数:0

[leetcode] Happy Number

简单题,模拟并记录即可~代码如下: bool isHappy(int n) { unordered_set appear; while(n != 1){ appear.insert(n); int tmp = n; ...

2015-04-22 11:01:31

阅读数:211

评论数:0

[leetcode] Bitwise AND of Numbers Range

简单题,当m和n不相等时,即区间内必存在奇偶数,故低位AND后取0,故结果为m和n从高位开始取相等的那些位得到的值。代码如下: int rangeBitwiseAnd(int m, int n) { int p = 0; while(m != n &&am...

2015-04-20 21:47:14

阅读数:260

评论数:0

[leetcode] Number of Islands

BFS或DFS,利用grid数组来标记是否访问。代码如下: int numIslands(vector> &grid) { if(grid.empty()) return 0; const int m = grid.size(...

2015-04-14 16:56:34

阅读数:230

评论数:0

[leetcode] Binary Tree Right Side View

dfs,记录已经访问到的层数即可。代码如下: vector rightSideView(TreeNode *root) { vector result; int curLevel = 0, level = -1; dfs(result, curLe...

2015-04-07 22:09:32

阅读数:194

评论数:0

提示
确定要删除当前文章?
取消 删除
关闭
关闭