码迷,mamicode.com
首页 > 其他好文 > 详细

888. 公平的糖果交换

时间:2018-08-19 15:38:35      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:and   array   etc   new   tps   string   turn   import   ati   

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

标签:and   array   etc   new   tps   string   turn   import   ati   

原文地址:https://www.cnblogs.com/stono/p/9501189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!