阅读上一个主题 :: 阅读下一个主题
作者
正文
webdriver (只看此人 )
时间: 2014-9-23 12: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 !
第一部分 (1/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-25 09:57修改,总共修改了4次
楼主 |
电梯直达
webdriver (只看此人 )
时间: 2014-9-23 12:19
If you can finish most of these questions you're ready for a new level ...
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
沙发 |
返回顶端
webdriver (只看此人 )
板凳 |
返回顶端
webdriver (只看此人 )
时间: 2014-9-23 12:23
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)
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
地板 |
返回顶端
webdriver (只看此人 )
时间: 2014-9-23 12:25
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;
}
};
_________________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-23 12:27
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).
_________________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-23 12:29
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;
}
};
_________________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-23 12:29
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)
_________________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-23 12:30
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;
}
};
_________________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-23 12:31
Problem Name: Add Binary
Description:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
_________________There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
10 楼 |
返回顶端
您不能 在本论坛发表新主题 您不能 在本论坛回复主题 您不能 在本论坛编辑自己的文章 您不能 在本论坛删除自己的文章 您不能 在本论坛发表投票 您不能 在这个论坛添加附件 您可以 在这个论坛下载文件
论坛转跳:
选择一个论坛
焦点板块
加西知乎
发布求助
买房卖房
美丽的菲沙河谷
地产投资
加西菜园
商海微澜
租房那些事儿
加国一家亲
咱们本拿比
温哥华不眠夜
日月当空
休闲Langley
菩提树下
神爱世人
素里新家园
白石华人
歌声响起
读书沙龙
单身情缘
新时代之光
欢迎来新西敏
我爱列治文
高贵林的朋友们
中英双语班在Walton Elementary
生活互助
法律探索
医疗健康
准备养老
佛道净土.中医养生
天下收藏
减肥
投资理财
亲子教育
猪宝宝俱乐部
汽车天地
温村Jeep之家
钱币交流小站 (Coin Community)
免费个人广告
会员之家
北美盾之家
流协
吃喝与玩乐
玩玩乐乐
加西群英游艇会
结伴旅游
跳舞俱乐部
北美Hiking
吃吃喝喝
社团之窗
南粤茶居
交通大学校友会
福建同乡在此聚集
加拿大四川同乡联谊会
同济大学温哥华校友会
精彩贴图
精彩连环画
开心一笑
国家与城市
新闻时评
军事天地
教师罢工
大温市选
情系中国
绵绵乡音
香港占中
岭南人家
悠悠华夏
只刚上海话
西雅图夜未央
爱好与沙龙
原创原地
摄影交流
白石镇摄影交流
视听世界
疯人院 - 你的喜鹊巢
电子电玩
Twitter 交流
苹果乐园
android的星空
无限手机计划
部落冲突
大数据论坛
网站建设
颠覆小组—LINUX
IT人生
iTalkBB讨论组
English Corner
生活与教育
美好家居
精致生活
移民资讯
温哥华留学
宠物乐园
致富与创业
精明消费
炫彩珠宝
南下购物
创业沙龙
职场风云
运动与休闲
羽毛球场
高尔夫俱乐部
休闲运动
加西滑雪俱乐部
加西游泳俱乐部
武术之家
国家台球事务办公室
加西围棋俱乐部
网球天地
痴迷足球
乒乓球场
年青人天地
周末娱乐
美酒咖啡
彩妆
西餐美食
温哥华高尔夫
webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver