由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一题
相关主题
说说4sum的复杂度吧3 sum 哪个题目 怎么 去掉 duplicate 呢?
讨论,careercup150 的1.3LC的题确实变难了。。。
一道面试题2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么
M家问题Amazon电面问题求大牛解答
longest subarray with numbers arranged as a seq一道C/C++的面试题
面经请教一个C++的题目,谢谢
求教 合并两数组 并排除重复请教几个电面题
detect number of duplicates in bst问一道关于字符串的面试题
相关话题的讨论汇总
话题: input话题: sum话题: sort话题: array话题: true
进入JobHunting版参与讨论
1 (共1页)
e***s
发帖数: 799
1
之前大牛电面题,不知道有没有比O(n^2)快的答案?
Given a integer array, test if there is any consequence subarray
which sum of elements is 0.
[7, 1, 1, -2, 3, 4] ==> true [1, 1, -2]
s****g
发帖数: 43
2
Input S[N]. Make another array T, let T[i]=the sum of S[0] to S[i]: O(N).
Then sort T: O(NlgN). Walk through T again, if there are duplicates, then
there must exist a sum-0 sequence.
c*****e
发帖数: 59
3
同问,偶只会 O(n^2)的解法
c*******2
发帖数: 60
4
Beautiful !

Input S[N]. Make another array T, let T[i]=the sum of S[0] to S[i]: O(N).
Then sort T: O........

【在 s****g 的大作中提到】
: Input S[N]. Make another array T, let T[i]=the sum of S[0] to S[i]: O(N).
: Then sort T: O(NlgN). Walk through T again, if there are duplicates, then
: there must exist a sum-0 sequence.

c*****e
发帖数: 59
5
Input [7, 1, 1, -2, 3, 4]
sort [-2, 1, 1, 3, 4, 7]
T [-2, -1, 0, 3, 7, 14] ?
but no duplicate ?
d****n
发帖数: 397
6
divide conqur?和mergesort一样nlogn?
有0就输出true,如果数组不含0,
首先讲数组分为两半
判断两半是不是有true,有的话,return true,否则从中间开始,
i=floor[n/2],j=ceiling[n/2],如果i--,j++,直到i=0,j=n-1还没有sum是0的情况,
return false.
这样
T(n)=2T(n/2)+theta(n)=theta(nlogn);

【在 e***s 的大作中提到】
: 之前大牛电面题,不知道有没有比O(n^2)快的答案?
: Given a integer array, test if there is any consequence subarray
: which sum of elements is 0.
: [7, 1, 1, -2, 3, 4] ==> true [1, 1, -2]

y********9
发帖数: 130
7

你得排序和

【在 c*****e 的大作中提到】
: Input [7, 1, 1, -2, 3, 4]
: sort [-2, 1, 1, 3, 4, 7]
: T [-2, -1, 0, 3, 7, 14] ?
: but no duplicate ?

s*w
发帖数: 729
8
use hash table to check duplicates, so it is O(N)
someone posted the code in the past few days

【在 s****g 的大作中提到】
: Input S[N]. Make another array T, let T[i]=the sum of S[0] to S[i]: O(N).
: Then sort T: O(NlgN). Walk through T again, if there are duplicates, then
: there must exist a sum-0 sequence.

c*****e
发帖数: 59
9
OK,明白了,
除了验证duplicate,还需要验证 T 第三位是不是零这个特殊情况

【在 c*****e 的大作中提到】
: Input [7, 1, 1, -2, 3, 4]
: sort [-2, 1, 1, 3, 4, 7]
: T [-2, -1, 0, 3, 7, 14] ?
: but no duplicate ?

e***s
发帖数: 799
10
牛!我必须声明,我在回复大牛之前自己想到了。
A = [7,1,1,-2,3,4]
S = [7,8,9,7,10,14]
S[0] == S[3]
所以return true, [A[1], A[2], A[3]]

【在 s****g 的大作中提到】
: Input S[N]. Make another array T, let T[i]=the sum of S[0] to S[i]: O(N).
: Then sort T: O(NlgN). Walk through T again, if there are duplicates, then
: there must exist a sum-0 sequence.

p******s
发帖数: 40
11
应该不对,i--,j++如何协调?海猪的解法简单明了

【在 d****n 的大作中提到】
: divide conqur?和mergesort一样nlogn?
: 有0就输出true,如果数组不含0,
: 首先讲数组分为两半
: 判断两半是不是有true,有的话,return true,否则从中间开始,
: i=floor[n/2],j=ceiling[n/2],如果i--,j++,直到i=0,j=n-1还没有sum是0的情况,
: return false.
: 这样
: T(n)=2T(n/2)+theta(n)=theta(nlogn);

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一道关于字符串的面试题longest subarray with numbers arranged as a seq
问一道面世题面经
问个题: 找read-only array中duplicate的数求教 合并两数组 并排除重复
问个构造函数的问题detect number of duplicates in bst
说说4sum的复杂度吧3 sum 哪个题目 怎么 去掉 duplicate 呢?
讨论,careercup150 的1.3LC的题确实变难了。。。
一道面试题2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么
M家问题Amazon电面问题求大牛解答
相关话题的讨论汇总
话题: input话题: sum话题: sort话题: array话题: true