zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    python基础4
    python的基础数据类型和编码
    python的if语句和while循环
    java特殊运算符
    深入理解java集合
    python常用模块
    python函数的参数问题
    集合关系之间的运算
    集合
    可变类型与不可变类型
  • 原文地址:https://www.cnblogs.com/stono/p/9501189.html
Copyright © 2011-2022 走看看