leecode更新
This commit is contained in:
parent
7e0a828387
commit
5e970a95f0
|
|
@ -7,6 +7,18 @@
|
||||||
<groupId>org.example</groupId>
|
<groupId>org.example</groupId>
|
||||||
<artifactId>Leecode</artifactId>
|
<artifactId>Leecode</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.projectlombok</groupId>-->
|
<!-- <groupId>org.projectlombok</groupId>-->
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,92 @@
|
||||||
|
package com.markilue.leecode.hashtable;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: Leecode
|
||||||
|
* @BelongsPackage: com.markilue.leecode.hashtable
|
||||||
|
* @Author: dingjiawen
|
||||||
|
* @CreateTime: 2022-09-07 11:47
|
||||||
|
* @Description:
|
||||||
|
* TODO 力扣349题:求两个数组的交集
|
||||||
|
* 给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Intersection {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
|
||||||
|
int[] nums1 = {1, 2, 2, 1};
|
||||||
|
int[] nums2 = {2, 2};
|
||||||
|
System.out.println(Arrays.toString(intersection(nums1, nums2)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 维护一个HashSet存储一个nums数组的数据,遍历另一个数组,如果有就保留
|
||||||
|
* 速度击败99.13%,内存击败83.58%
|
||||||
|
* @param nums1
|
||||||
|
* @param nums2
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int[] intersection(int[] nums1, int[] nums2) {
|
||||||
|
|
||||||
|
|
||||||
|
HashSet<Integer> integers = new HashSet<>();
|
||||||
|
|
||||||
|
for (int i : nums1) {
|
||||||
|
integers.add(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] result=new int[integers.size()];
|
||||||
|
int count=0;
|
||||||
|
for (int i : nums2) {
|
||||||
|
if(integers.contains(i)){
|
||||||
|
integers.remove(i);
|
||||||
|
result[count++]=i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] result1=new int[count];
|
||||||
|
|
||||||
|
for (int i = 0; i < result1.length; i++) {
|
||||||
|
result1[i]=result[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代码随想录方法.使用两个HashSet
|
||||||
|
* 速度击败26%,内存击败68.81%
|
||||||
|
* @param nums1
|
||||||
|
* @param nums2
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int[] intersection1(int[] nums1, int[] nums2) {
|
||||||
|
if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
|
||||||
|
return new int[0];
|
||||||
|
}
|
||||||
|
Set<Integer> set1 = new HashSet<Integer>();
|
||||||
|
Set<Integer> resSet = new HashSet<Integer>();
|
||||||
|
//遍历数组1
|
||||||
|
for (int i : nums1) {
|
||||||
|
set1.add(i);
|
||||||
|
}
|
||||||
|
//遍历数组2的过程中判断哈希表中是否存在该元素
|
||||||
|
for (int i : nums2) {
|
||||||
|
if (set1.contains(i)) {
|
||||||
|
resSet.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//将结果几何转为数组
|
||||||
|
return resSet.stream().mapToInt(x -> x).toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,123 @@
|
||||||
|
package com.markilue.leecode.hashtable;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: Leecode
|
||||||
|
* @BelongsPackage: com.markilue.leecode.hashtable
|
||||||
|
* @Author: dingjiawen
|
||||||
|
* @CreateTime: 2022-09-07 12:45
|
||||||
|
* @Description:
|
||||||
|
* TODO leecode第1题:两数之和:
|
||||||
|
* 给定一个整数数组 nums和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那两个整数,并返回它们的数组下标。
|
||||||
|
* 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
|
||||||
|
* 你可以按任意顺序返回答案。
|
||||||
|
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class TwoSum {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
|
||||||
|
int[] nums = {2, 7, 11, 15};
|
||||||
|
int target = 9;
|
||||||
|
System.out.println(Arrays.toString(twoSum(nums, target)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1(){
|
||||||
|
|
||||||
|
int[] nums = {3,2,4};
|
||||||
|
int target = 6;
|
||||||
|
System.out.println(Arrays.toString(twoSum(nums, target)));
|
||||||
|
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test2(){
|
||||||
|
|
||||||
|
int[] nums = {3,3};
|
||||||
|
int target = 6;
|
||||||
|
System.out.println(Arrays.toString(twoSum(nums, target)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test3(){
|
||||||
|
|
||||||
|
int[] nums = {3,2,3};
|
||||||
|
int target = 6;
|
||||||
|
System.out.println(Arrays.toString(twoSum(nums, target)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自己的思路:维护一个hashMap<数组位置,需要的值>
|
||||||
|
* 速度击败53.1%,内存击败7.31%
|
||||||
|
* @param nums
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int[] twoSum(int[] nums, int target) {
|
||||||
|
|
||||||
|
HashMap<Integer, Integer> map = new HashMap<>();
|
||||||
|
int[] result=new int[2];
|
||||||
|
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
map.put(target-nums[i],i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int last=0;
|
||||||
|
int index=0;
|
||||||
|
boolean flag=false;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
if(flag){
|
||||||
|
if(nums[i]==last){
|
||||||
|
result[0]=i;
|
||||||
|
result[1]=index;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (map.containsKey(nums[i])) {
|
||||||
|
//如果进了这里还没有返回,那么可能是两数相等的情况
|
||||||
|
flag=true;
|
||||||
|
last=nums[i];
|
||||||
|
index=i;
|
||||||
|
if(map.get(nums[i])!=map.get(target-nums[i])){
|
||||||
|
result[0]=map.get(nums[i]);
|
||||||
|
result[1]=map.get(target-nums[i]);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 官方的思路:维护一个hashMap<数组位置,需要的值>,代码更加简洁,只使用一次循环,避免了很多的重复判断
|
||||||
|
* 速度击败73.35%,内存击败54.57%
|
||||||
|
* @param nums
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int[] twoSum1(int[] nums, int target) {
|
||||||
|
|
||||||
|
HashMap<Integer, Integer> map = new HashMap<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
if (map.containsKey(target-nums[i])) {
|
||||||
|
return new int[]{i,map.get(target-nums[i])};
|
||||||
|
}
|
||||||
|
map.put(nums[i],i);
|
||||||
|
}
|
||||||
|
return new int[0];
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue