716. 求和查找

 

716. 求和查找

 
给出两个整型数组  inputs i n p u t s    tests t e s t s ,只要你能从  inputs i n p u t s  中挑出一对数字,令它们的和能够在  tests t e s t s  被找到,就返回真;如果一对都找不到,则返回假。

样例

输入:
[-3,5,3]
[-1,0,1,2]
输出:
true
输入:
[3,4,5]
[1,3,6]
输出:
false

说明

 

第一组样例中, (-3) + 3 = 0, (-3) + 5 = 2 ( 3 ) + 3 = 0 , ( 3 ) + 5 = 2 0 0  和  2 2  都出现在  tests t e s t s  中,返回 true。
第二组样例中,不能找到一对的和等于  tests t e s t s  中的任何数,返回 false。

注意事项

 

两个数组的长度分别为  n, m n , m 1 \le n, m \le 200 1 n , m 2 0 0
数组中的值满足 
-1000 \le inputs_i, tests_i \le 1000
1 0 0 0 i n p u t
s
i
, t e s t
s
i
1 0 0 0
 
 
public class Solution {
    /**
     * @param inputs: an integer array
     * @param tests: an integer array
     * @return: return true if sum of two values in inputs are in tests.
     */
    public boolean addAndSearch(int[] inputs, int[] tests) {
        HashSet<Integer> hashSet=new HashSet<>();
        for (int test : tests) {
            hashSet.add(test);
        }
 
 
 
 
        for (int i = 0; i < inputs.length; i++) {
            for (int j = i+1; j < inputs.length; j++) {
                int temp=inputs[i]+inputs[j];
                if (hashSet.contains(temp)){
                  return true;
                }
            }
        }
        return false;
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代我西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值