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 !
第一部分 (1/5)
If you can finish most of these questions you're ready for a new level ...
If you want to be successfully in technique interview, try solve the problem by yourself.

Problem Name: 3Sum
Description:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a <= b <= c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
Now comes the solution (in C++) ...
Simplify '3sum' to '2sum' O(n^2).
en.wikipedia.org/wiki/3SUM
代码: |
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int>> res;
sort(num.begin(), num.end());
int N = num.size();
for (int i = 0; i < N-2 && num[i] <= 0; ++i)
{
if (i > 0 && num[i] == num[i-1])
continue; // avoid duplicates
int twosum = 0 - num[i];
int l = i + 1, r = N - 1;
while (l < r)
{
int sum = num[l] + num[r];
if (sum < twosum)
l++;
else if (sum > twosum)
r--;
else {
vector<int> triplet;
triplet.push_back(num[i]);
triplet.push_back(num[l]);
triplet.push_back(num[r]);
res.push_back(triplet);
l++; r--;
while (l < r && num[l] == num[l-1]) l++; // avoid duplicates
while (l < r && num[r] == num[r+1]) r--; // avoid duplicates
}
}
}
return res;
}
};
|
Problem Name: 3Sum Closest
Description:
Given an array S of n integers, find three integers in S such that the sum is closest to
a given number, target. Return the sum of the three integers.
You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Now comes the solution (in C++) ...
Similar to 3Sum, taking O(n^2) time complexity.
代码: |
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int res = INT_MAX;
int N = num.size();
sort(num.begin(), num.end());
for (int i = 0; i < N-2; ++i)
{
int l = i + 1, r = N - 1;
while (l < r)
{
int threesum = num[i] + num[l] + num[r];
if (threesum == target) return target;
else if (threesum < target) l++;
else r--;
if (res == INT_MAX || abs(threesum - target) < abs(res - target))
res = threesum;
}
}
return res;
}
};
|
Problem Name: 4Sum
Description:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero.
Note:
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a <= b <= c <= d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
Now comes the solution (in C++) ...
Similar to 3Sum, 2Sum.
代码: |
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
int N = num.size();
vector<vector<int> > res;
if (N < 4) return res;
sort(num.begin(), num.end());
for (int i = 0; i < N; ++i)
{
if (i > 0 && num[i] == num[i-1]) continue; // avoid duplicates
for (int j = i+1; j < N; ++j)
{
if (j > i+1 && num[j] == num[j-1]) continue; // avoid duplicates
int twosum = target - num[i] - num[j];
int l = j + 1, r = N - 1;
while (l < r)
{
int sum = num[l] + num[r];
if (sum == twosum) {
vector<int> quadruplet(4);
quadruplet[0] = num[i];
quadruplet[1] = num[j];
quadruplet[2] = num[l];
quadruplet[3] = num[r];
res.push_back(quadruplet);
while (l < r && num[l+1] == num[l]) l++; // avoid duplicates
while (l < r && num[r-1] == num[r]) r--; // avoid duplicates
l++; r--;
}
else if (sum < twosum) l++;
else r--;
}
}
}
return res;
}
};
|
Problem Name: Add Binary
Description:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".