- 博客(176)
- 资源 (8)
- 收藏
- 关注
原创 Sliding Window Maximum
使用deque存数组的索引值,保证deque中元素是降序的,即每次取front就能取到最大元素。class Solution {public: vector maxSlidingWindow(vector& nums, int k) { deque win; vector res; if(nums.
2015-08-15 11:15:52
474
原创 QuickSort 和 QuickSelection中partition注记
CTCI 5th中提到了quicksort partition中的另一种写法,感觉比算法导论上的更容易理解,更直观。其中需要注意的是:1) while(A[left] 2) pivot的选取,可以选取[start, end]中任何一个,或者随机选取。3) return left-1,是partition的左半部分的最有一个元素的Index。Quick Partition
2014-11-28 23:17:55
648
转载 autocomplete
1. using Trie with each node attached a list of suggestions.2. using
2014-11-10 21:13:17
472
转载 bit operations topics
source1: Low Level Bit Hacks You Absolutely Must Know
2014-10-30 20:15:41
452
原创 Largest Subtree Which is a Binary Search Tree (BST)
找到一个树中最大的BST subtree,题目来源:http://leetcode.com/2010/11/largest-binary-search-tree-bst-in.html代码
2014-10-30 10:54:39
485
原创 Coins in a Line
题目来源: http://leetcode.com/2011/02/coins-in-line.html二维DP。代码如下:
2014-10-29 21:29:56
457
原创 restore BST from pre-order
#include #include #include using namespace std;struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int v): val(v){}};TreeNode *restore(int min, int max, int &idx, vector a
2014-10-28 22:17:50
452
原创 find the N-th node In Inorder
1. Write a function that returns a node in a tree given two parameters: pointer to the root node and the in order traversal number of the node we want to return. The only information stored in the
2014-10-21 15:38:09
448
原创 Rotated Sorted Array(合辑)
Corner cases: 1 duplicate elements. 2. the array is not rotated at all.1. Find
2014-10-20 09:56:41
495
原创 Count index of sequence of words
problem source: http://www.careercup.com/question?id=21117662
2014-10-19 11:37:28
496
转载 (MS)Design an algorithm to find the maximum subsquare such that all four borders are filled with bla
Imagine there is a square matrix with n x n cells. Each cell is either filled with a black pixel or a white pixel. Design an algorithm to find the maximum Subsquare such that all four borders are fill
2014-10-19 10:02:58
581
原创 整数相加溢出判断
判断两个数相加overflow的代码:int add(int a, int b){ if(((a>>31) & 1) == 0 && ((b>>31) & 1) == 0) { if(a <= INT_MAX-b) return a+b; else return 88888; }
2014-10-18 16:50:53
619
原创 lower_bound和upper_bound的另一种写法
int lower_bound(int A[], int n, int target) { int left = 0, right = n-1; while(left <= right) { int mid = left+(right-left)/2; if(A[mid] < target)
2014-10-15 10:24:11
566
原创 longest substring with two unique charachters
O(n)双指针。code#include #include #include #include #include using namespace std;string longest(string str){ int longest = 0; int longest_start = -1; int occurance[256]; int count = 0
2014-10-14 18:27:46
486
原创 count duplicate in a sorted array
count insead of find. binary search.#include #include using namespace std;map blist; void count_dup(int* arr, int start, int end){ if(end == start) { blist[arr[start]] +=1;
2014-10-14 12:02:11
554
原创 CTRL+A, CTRL+C, CTRL+V
来源 http://leetcode.com/2011/01/ctrla-ctrlc-ctrlv.html原题的思路不好理解,可以考虑使用冬天
2014-10-13 21:58:49
1259
原创 Copy Binary Tree with Random Pointer
similar with the leetcode problem: copy LinkedList with random pointer
2014-10-12 19:35:16
625
转载 求分数的循环节和小数表示
参考http://www.cnblogs.com/clive/archive/2009/10/28/Circular_decimal_fraction_USCAO_2_4_5.html
2014-10-12 15:30:00
1746
转载 uva 10716 - Minimum Swap to form Palindrome
转自: http://blog.youkuaiyun.com/chenguolinblog/article/details/7908609
2014-10-07 22:03:33
500
原创 Leetcode第三刷需要注意的问题
2. Reverse Words in a String注意最后一个单词后面的空格。4. Max Points on a Line 注意slope为double类型, infinity是slope的一个特殊值。5. Sort List:需要注意fast slow pointer在只有两个节点时是否能够将list分成两部分。6. Insertion
2014-09-30 08:31:22
601
转载 Max Points on a Line
class Solution {public: int maxPoints(vector &points) { if(points.size() <= 2) return points.size(); int maxpoint = 0; unordered_map hash; f
2014-09-29 16:11:31
372
转载 Maximum Product Subarray
source: http://blog.youkuaiyun.com/sbitswc/article/details/39546719
2014-09-29 15:06:28
380
原创 suffix tree pattern matching
// A simple C++ implementation of substring search using trie of suffixes#include #include #define MAX_CHAR 256using namespace std;// A Suffix Trie (A Trie of all suffixes) Nodeclass SuffixTrie
2014-09-28 16:14:18
574
原创 Interview Set
1. You have an array of n elements, and a sum. Check if any 2 elements in the array sum to the given sum. ( Expected time complexity O(n). Use hashing)
2014-09-13 12:08:41
463
转载 Max Sum in circularly situated Values
Max Sum in circularly situated ValuesThis question was asked first in FACEBOOK On Campus for Internship.There are N trees in a circle. Each tree has a fruit value associated with it. A
2014-09-13 11:53:03
638
原创 Finding all possible simple paths (paths without cycles) between two vertices in a graph
This problem is different from traditional BFS or DFS search, as the previously visited node can be visited again, as they are on different paths.M
2014-09-12 20:28:33
616
转载 Graph Traversal (BFS vs DFS vs Stack)
BFS template: (from wiki)1 procedure BFS(G,v) is2 create a queue Q3 create a set V4 add v to V5 enqueue v onto Q6 while Q is not empty loop7 t ← Q.dequeue()
2014-09-11 15:56:18
834
原创 Text Justification
注意最后一行和只有一个word的行的特殊处理。class Solution {public: vector fullJustify(vector &words, int L) { vector result; if(words.size() == 0) return result; int start = 0;
2014-09-11 11:36:18
359
原创 Spiral Matrix
注意细节。class Solution {public: vector spiralOrder(vector > &matrix) { vector result; int row = matrix.size(); if(row == 0) return result; int col = matrix
2014-09-10 20:22:49
365
原创 Substring with Concatenation of All Words
check each possible position i to see if the substring start at position i is a
2014-09-10 18:01:19
373
原创 Reverse Integer
注意处理overflow和negative的问题。class Solution {public: int reverse(int x) { bool positive = (x>=0); x = abs(x); int result = 0; for(; x; x/=10)
2014-09-10 11:54:03
345
Data-Intensive Text Processing with MapReduce
2012-03-09
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人