This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct Rectangle { | |
int x, y, width, height; | |
}; | |
bool is_intersect(const Rectangle &r1, const Rectangle &r2); | |
Rectangle rect_intersect(const Rectangle &r1, const Rectangle &r2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <ctime> | |
using namespace std; | |
// Generate random numbers in large ranges with random number | |
// generator in small ranges | |
// For random number generator with 0/1, we can use it to generate bits | |
unsigned char zero_one() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxProduct(int A[], int n) { | |
if (n == 0) | |
return 0; | |
// record both min values and max values. Use dp | |
int max_pre = A[0]; | |
int min_pre = A[0]; | |
int max_value = A[0]; | |
int min_cur, max_cur; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for binary tree | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
void reverseWords(string &s) { | |
if(s.size() == 0) | |
return; | |
reverse(s.begin(), s.end()); | |
// reverse word by word | |
istringstream iss(s); | |
string word; | |
ostringstream oss; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<string> anagrams(vector<string> &strs) { | |
// find distribution or sort | |
vector<string> res; | |
int n = strs.size(); | |
// sort each string in string array | |
// use hash to find equal elements of two arrays or duplicate elements in one array | |
unordered_map<string, int> dict; | |
for(int i = 0; i<n; ++i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int longestValidParentheses(string s) { | |
vector<int> dp(s.size(),0); | |
int max=0; | |
for(int i=s.size()-2; i>=0; i--) | |
{ | |
if(s[i]=='(') | |
{ | |
int j=i+1+dp[i+1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> findSubstring(string S, vector<string> &L) { | |
vector<int> ret; | |
if(S.empty() || L.empty() || S.size()<L.size()*L[0].size()) | |
return ret; | |
int wordLength = L[0].size(), sumLength=L.size()*wordLength; | |
//Initilize hashtable: needFound[L[i]] is no. of occurences of L[i] in L | |
unordered_map<string, int> needFound; // may contain duplicates | |
for(int i=0; i<L.size(); ++i) needFound[L[i]]++; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
// based on two sum | |
void twoSum(vector<int> & num, vector<vector<int> > & res, int target) | |
{ | |
int n = num.size(); | |
int i = 0, j = n-1; | |
int prei = INT_MAX;// note ensure uniqueness | |
int prej = INT_MAX; | |
while(i<j) |
NewerOlder