ÔĶÁÉÏÒ»¸öÖ÷Ìâ :: ÔĶÁÏÂÒ»¸öÖ÷Ìâ
×÷Õß
ÕýÎÄ
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:11
LeetCode (used to call i has 1337 code) is a social platform for preparing IT technical interviews. We strive to provide you with the best learning experience in preparing interviews for companies in the IT industry.
To be successful in a technical interview, we believe it is mainly repeating these three important steps:
Code. Read. Discuss.
We strive to provide you the LeetCode platform as the ultimate solution for preparing technical interviews.
1. Code -> Code solution using the Online Judge system.
2. Read -> Read high quality article featuring in-depth thought process. Also read other LeetCoders¡¯ code.
3. Discuss -> Discuss your thoughts about the problem with other LeetCoders.
We hope that through our platform, you will grow into a LeetCoder. Not only will you be successful in all of your interviews, and most importantly, you will be a better coder in general !
This is Part 3 £¨µÚÈý²¿·Ö£©
µÚÒ»²¿·Ö£º www.westca.com/Forums/...inese.html
µÚ¶þ²¿·Ö£º www.westca.com/Forums/...inese.html
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
ÉÏÒ»´ÎÓÉwebdriverÓÚ2014-9-25 11:50Ð޸ģ¬×ܹ²ÐÞ¸ÄÁË2´Î
Â¥Ö÷ |
µçÌÝÖ±´ï
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:12
Problem Name: Path Sum 2
Description:
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
ɳ·¢ |
·µ»Ø¶¥¶Ë
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:13
Now comes the solution (in C++) ...
DFS.
´úÂë:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int>> res;
vector<int> path;
pathSumRe(root, sum, res, path);
return res;
}
void pathSumRe(TreeNode *root, int sum, vector<vector<int>> &res, vector<int> &path)
{
if (!root) return;
if (!root->left && !root->right)
{
if (sum == root->val)
{
path.push_back(root->val);
res.push_back(path);
path.pop_back();
}
return;
}
path.push_back(root->val);
pathSumRe(root->left, sum - root->val, res, path);
pathSumRe(root->right, sum - root->val, res, path);
path.pop_back();
}
};
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
°åµÊ |
·µ»Ø¶¥¶Ë
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:13
Problem Name: Permutations
Description:
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
µØ°å |
·µ»Ø¶¥¶Ë
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:14
Now comes the solution (in C++) ...
dfs...
´úÂë:
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> permute(vector<int> &num) {
res.clear();
vector<bool> avail(num.size(), true);
vector<int> pum;
permuteRe(num, avail, pum);
return res;
}
void permuteRe(const vector<int> &num, vector<bool> &avail, vector<int> &pum)
{
if (pum.size() == num.size())
{
res.push_back(pum);
return;
}
for (int i = 0; i < num.size(); ++i)
{
if (avail[i])
{
avail[i] = false;
pum.push_back(num[i]);
permuteRe(num, avail, pum);
pum.pop_back();
avail[i] = true;
}
}
}
};
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
5 ¥ |
·µ»Ø¶¥¶Ë
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:14
Now comes the solution (in C++) ...
...
´úÂë:
class Solution {
public:
string getPermutation(int n, int k) {
string num, res;
int total = 1;
for (int i = 1; i <= n; ++i)
{
num.push_back(i + '0');
total *= i;
}
k--;
while (n)
{
total /= n;
int i = k / total;
k %= total;
res.push_back(num[i]);
num.erase(i, 1);
n--;
}
return res;
}
};
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
6 ¥ |
·µ»Ø¶¥¶Ë
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:16
Problem Name: Permutations II
Description:
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
7 ¥ |
·µ»Ø¶¥¶Ë
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:17
Now comes the solution (in C++) ...
dfs...
´úÂë:
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> permuteUnique(vector<int> &num) {
res.clear();
sort(num.begin(), num.end());
bool avail[num.size()];
memset(avail, true, sizeof(avail));
vector<int> pum;
permuteUniqueRe(num, pum, avail);
return res;
}
void permuteUniqueRe(vector<int> &num, vector<int> &pum, bool avail[])
{
if (pum.size() == num.size())
{
res.push_back(pum);
return;
}
int last_index = -1;
for (int i = 0; i < num.size(); ++i)
{
if (!avail[i]) continue;
if (last_index != -1 && num[i] == num[last_index]) continue;
avail[i] = false;
pum.push_back(num[i]);
permuteUniqueRe(num, pum, avail);
pum.pop_back();
avail[i] = true;
last_index = i;
}
}
};
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
8 ¥ |
·µ»Ø¶¥¶Ë
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:17
Problem Name: Plus One
Description:
Given a number represented as an array of digits, plus one to the number.
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
9 ¥ |
·µ»Ø¶¥¶Ë
webdriver (Ö»¿´´ËÈË )
ʱ¼ä: 2014-9-24 02:18
Now comes the solution (in C++) ...
...
´úÂë:
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int carry = 1, N = digits.size();
for (int i = N-1; i >= 0 && carry; --i)
{
int sum = carry + digits[i];
carry = sum / 10;
digits[i] = sum % 10;
}
if (carry == 1)
digits.insert(digits.begin(), 1);
return digits;
}
};
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
10 ¥ |
·µ»Ø¶¥¶Ë
ÂÛ̳Ê×Ò³
-> ITÈËÉú
ËùÓеÄʱ¼ä¾ùΪ ÃÀ¹ú̫ƽÑóʱ¼ä
µÚ1 Ò³£¬¹²5 Ò³
·ÖÒ³: 1 , 2 , 3 , 4 , 5 ÏÂÒ»Ò³
×¢£ºÒÔÉÏÂÛ̳ËùÓз¢ÑÔ½ö´ú±í·¢ÌûÕ߸öÈ˹۵ã, ²¢²»´ú±í±¾Õ¾¹Ûµã»òÁ¢³¡, ¼ÓÎ÷Íø¶Ô´Ë²»¸ºÈκÎÔðÈΡ£ Ͷ×ÊÀí²Æ¼°Âò·¿Âô·¿°æÃæµÄÌû×Ó²»¹¹³ÉͶ×ʽ¨Ò顣Ͷ×ÊÓзçÏÕ£¬ÔðÈÎÇë×Ô¸º ¶Ô¶þÊÖÂòÂôÖеÄÐé¼ÙÐÅÏ¢£¬ÂòÂôÖеľÀ·×µÈ¾ùÓë±¾Õ¾Î޹ء£
Äú²»ÄÜ ÔÚ±¾ÂÛ̳·¢±íÐÂÖ÷Ìâ Äú²»ÄÜ ÔÚ±¾ÂÛ̳»Ø¸´Ö÷Ìâ Äú²»ÄÜ ÔÚ±¾ÂÛ̳±à¼×Ô¼ºµÄÎÄÕ Äú²»ÄÜ ÔÚ±¾ÂÛ̳ɾ³ý×Ô¼ºµÄÎÄÕ Äú²»ÄÜ ÔÚ±¾ÂÛ̳·¢±íͶƱ Äú²»ÄÜ ÔÚÕâ¸öÂÛ̳Ìí¼Ó¸½¼þ Äú¿ÉÒÔ ÔÚÕâ¸öÂÛ̳ÏÂÔØÎļþ
ÂÛ̳תÌø:
Ñ¡ÔñÒ»¸öÂÛ̳
½¹µã°å¿é
¼ÓÎ÷Öªºõ
·¢²¼ÇóÖú
Âò·¿Âô·¿
ÃÀÀöµÄ·ÆɳºÓ¹È
µØ²úͶ×Ê
¼ÓÎ÷²ËÔ°
É̺£Î¢À½
×â·¿ÄÇЩʶù
¼Ó¹úÒ»¼ÒÇ×
ÔÛÃDZ¾ÄñÈ
θ绪²»ÃßÒ¹
ÈÕÔµ±¿Õ
ÐÝÏÐLangley
ÆÐÌáÊ÷ÏÂ
Éñ°®ÊÀÈË
ËØÀïмÒÔ°
°×ʯ»ªÈË
¸èÉùÏìÆð
¶ÁÊéɳÁú
µ¥ÉíÇéÔµ
ÐÂʱ´úÖ®¹â
»¶ÓÀ´ÐÂÎ÷Ãô
ÎÒ°®ÁÐÖÎÎÄ
¸ß¹óÁÖµÄÅóÓÑÃÇ
ÖÐӢ˫Óï°àÔÚWalton Elementary
Éú»î»¥Öú
·¨ÂÉ̽Ë÷
Ò½Áƽ¡¿µ
×¼±¸ÑøÀÏ
·ðµÀ¾»ÍÁ.ÖÐÒ½ÑøÉú
ÌìÏÂÊÕ²Ø
¼õ·Ê
Ͷ×ÊÀí²Æ
Ç××Ó½ÌÓý
Öí±¦±¦¾ãÀÖ²¿
Æû³µÌìµØ
δåJeepÖ®¼Ò
Ç®±Ò½»Á÷Сվ (Coin Community)
Ãâ·Ñ¸öÈ˹ã¸æ
»áÔ±Ö®¼Ò
±±ÃÀ¶ÜÖ®¼Ò
Á÷Ð
³ÔºÈÓëÍæÀÖ
ÍæÍæÀÖÀÖ
¼ÓÎ÷ȺӢÓÎͧ»á
½á°éÂÃÓÎ
ÌøÎè¾ãÀÖ²¿
±±ÃÀHiking
³Ô³ÔºÈºÈ
ÉçÍÅÖ®´°
ÄÏÔÁ²è¾Ó
½»Í¨´óѧУÓÑ»á
¸£½¨Í¬ÏçÔڴ˾ۼ¯
¼ÓÄôóËÄ´¨Í¬ÏçÁªÒê»á
ͬ¼Ã´óѧθ绪УÓÑ»á
¾«²ÊÌùͼ
¾«²ÊÁ¬»·»
¿ªÐÄһЦ
¹ú¼ÒÓë³ÇÊÐ
ÐÂÎÅʱÆÀ
¾üÊÂÌìµØ
½Ìʦ°Õ¹¤
´óÎÂÊÐÑ¡
ÇéϵÖйú
ÃàÃàÏçÒô
Ïã¸ÛÕ¼ÖÐ
ÁëÄÏÈ˼Ò
ÓÆÓÆ»ªÏÄ
Ö»¸ÕÉϺ£»°
Î÷ÑÅͼҹδÑë
°®ºÃÓëɳÁú
﫫ﵯ
ÉãÓ°½»Á÷
°×ʯÕòÉãÓ°½»Á÷
ÊÓÌýÊÀ½ç
·èÈËÔº - ÄãµÄϲȵ³²
µç×ÓµçÍæ
Twitter ½»Á÷
Æ»¹ûÀÖÔ°
androidµÄÐÇ¿Õ
ÎÞÏÞÊÖ»ú¼Æ»®
²¿Âä³åÍ»
´óÊý¾ÝÂÛ̳
ÍøÕ¾½¨Éè
µß¸²Ð¡×顪LINUX
ITÈËÉú
iTalkBBÌÖÂÛ×é
English Corner
Éú»îÓë½ÌÓý
ÃÀºÃ¼Ò¾Ó
¾«ÖÂÉú»î
ÒÆÃñ×ÊѶ
θ绪Áôѧ
³èÎïÀÖÔ°
Ö¸»Óë´´Òµ
¾«Ã÷Ïû·Ñ
ìŲÊÖ鱦
ÄÏϹºÎï
´´ÒµÉ³Áú
Ö°³¡·çÔÆ
Ô˶¯ÓëÐÝÏÐ
ÓðëÇò³¡
¸ß¶û·ò¾ãÀÖ²¿
ÐÝÏÐÔ˶¯
¼ÓÎ÷»¬Ñ©¾ãÀÖ²¿
¼ÓÎ÷ÓÎÓ¾¾ãÀÖ²¿
ÎäÊõÖ®¼Ò
¹ú¼Ǫ̀ÇòÊÂÎñ°ì¹«ÊÒ
¼ÓÎ÷ΧÆå¾ãÀÖ²¿
ÍøÇòÌìµØ
³ÕÃÔ×ãÇò
ƹÅÒÇò³¡
ÄêÇàÈËÌìµØ
ÖÜÄ©ÓéÀÖ
ÃÀ¾Æ¿§·È
²Ê×±
Î÷²ÍÃÀʳ
θ绪¸ß¶û·ò
webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver