📣 独立日限时特惠: VIP通行证立减$68
楼主: qshen1989
跳转到指定楼层
上一主题 下一主题
收起左侧

LinkedIn一面攒人品

🔗
北美农民 2014-8-1 22:31:51 | 只看该作者
全局:
readman 发表于 2014-8-1 08:37
https://gist.github.com/gaoyike/40df1e0ec5a6c95e0014

做出来了

great, but seems do not meet the follow-up at 18th floor.
回复

使用道具 举报

🔗
readman 2014-8-1 23:13:10 | 只看该作者
全局:
北美农民 发表于 2014-8-1 22:31
great, but seems do not meet the follow-up at 18th floor.

什么是不相同?
{1,2,3}
[[1, 1, 1], [1, 2, 2], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]
这个都不相同吧..
你的意思是不能同一个数取值?
{1,2,3} = [[1,2,3]] ?

补充内容 (2014-8-1 23:15):
是..{1,2,3} = null..

补充内容 (2014-8-1 23:19):
在加入前, 添加条件 if (l != i && j != i && j != l)
{5, 2, 4, 8, 0} =>[[2, 4, 5], [4, 5, 8]]

点评

e.g. for input {2, 2, 3, 3, 4, 4}, the solution {2, 3, 4} should appear only once  发表于 2014-8-1 23:31
回复

使用道具 举报

🔗
readman 2014-8-1 23:53:37 | 只看该作者
全局:
北美农民 发表于 2014-8-1 22:31
great, but seems do not meet the follow-up at 18th floor.

   public HashSet<ArrayList<Integer>> valid2(int[] A) {
        HashSet<ArrayList<Integer>> result = new HashSet<ArrayList<Integer>>();
        Arrays.sort(A);
        for (int i = 0; i < A.length; ++i) {
            int k = i;
            for (int j = i; j < A.length; ++j) {
                while (k < A.length && A[i] + A[j] > A[k])
                    ++k;
                for (int l = j; l < k; l++) {
                        if (A[i] != A[j] && A[j] != A[l] && A[i] != A[l]) {
                            ArrayList<Integer> tmp = new ArrayList<Integer>();
                            tmp.add(A[i]);
                            tmp.add(A[j]);
                            tmp.add(A[l]);
                            result.add(tmp);
                        }
                }
            }
        }
        return result;
    }


Hashset - =
回复

使用道具 举报

🔗
北美农民 2014-8-2 00:09:06 | 只看该作者
全局:
readman 发表于 2014-8-1 10:53
public HashSet valid2(int[] A) {
        HashSet result = new HashSet();
        Arrays.sort( ...

Sry cannot type CN in company.

Still not correct. An extreme example is, {{2, 2, 2}} will be returned if the input is {2. 2. 2. ...., 2}.
All solutions should be unique.
回复

使用道具 举报

🔗
readman 2014-8-2 00:15:18 | 只看该作者
全局:
北美农民 发表于 2014-8-2 00:09
Sry cannot type CN in company.

Still not correct. An extreme example is, {{2, 2, 2}} will be re ...

如果
{2. 2. 2. ...., 2} => {{2, 2, 2}}
那么不就是说, 3个数字, 每一个都可以pick任何不重复的Index上的数字, 但是无所谓结果是否是相同字符.

那么
{2, 2, 3, 3, 4, 4} =>  {2, 3, 4} 为什么只有 一个? {2,3,3} 为什么没有...



回复

使用道具 举报

🔗
北美农民 2014-8-2 01:04:11 | 只看该作者
全局:
本帖最后由 北美农民 于 2014-8-1 12:41 编辑
readman 发表于 2014-8-1 11:15
如果
{2. 2. 2. ...., 2} => {{2, 2, 2}}
那么不就是说, 3个数字, 每一个都可以pick任何不重复的Index ...

I just listed {2, 3, 4} because of simplicity,

if the input is {2, 2, 3, 3, 4, 4}, the result should be {{2,2,2}, {2,3,3}, {2, 3, 4}, {3,3,3},{3,3,4}, {3,4,4}, {4,4,4}}. Each solution has to be unique.
回复

使用道具 举报

🔗
 楼主| qshen1989 2014-8-2 01:34:45 | 只看该作者
全局:
北美农民 发表于 2014-8-1 21:25
为啥c始终是b后面的一个? 必须找到c' 使得 a + b

嗯,你说得对,面的时候没让考虑重复的情况,所以排完序后是递增的而且只要返回一个。。。各位发散得好~这样以后碰到就轻松啦
回复

使用道具 举报

🔗
北美农民 2014-8-2 01:37:35 | 只看该作者
全局:
qshen1989 发表于 2014-8-1 12:34
嗯,你说得对,面的时候没让考虑重复的情况,所以排完序后是递增的而且只要返回一个。。。各位发散得好~ ...

no, even we have 发散ed, the time complexity does not become better than brute-force. Just some trvial constant time optimization.
回复

使用道具 举报

🔗
 楼主| qshen1989 2014-8-2 02:25:13 | 只看该作者
全局:
北美农民 发表于 2014-8-2 01:37
no, even we have 发散ed, the time complexity does not become better than brute-force. Just some tr ...

{2,3,7,8,9,10} 这样, i=0:length-3; j=i+1: length-2; k=j+1;
i=0 j=1 k=2时
2+3<7, 就不用考虑后面的8,9,10了,++j;
i=0 j=2 k=3
2 + 7 > 8 满足

补充内容 (2014-8-2 02:53):
刚出去买饭了= =要返回所有解的话的确是 O(n^3),只要第一个的话是O(n^2)吧?

点评

As I said, just constant optimizations, time complexity is still the same, O(n^3)  发表于 2014-8-2 02:36
回复

使用道具 举报

🔗
readman 2014-8-2 08:21:43 | 只看该作者
全局:
北美农民 发表于 2014-8-2 01:37
no, even we have 发散ed, the time complexity does not become better than brute-force. Just some tr ...

http://stackoverflow.com/questio ... gles-from-n-numbers
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表