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: | |
//recursive; push one candidate C to a vector, then find new target = target - C | |
// remember which is visited; if visited, just neglect in next finding | |
vector<vector<int> > combinationSum2(vector<int> &candidates, int target) { | |
vector<vector<int> > vvInt; | |
set<vector<int> >svInt; | |
vector<int> oneCombination; | |
vector<bool> visited((int)candidates.size(), false); | |
sort(candidates.begin(), candidates.end()); |
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: | |
//recursive; push one candidate C to a vector, then find new target = target - C | |
vector<vector<int> > combinationSum(vector<int> &candidates, int target) { | |
vector<vector<int> > vvInt; | |
set<vector<int> >svInt; | |
vector<int> oneCombination; | |
sort(candidates.begin(), candidates.end()); | |
getCombinationSum(candidates, target, oneCombination, svInt); | |
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
/* | |
Given an absolute path for a file (Unix-style), simplify it. | |
For example, | |
path = "/home/", => "/home" | |
path = "/a/./b/../../c/", => "/c" | |
*/ | |
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: | |
// two ways: | |
// 1. iterative, O(n): sum from left; if <0, reset to 0; get max() at every step | |
// 2. divide and conquer: | |
// max = max(left), or max(right), or max(cross_left_right) | |
// cross_left_right is also to get max_from_middle_to_left, and max_from_middle+1_to_right | |
/*int maxSubArray(int A[], int n) { | |
if (n <= 0) | |
return 0; |
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
/* | |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: | |
Integers in each row are sorted from left to right. | |
The first integer of each row is greater than the last integer of the previous row. | |
For example, | |
Consider the following matrix: |
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) {} | |
* }; | |
* Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. | |
For example: |
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: | |
/* | |
Surrounded Regions | |
Total Accepted: 12671 Total Submissions: 89715 | |
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. | |
A region is captured by flipping all 'O's into 'X's in that surrounded region. |
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
/* | |
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. | |
For example, | |
Given [100, 4, 200, 1, 3, 2], | |
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. | |
Your algorithm should run in O(n) complexity. | |
*/ | |
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
/* | |
There are N children standing in a line. Each child is assigned a rating value. | |
You are giving candies to these children subjected to the following requirements: | |
Each child must have at least one candy. | |
Children with a higher rating get more candies than their neighbors. | |
What is the minimum candies you must give? | |
*/ |
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<vector<int> > combine(int n, int k) { | |
vector<vector<int> > vvInt; | |
set<vector<int> > svInt; //to avoid duplications like [4,1] and [1 4] | |
if ( n <= 0 || k < 0) | |
return vvInt; | |
//select all combinations, but only select those with k elemetns | |
int cnt = 0; |
NewerOlder