从两个元素中的整数数组中找到两对

时间:2016-07-26 09:13:13

标签: java algorithm

两对:如果有两对具有相同数字的骰子,则玩家对这些骰子的总和进行评分。如果不是,则玩家得分为0.例如,放在“两对”上的1,1,2,3,3给出8。

例子: 1,1,2,3,3结果8 1,1,2,3,4结果0 1,1,2,2,2结果6

如何有效地找到这个?

我一直在使用以下代码来查找一对

int max_difference = 0;
int val1 = 0 , val2 = 0;
Arrays.sort(dice);
for (int i = 0; i < dice.length - 1; i++) {
    int x = dice[i+1] - dice[i];
    if(x <= max_difference) {
        max_difference = x;
        val1 = dice[i];
        val2 = dice[i+1];

    }
}
pairScore = val1 + val2;

5 个答案:

答案 0 :(得分:5)

不需要那么复杂,因为你只是在搜索结果数字......

int prev = 0;
int result = 0;
int pairs = 0;

Arrays.sort(dice);
for (int i = 0; i < dice.length; i++) 
{
    int current = dice[i];
    if (current == prev) 
    {
        result += current*2;
        pairs++;
        prev = 0;
    } 
    else prev = current;
}

if (pairs == 2) return result;
else return 0;

答案 1 :(得分:3)

我使用频率图,即数字是键,值是计数器(所以Map<Integer, Integer>)。但是,由于它用于骰子,您可以使用长度等于最大骰子值(标准骰子为6)的数组来简化骰子。然后检查每个数字的频率,并从中获得对的数量。

示例:

int[] diceFrequency = new int[6];

//assuming d is in the range [1,6]
for( int d : dice ) {
  //increment the counter for the dice value
  diceFrequency[d-1]++; 
}

int numberOfPairs = 0;
int pairSum = 0;

for( int i = 0; i < diceFrequency.length; i++ ) {
  //due to integer division you get only the number of pairs, 
  //i.e. if you have 3x 1 you get 1 pair, for 5x 1 you get 2 pairs
  int num = diceFrequency[i] / 2;

  //total the number of pairs is just increases
  numberOfPairs += num; 

  //the total value of those pairs is the dice value (i+1) 
  //multiplied by the number of pairs and 2 (since it's a pair)
  pairSum += (i + 1 ) * 2 * num;
}

if( numerOfPairs >= 2 ) {
  //you have at least 2 pairs, do whatever is appropriate
}

答案 2 :(得分:3)

如何使用hashmap如下?

public static void main(String[] args) {
    List<Integer> list = Lists.newArrayList(1, 1, 2, 3, 3, 4);
    Map<Integer, Integer> map = Maps.newHashMap();

    int result = 0;

    for (int i : list) {
        int frequency = (int) MapUtils.getObject(map, i, 0);

        if (frequency < 2) {
            map.put(i, ++frequency);
        }
    }

    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        if (entry.getValue() > 1) {
            result += entry.getKey() * entry.getValue();
        }
    }

    System.out.println(result);
}

答案 3 :(得分:1)

public static void main(String[] args) {
    List<Integer> list = Lists.newArrayList(1, 1, 2, 3, 3);
    Map<Integer, Integer> map = new HashMap<>();

    int count= 0;

    for (int num : list)
        if(map.containsKey(num ))
            map.put(num , map.get(num )+1);
        else
            map.put(num , 1);

    for (int num  : map.keySet()) {
        if (map.get(num ) > 1) {
            count= count+ (num  * map.get(num ));
        }
    }

    System.out.println(count);
}

答案 4 :(得分:1)

我希望,我能理解这个问题。所以我们可以使用这个代码部分。

    List<Integer> diceList = new ArrayList<Integer>();
    diceList.add(1);
    diceList.add(1);
    diceList.add(2);
    diceList.add(4);
    diceList.add(3);
    diceList.add(3);
    diceList.add(6);
    diceList.add(8);

    Integer prev = null, score = 0;

    boolean flag = false;
    for (Integer val : diceList) {
        if (prev == null || !prev.equals(val)) {
            if (flag) {
                score = score + prev;
                flag = false;
            }
            prev = val;

        } else if (prev == val) {
            score = score + prev;
            flag = true;
        }
    }
    System.out.println(score);