当前位置:网站首页>Leetcode daily question - fair candy bar exchange
Leetcode daily question - fair candy bar exchange
2022-06-12 15:00:00 【George Sumu】
subject :
Alice and Bob have candy bars of different sizes :A[i] It's Alice's first i The size of a candy bar ,B[j] It's Bob's first j The size of a candy bar .
Because they are friends , So they want to exchange a candy bar , After this exchange , They all have the same amount of candy .( The total amount of candy a person has is the sum of the size of the candy bars they have .)
Returns an array of integers ans, among ans[0] It's the size of the candy bar Alice has to exchange ,ans[1] yes Bob The size of the candy bars that have to be exchanged .
If there are multiple answers , You can go back to any one of them . Make sure the answer exists .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/fair-candy-swap
Their thinking :
- 1. Calculate the difference between the sum of two original arrays X, The difference between the two numbers exchanged needs to be X/2 To meet the conditions .
class Solution {
public int[] fairCandySwap(int[] A, int[] B) {
int[] ans = new int[2];
int sumA = 0, sumB = 0;
for(int i=0;i<A.length;i++){
sumA += A[i];
}
for(int i=0;i<B.length;i++){
sumB += B[i];
}
int cha = sumA-sumB;
for(int i=0;i<A.length;i++){
int numA = A[i];
int numB = numA - cha/2;
for(int j =0;j<B.length;j++)
{
if(numB == B[j]){
ans[0] = numA;
ans[1] = numB;
return ans;
}
}
}
return ans;
}
}
边栏推荐
- [system. Currenttimemillis()] current timestamp: the number of milliseconds that have elapsed since the current system time was 0:00:00 on January 1, 1970
- Open Chinese path file in C language
- 产业端:618的新战场
- ROS初学者编写小乌龟以一定速度旋转一定角度的server
- 安装PS软件时提示程序无法访问关键文件/目录,错误代码:41的解决方法
- 掌门教育被强制退市:上市仅一年时间 软银CMC损失惨重
- 关于互联网大厂裁员
- FIRST集与FOLLOW集白话版
- Seaborn的简述
- Industrial end: a new battlefield of 618
猜你喜欢
随机推荐
亿纬锂能拟募资90亿:刘金成骆锦红夫妇合计认购60亿 布局光谷
Chapter I exercises of program construction and interpretation
Structure example
【SimpleDateFormat】1. Conversion of date type and text type 2 Thread unsafe
Error 1105: message:\“raft entry is too large
Function related matters
三维重建系统 | L3增量运动恢复结构(增量SFM)
【LocalDate LocalTime LocalDateTime】1. Using immutability to achieve thread safety 2 Current date, current time, current date time 3 Since the time zone is not considered, you need to add 8 hours to th
JUnit test suite method sorting (method 2 is not easy to use)
h3c GR5200路由器上如何设置公网ip可以访问
阿裏、騰訊、拼多多垂範,產業互聯網的新邏輯漸顯
[wechat applet] 6.1 applet configuration file
Energy chain smart electronics landed on NASDAQ: Bain is the shareholder to become the first share of charging services in China
Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
粒子滤波学习记录
启明智显分享| 2.8寸手持中控屏应用方案
MAT的安装和使用
Yiwei lithium energy plans to raise 9billion yuan: liujincheng and luojinhong jointly subscribe for 6billion yuan of layout Optical Valley
Function recursion example
Module VIII