1. 程式人生 > >888. 公平的糖果交換

888. 公平的糖果交換

pri diff system pub stat con problem -s contest

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. 公平的糖果交換