例1:给定两个数组,编写一个函数来计算它们的交集
leetCode 349 --------- Set的使用
示例 1:
输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2]
示例 2:
输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [9,4]
说明:
输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。
import org.omg.PortableInterceptor.INACTIVE;
import javax.sound.midi.Soundbank;
import javax.sql.rowset.FilteredRowSet;
import javax.swing.*;
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args){
Set s = new TreeSet();
}
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> s = new HashSet<Integer>();
Set<Integer> res = new HashSet<Integer>();
for (int i=0; i<nums1.length; i++){
s.add(nums1[i]);
}
for (int i=0; i<nums2.length; i++){
if (s.contains(nums2[i])){
res.add(nums2[i]);
}
}
int[] resList = new int[res.size()];
int i = 0;
for (Integer e:res){
resList[i++] = e;
}
return resList;
}
}
例2 两个数组的交集
leetCode 350 --------- map的使用
给定两个数组,编写一个函数来计算它们的交集。
示例 1:
输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2,2]
示例 2:
输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [4,9]
import org.omg.PortableInterceptor.INACTIVE;
import javax.sound.midi.Soundbank;
import javax.sql.rowset.FilteredRowSet;
import javax.swing.*;
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args){
Set s = new TreeSet();
}
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer,Integer> numCount = new HashMap<Integer, Integer>();
Stack<Integer> resStack = new Stack();
for (int i=0; i<nums1.length; i++){
if (numCount.containsKey(nums1[i])){
numCount.put(nums1[i],numCount.get(nums1[i])+1);
}else {
numCount.put(nums1[i],1);
}
}
for (int i=0; i<nums2.length; i++){
if (numCount.containsKey(nums2[i]) && numCount.get(nums2[i])>0){
resStack.push(nums2[i]);
numCount.put(nums2[i],numCount.get(nums2[i])-1);
}
}
int[] res = new int[resStack.size()];
for (int i=0; i<res.length; i++){
res[i] = resStack.pop();
}
return res;
}
}
例3:两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
这里可以使用map实现,比如前面的例子,当遍历到7时,查找2是否存在,如果存在返回两者的索引。
但是:
可能出现两个数相同的情况,比如 target=84 数组中有两个一样的数 42和42,由于后面的42会覆盖前面的,查找失败。
解决方案
边加入边查找,比如 [23 24 42 65 42]
查找23是否存在,不存在,在map中加入
map[23] = 0
map[24] = 1
map[42] = 2
map[65] = 3
这时候加入42,84-42=42,在数组中查找42得出索引为2,即为所求 [2,4]
import org.omg.PortableInterceptor.INACTIVE;
import javax.sound.midi.Soundbank;
import javax.sql.rowset.FilteredRowSet;
import javax.swing.*;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
public class Main {
static class Count{
int count;
char c;
public Count(char c,int count){
this.count = count;
this.c = c;
}
}
public static void main(String[] args){
}
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<Integer, Integer>();
int[] res = new int[2];
for (int i=0; i<nums.length; i++){
int other = target - nums[i];
if (map.containsKey(other)){
res[0] = i;
res[1] = map.get(other);
}else {
map.put(nums[i],i);
}
}
return res;
}
}
例4:
leetcode 454 四数相加
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。
例如
输入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
输出:
2
解释:
两个元组如下:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
先遍历A B,求出A B所有可能的和,存入map ASumB中,键为和,值为和出现的次数
然后判断 -(C+D)的和是否在ASumB中,如果在,将次数累加
import org.omg.PortableInterceptor.INACTIVE;
import javax.sound.midi.Soundbank;
import javax.sql.rowset.FilteredRowSet;
import javax.swing.*;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String[] args){
}
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
HashMap<Integer,Integer> ASumB = new HashMap<Integer, Integer>();
int count = 0;
for (int i=0; i<A.length; i++){
for (int j=0; j<B.length; j++){
int sum = A[i] + B[j];
if (ASumB.containsKey(sum)){
ASumB.put(sum,ASumB.get(sum) + 1);
}else {
ASumB.put(sum,1);
}
}
}
for (int i=0; i<C.length; i++){
for (int j=0; j<C.length; j++){
int sum = C[i] + D[j];
if (ASumB.containsKey(-sum)){
count += ASumB.get(-sum);
}
}
}
return count;
}
}
例5:
给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序)。
找到所有回旋镖的数量。你可以假设 n 最大为 500,所有点的坐标在闭区间 [-10000, 10000] 中。
示例:
输入:
[[0,0],[1,0],[2,0]]
输出:
2
解释:
两个回旋镖为 [[1,0],[0,0],[2,0]] 和 [[1,0],[2,0],[0,0]]
- 对于每一个点,求出其他点到这个点的距离,使用map保存。比如距离为10的出现了3次,map[3]=2
- 如果距离n大于2,排列数为 n(n-1)
- 把所有的排列数加起来
import org.omg.PortableInterceptor.INACTIVE;
import javax.sound.midi.Soundbank;
import javax.sql.rowset.FilteredRowSet;
import javax.swing.*;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String[] args){
}
public int numberOfBoomerangs(int[][] points) {
int count = 0;
for (int i=0; i<points.length; i++){
Map<Integer,Integer> disMap = new HashMap<Integer,Integer>();
for (int j=0; j<points.length; j++){
int dis = distance(points[i],points[j]);
if (disMap.containsKey(dis)){
disMap.put(dis,disMap.get(dis)+1);
}else {
disMap.put(dis,1);
}
}
for (int key:disMap.keySet()){
if (disMap.get(key) >= 2){
count += disMap.get(key) * (disMap.get(key) - 1);
}
}
}
return count;
}
public int distance(int[] a,int[] b){
return (int)(Math.pow(a[0]-b[0],2) + Math.pow(a[1]-b[1],2));
}
}
例6: 存在重复元素 II
leetcode 219. 存在重复元素 II
查找表和滑动窗口法结合一起使用
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k。
示例 1:
输入: nums = [1,2,3,1], k = 3
输出: true
示例 2:
输入: nums = [1,0,1,1], k = 1
输出: true
示例 3:
输入: nums = [1,2,3,1,2,3], k = 2
输出: false
import org.omg.PortableInterceptor.INACTIVE;
import javax.sound.midi.Soundbank;
import javax.sql.rowset.FilteredRowSet;
import javax.swing.*;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String[] args){
Queue<Integer> queue = new LinkedList<>();
}
public boolean containsNearbyDuplicate(int[] nums, int k) {
Set<Integer> numSet = new HashSet<Integer>();
for (int i=0; i<nums.length; i++){
if (numSet.contains(nums[i])){
return true;
}else {
numSet.add(nums[i]);
}
if (numSet.size() > k){
numSet.remove(nums[i-k]);
}
}
return false;
}
}
例7:存在重复元素 III
letcode 220. 存在重复元素 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。
示例 1:
输入: nums = [1,2,3,1], k = 3, t = 0
输出: true
示例 2:
输入: nums = [1,0,1,1], k = 1, t = 2
输出: true
示例 3:
输入: nums = [1,5,9,1,5,9], k = 2, t = 3
输出: false
使用TreeMap结合滑动窗口解决(这里要用到TreeMap的有序性,只能用treeMap,不能用hashMap)
import org.omg.PortableInterceptor.INACTIVE;
import javax.sound.midi.Soundbank;
import javax.sql.rowset.FilteredRowSet;
import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String[] args){
long a = 100000000;
int b = 20;
System.out.println();
int[] arr = {1,5,9,1,5,9};
System.out.println(containsNearbyAlmostDuplicate(arr,2,3));
}
public static boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
TreeSet<Long> numSet = new TreeSet<Long>();
long lt = (long)t;
for (int i=0; i<nums.length; i++){
long cur = (long)nums[i];
if (numSet.higher(cur-lt-1) != null && numSet.higher(cur-lt-1) <= cur+lt){
return true;
}else {
numSet.add(cur);
}
//维持窗口大小(滑动窗口大小为k,第一个元素到最后一个元素的距离为k-1,如果为k+1时,移除一个元素)
if (numSet.size() > k){
numSet.remove((long)nums[i-k]);
}
}
return false;
}
}