在随机数列表中查找三角形

时间:2015-03-15 04:08:46

标签: java arrays triangle-count

我把每个元素都当作“总和”,“第一个”和“秒”。如果(第一个+秒< sum)我将制作这些3的散列集(tmp)并将此散列集放入包含所有tmp散列集的更大的散列集(三角形)中。这将删除3个数字的重复组合。这是我的代码。它有效,但还有更好的解决方案吗?

public void findTriangle(int[] a){

    HashSet<HashSet<Integer>> triangle = new HashSet<HashSet<Integer>>();
    HashSet<Integer> tmp;
    for(int i=0;i<a.length;i++){
        int sum=a[i];
        for(int j=0;j<a.length;j++){
            int first = a[j];
            if(first!=sum){
                for(int k=0;k<a.length;k++){
                    int sec = a[k];
                    if(sec!=first && sec!=sum && (first + sec < sum)){
                        tmp = new HashSet<Integer>();
                        tmp.add(first);
                        tmp.add(sec);
                        tmp.add(sum);
                        triangle.add(tmp);
                    }
                }       
            }
        }

    }

    for(HashSet<Integer> hs : triangle)
        System.out.println(hs);
}

2 个答案:

答案 0 :(得分:0)

对数组进行排序并将三元组添加到列表中 -

public static ArrayList<ArrayList<Integer>> get(int[] input) {
    ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
    if (input.length < 3) {
      return result;
    }
    Arrays.sort(input);
    for (int i = 0; i < input.length - 2; i++) {
      int k = i + 2;
      for (int j = i + 1; j < input.length; j++) {
        while (k < input.length && input[i] + input[j] > input[k]) {
          ArrayList<Integer> inner = new ArrayList<Integer>();
          inner.add(input[i]);
          inner.add(input[j]);
          inner.add(input[k]);
          result.add(inner);
          k++;
        }
      }
    }
    return result;
  }

答案 1 :(得分:0)

尚未达到最佳效果。我试过测试上面两个解决方案,但它们似乎不起作用。可能是我错过了什么。因此决定在这里发布我测试的解决方案。它不会检查重复项。 查找三角形的条件:http://www.wikihow.com/Determine-if-Three-Side-Lengths-Are-a-Triangle

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Arrays;

class Triangle {
    int a;
    int b;
    int c;

    public Triangle(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public String toString() {
        return this.a + " " + this.b + " " + this.c;
    }
}

public class FindTriangle {

    public List<Triangle> findTriangle(List<Integer> points) {
        List<Triangle> result = new ArrayList<Triangle>();
        System.out.println("Entered");

        for (int i = 0; i < points.size(); i++) {
            int pt0 = points.get(i);
            System.out.println("Entered i:" + i);

            for (int j = i + 1; j < points.size() - 2; j++) {
                int pt1 = points.get(j);
                int pt2 = points.get(j + 1);
                Boolean isTri = isTriangle(pt0, pt1, pt2);
                if (isTri.equals(Boolean.TRUE)) {
                    Triangle t = new Triangle(pt0, pt1, pt2);
                    result.add(t);
                }
            }
            for (int j = 0; j < (i - 1) && j > 0; j++) {
                int pt1 = points.get(j);
                int pt2 = points.get(j + 1);
                Boolean isTri = isTriangle(pt0, pt1, pt2);
                if (isTri.equals(Boolean.TRUE)) {
                    Triangle t = new Triangle(pt0, pt1, pt2);
                    result.add(t);
                }
            }

            // final
            int pt1, pt2;
            if (i == 0) {
                pt1 = points.get(i + 1);
                pt2 = points.get(points.size() - 1);
            } else if (i == points.size() - 1) {
                pt1 = points.get(0);
                pt2 = points.get(i - 1);
            } else {
                pt1 = points.get(i + 1);
                pt2 = points.get(i - 1);
            }

            Boolean isTri = isTriangle(pt0, pt1, pt2);
            if (isTri.equals(Boolean.TRUE)) {
                Triangle t = new Triangle(pt0, pt1, pt2);
                result.add(t);
            }
        }
        return result;
    }

    public Boolean isTriangle(Integer pt1, Integer pt2, Integer pt3) {
        System.out.println("Pt1, Pt2, Pt3: " + pt1 + ":" + pt2 + ":" + pt3);

        if ((pt1 + pt2) > pt3 && (pt1 + pt3) > pt2 && (pt2 + pt3) > pt1) {
            System.out.println("This is triangle");
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public ArrayList<ArrayList<Integer>> getTri(int[] input) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if (input.length < 3) {
            return result;
        }
        Arrays.sort(input);
        for (int i = 0; i < input.length - 2; i++) {
            int k = i + 2;
            for (int j = i + 1; j < input.length; j++) {
                while (k < input.length && input[i] + input[j] > input[k]) {
                    ArrayList<Integer> inner = new ArrayList<Integer>();
                    inner.add(input[i]);
                    inner.add(input[j]);
                    inner.add(input[k]);
                    result.add(inner);
                    k++;
                }
            }
        }
        return result;
    }

    public void findTriangleW(int[] a) {

        HashSet<HashSet<Integer>> triangle = new HashSet<HashSet<Integer>>();
        HashSet<Integer> tmp;
        for (int i = 0; i < a.length; i++) {
            int sum = a[i];
            for (int j = 0; j < a.length; j++) {
                int first = a[j];
                if (first != sum) {
                    for (int k = 0; k < a.length; k++) {
                        int sec = a[k];
                        if (sec != first && sec != sum && (first + sec < sum)) {
                            tmp = new HashSet<Integer>();
                            tmp.add(first);
                            tmp.add(sec);
                            tmp.add(sum);
                            triangle.add(tmp);
                        }
                    }
                }
            }

        }

        for (HashSet<Integer> hs : triangle)
            System.out.println(hs);
    }

    public static void main(String[] args) {
        FindTriangle f = new FindTriangle();
        List<Integer> points = new ArrayList<Integer>();
        points.add(1);
        points.add(5);
        points.add(10);
        points.add(7);

        System.out.println("Printing final results");
        List<Triangle> result = f.findTriangle(points);
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i).toString());
        }
    }
}