当前位置:网站首页>349两个数组的交集和01两数之和
349两个数组的交集和01两数之和
2022-07-27 10:33:00 【懒虫虫~】
一、01两数之和
1.1 题目
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
分析:
题意就是让你从数组中找到两个位置他们对应位置的和为target。
1.2 实现
public class TwoSum {
public static void main(String[] args) {
int[] nums1 = {
4,9,5};
int[] res = twoSum(nums1, 9);
System.out.println(res[0]);
System.out.println(res[1]);
}
public static int[] twoSum(int[] nums, int target) {
//利用map结构,key存储数值,value存储下标
int[] res = new int[2];
if(nums == null || nums.length == 0){
return res;
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
}
map.put(nums[i], i);
}
return res;
}
}
二、01两数之和
2.1 题目
给定两个数组,编写一个函数来计算它们的交集。
示例1:
输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2]
示例1:
输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [9,4]
说明:
输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。
2.2 实现
/**349. 两个数组的交集**/
public class Intersection {
/** * 创建set集合,利用其具有去重的特点进行筛选 * 创建nums集合,用于存储交集不重复的元素 * 循环遍历数组2,如果set中包含有数组2的元素,则将这个元素加入到set2中 * 遍历set集合,将结果存储在数组 */
public static void main(String[] args) {
int[] nums1 = {
4,9,5};
int[] nums2 = {
9,4,9,8,4};
System.out.println(Arrays.toString(intersection2(nums1, nums2)));
}
// 方法1:Set集合
public static int[] intersection1(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>(); // HashSet特性:无序、不重复、无索引。
Set<Integer> set2 = new HashSet<>();
for(int i = 0; i < nums1.length; i++){
set1.add(nums1[i]);
}
for(int i = 0; i < nums2.length; i++){
if(set1.contains(nums2[i])){
set2.add(nums2[i]);
}
}
int[] nums = new int[set2.size()];
int k = 0;
for(Integer it : set2){
nums[k++] = it;
}
return nums;
}
// 方法2:Map集合
public static int[] intersection2(int[] nums1, int[] nums2) {
// 创建map集合
Map<Integer, Integer> map = new HashMap<>();
// 将数组1中的元素全部加入到map集合中,并将其值默认为1,相当于做了去重的操作
for(int i = 0; i < nums1.length; i++){
map.put(nums1[i], 1);
}
// 创建list集合,用于存储结果
ArrayList<Integer> list = new ArrayList<>();
// 循环遍历数组2,如果map中包含数组2的元素,且其value大于0,则将其加入到list集合中,更新其value
for(int j = 0; j < nums2.length; j++){
if(map.containsKey(nums2[j]) && map.get(nums2[j]) > 0){
list.add(nums2[j]);
map.put(nums2[j], map.get(nums2[j]) - 1);// 将其value更新,也就是对其-1,相当于去重
}
}
// 创建数组,用于存放结果,将list中的元素赋值给数组,并返回
int len = list.size();
int[] res = new int[len];
for(int i = 0; i < len; i++){
res[i] = list.remove(0);
}
return res;
}
}
边栏推荐
- The second method of calculating overlapping integral
- 图片中非0值的数量对分类的影响
- Customized modification based on jira7.9.2
- 11 wrong set
- Study notes Minio
- 8 find subsequences with a maximum length of K
- 12 is at least twice the maximum number of other numbers
- Using skills of word
- Maximized array sum after 13 K negations
- 学习笔记-minio
猜你喜欢

Kangaroo cloud stack based on CBO in spark SQL optimization

计算重叠积分的第二种方法

The difference between scalar, vector, matrix and tensor in deep learning

Local and overall differences between emergence and morphology

KEPServer配置

Derivation of the detailed expansion sto overlap integrals

SQL Server2000数据库错误

Iptables prevent nmap scanning and binlog explanation

Yiwen counts NFT projects valued at more than US $100million

Tensorflow notes - basic functions and concepts
随机推荐
DNS principle and resolution process
Learning notes - simple server implementation
MySQL index, transaction and storage engine
tf.AUTO_ Function of reuse
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
Research on synaesthesia integration and its challenges
The difference between scalar, vector, matrix and tensor in deep learning
I've compromised. Since everyone wants to call me Yelin, there's nothing I can do
树形数据转换
Influence of black and white pixel distribution on iteration times
Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
BeautifulSoup的使用
Yiwen counts NFT projects valued at more than US $100million
Using skills of word
Symmetric encryption and asymmetric encryption
Ansible
Tcp/ip protocol
11 wrong set
A verification test of the relationship between iteration number and entropy
tensorflow运行报错解决方法