371. Sum of Two Integers (Easy)

https://leetcode.com/problems/sum-of-two-integers/

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example 1:

Input: a = 1, b = 2
Output: 3

Example 2:

Input: a = -2, b = 3
Output: 1

Hints

Solutions

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> s1 = new HashSet<>();
        Set<Integer> s2 = new HashSet<>();
        for(int i = 0;i < nums1.length;++i) s1.add(nums1[i]);
        for(int i = 0;i < nums2.length;++i) s2.add(nums2[i]);
        Set<Integer> inter = new HashSet<>();
        for(Integer num: s1) {
            if(s2.contains(num)) inter.add(num);
        }
        int[] res = new int[inter.size()];
        int index = 0;
        for(Integer num: inter) {
            res[index++] = num;
        }
        return res;
    }
}
Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2019-04-15 19:26:13

results matching ""

    No results matching ""