888. 公平的糖果交换

Posted stono

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了888. 公平的糖果交换相关的知识,希望对你有一定的参考价值。

888. 公平的糖果交换
https://leetcode-cn.com/contest/weekly-contest-98/problems/fair-candy-swap/
package com.test;

import java.util.Arrays;

//888. 公平的糖果交换
//https://leetcode-cn.com/contest/weekly-contest-98/problems/fair-candy-swap/
public class Lesson888 {

    public static void main(String[] args) {
        int[] A = {10000,10001,70000};
        int[] B = {1,2,3,4,5,50014};

        int[] ints = fairCandySwap(A, B);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] fairCandySwap(int[] A, int[] B) {
        int aLength = A.length;
        int bLength = B.length;
        int sumA = 0;
        int sumB = 0;
        for (int i = 0; i < aLength; i++) {
            sumA = sumA + A[i];
        }
        for (int i = 0; i < bLength; i++) {
            sumB = sumB + B[i];
        }
        int[] res = new int[2];
        if (sumA < sumB) {
            int diffrence = (sumB - sumA) / 2;
            for (int i = 0; i < aLength; i++) {
                for (int j = 0; j < bLength; j++) {
                    if (A[i] + diffrence - B[j] == 0) {
                        res[0] = A[i];
                        res[1] = B[j];
                        return res;
                    }
                }
            }
        }
        if (sumA > sumB) {
            int diffrence = (sumA - sumB) / 2;
            for (int j = 0; j < bLength; j++) {
                for (int i = 0; i < aLength; i++) {
                    if (B[j] + diffrence - A[i] == 0) {
                        res[0] = A[i];
                        res[1] = B[j];
                        return res;
                    }
                }
            }

        }
        return res;
    }
}

 

以上是关于888. 公平的糖果交换的主要内容,如果未能解决你的问题,请参考以下文章

888. 公平的糖果交换

leetcode-888. 公平的糖果交换

Python描述 LeetCode 888. 公平的糖果交换

Leetcode-888 公平的糖果交换

公平的糖果交换

LeetCode210513矩阵的转置和公平的糖果棒交换