由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode上的3sum
相关主题
3sum on LeetCode OJLC的3sum谁有简洁代码?
请问如何去除结果里面的重复以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)
sleetcode中的online judge都报runtime error, 用本地编译器执行一些例子都okCS: print all combination from an array
leetcode的3sum的运行时间问题如何避免permutation中的重复计数
问一个3 sum的问题一FG家常见题
请教下3sum为撒超时a problem from leetcode: high efficiency algorithm for combinations problem
又有leetcode题目来请教了leetcode出了新题word ladder
问道leetcode的题:Combination Sum IIcombinations 有没有 iterative的方法阿 ?
相关话题的讨论汇总
话题: three话题: arraylist话题: int话题: integer话题: num
进入JobHunting版参与讨论
1 (共1页)
t**i
发帖数: 314
1
编译过了,运行出错,帮忙看看哪儿出了问题,多谢。
import java.util.*;
public class Solution {
public ArrayList> threeSum(int[] num) {
assert(num.length >= 3);
ArrayList> triplet = new
ArrayList>();
for(int i = 0; i < num.length - 2; i++) {
for(int j = i + 2; j < num.length; j++) {
for(int k = i + 1; k < j; k++) {
if((num[i] + num[j] + num[k]) == 0) {
int[] three = threeInOrder(num[i], num[j],
num[k]);
ArrayList newTriplet = new
ArrayList();
for(int m = 0; m < three.length; m++)
newTriplet.add(three[m]);
if(!triplet.contains(newTriplet))
triplet.add(newTriplet);
}
}
}
}
return triplet;
}

private int[] threeInOrder(int a, int b, int c) {
int[] three = new int[3];
three[0] = a;
three[1] = b;
three[2] = c;
if(three[0] > three[1]) swap(three, 0, 1);
if(three[1] > three[2]) swap(three, 1, 2);
if(three[0] > three[1]) swap(three, 0, 1);
return three;
}

private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
h****e
发帖数: 928
2
你可以用这个trick:
先在程序中直接返回一个空的结果:
ArrayList> result;
result = new ArrayList>();
return result;
Online Judge会告诉你程序的输入和期望的输出是什么,你把它拷贝
下来,自己写个script一个个试过去就知道哪里出错了。
t**i
发帖数: 314
3
哇,多谢这个trick。
在eclipse里试了试没有问题,不知道为什么网站上测试时报错

【在 h****e 的大作中提到】
: 你可以用这个trick:
: 先在程序中直接返回一个空的结果:
: ArrayList> result;
: result = new ArrayList>();
: return result;
: Online Judge会告诉你程序的输入和期望的输出是什么,你把它拷贝
: 下来,自己写个script一个个试过去就知道哪里出错了。

g*********e
发帖数: 14401
4
估计你的runtime太长了,leetcode表示无法接受。
i**********e
发帖数: 1145
5
把你那个 assert 那行 comment 掉,因为输入的数组长度有可能小于三。

【在 t**i 的大作中提到】
: 编译过了,运行出错,帮忙看看哪儿出了问题,多谢。
: import java.util.*;
: public class Solution {
: public ArrayList> threeSum(int[] num) {
: assert(num.length >= 3);
: ArrayList> triplet = new
: ArrayList>();
: for(int i = 0; i < num.length - 2; i++) {
: for(int j = i + 2; j < num.length; j++) {
: for(int k = i + 1; k < j; k++) {

i**********e
发帖数: 1145
6
不是, runtime 太长会报 time limit exceeded (TLE).
runtime error 通常是 java 抛出异常了。这有可能是用户的 assertion fails,或者
数组越位等等问题。

【在 g*********e 的大作中提到】
: 估计你的runtime太长了,leetcode表示无法接受。
t**i
发帖数: 314
7
通过了,行家的眼光就是赞啊。
对了,这些题哪儿有答案不?写完以后好对照参考一下啊。

【在 i**********e 的大作中提到】
: 把你那个 assert 那行 comment 掉,因为输入的数组长度有可能小于三。
h****e
发帖数: 928
r*****e
发帖数: 792
9
really nice. thanks for sharing

【在 h****e 的大作中提到】
: 部分答案:
: http://dl.dropbox.com/u/19732851/LeetCode/ReadMe.html

t**i
发帖数: 314
10
Thanks a lot!

【在 h****e 的大作中提到】
: 部分答案:
: http://dl.dropbox.com/u/19732851/LeetCode/ReadMe.html

1 (共1页)
进入JobHunting版参与讨论
相关主题
combinations 有没有 iterative的方法阿 ?问一个3 sum的问题
问个递归的问题请教下3sum为撒超时
leetcode 上 wordladderII 求教又有leetcode题目来请教了
Word ladder II 感觉算法已经是最优了,但是过不了大测试,能不能帮忙看看?问道leetcode的题:Combination Sum II
3sum on LeetCode OJLC的3sum谁有简洁代码?
请问如何去除结果里面的重复以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)
sleetcode中的online judge都报runtime error, 用本地编译器执行一些例子都okCS: print all combination from an array
leetcode的3sum的运行时间问题如何避免permutation中的重复计数
相关话题的讨论汇总
话题: three话题: arraylist话题: int话题: integer话题: num