当前位置:网站首页>349 sum of intersection of two arrays and 01
349 sum of intersection of two arrays and 01
2022-07-27 11:14:00 【Lazy bug~】
One 、01 Sum of two numbers
1.1 subject
Given an array of integers nums And a target value target, Please find and as the target value in the array Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in an array cannot be used twice .
Example :
Given nums = [2, 7, 11, 15], target = 9
because nums[0] + nums[1] = 2 + 7 = 9
So back [0, 1]
analysis :
The meaning of the question is to let you find two positions in the array, and the sum of their corresponding positions is target.
1.2 Realization
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) {
// utilize map structure ,key Store values ,value Store subscripts
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;
}
}
Two 、01 Sum of two numbers
2.1 subject
Given two arrays , Write a function to calculate their intersection .
Example 1:
Input : nums1 = [1,2,2,1], nums2 = [2,2]
Output : [2]
Example 1:
Input : nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output : [9,4]
explain :
Each element of the output must be unique .
We can ignore the order of output results .
2.2 Realization
/**349. Intersection of two arrays **/
public class Intersection {
/** * establish set aggregate , Use its weight removal characteristics to screen * establish nums aggregate , Used to store elements that do not overlap * Loop through groups 2, If set Contains an array 2 The elements of , Then add this element to set2 in * Traverse set aggregate , Store the results in an array */
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)));
}
// Method 1:Set aggregate
public static int[] intersection1(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>(); // HashSet characteristic : disorder 、 No repetition 、 No index .
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;
}
// Method 2:Map aggregate
public static int[] intersection2(int[] nums1, int[] nums2) {
// establish map aggregate
Map<Integer, Integer> map = new HashMap<>();
// Will array 1 All the elements in are added to map Collection , And the default value is 1, It is equivalent to the operation of weight removal
for(int i = 0; i < nums1.length; i++){
map.put(nums1[i], 1);
}
// establish list aggregate , Used to store results
ArrayList<Integer> list = new ArrayList<>();
// Loop through groups 2, If map Contains arrays 2 The elements of , And its value Greater than 0, Then add it to list Collection , Update it 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);// Put it value to update , That is to say -1, It's equivalent to removing weight
}
}
// Create array , For storing results , take list The elements in are assigned to the array , And back to
int len = list.size();
int[] res = new int[len];
for(int i = 0; i < len; i++){
res[i] = list.remove(0);
}
return res;
}
}
边栏推荐
- 背包模型 AcWing 1022. 宠物小精灵之收服
- 十年架构五年生活-07 年轻气盛的蜕变
- Study notes Minio
- 15 design movie rental system
- Error: image clipToBoundsAndScale, argument 'input'
- Time and power allocation method to ensure fairness in sensor fusion system
- 熵与形态的非递进现象
- What changes will metauniverse bring to the music industry in the trillion market?
- 12 is at least twice the maximum number of other numbers
- Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
猜你喜欢

Play with the cluster configuration center and learn about the Taier console

How to create a.Net image with diagnostic tools

ethereum rpc

栈 AcWing 3302. 表达式求值

黑白像素分布对迭代次数的影响

Internal and external troubles of digital collection NFT "boring ape" bayc

MySQL installation (RPM package)

图片中非0值的数量对分类的影响

BeautifulSoup的使用

The second method of calculating overlapping integral
随机推荐
Overview of radar communication integrated waveform design
c语言指针函数和函数指针的辨析
Synaesthesia integrated de cellular super large-scale MIMO and high-frequency wireless access technology
背包模型 AcWing 1024. 装箱问题
Background color style modification on table hover in antd
Description and feelings
Non progressive phenomena of entropy and morphology
栈 AcWing 3302. 表达式求值
15th largest value of data flow
发动机悬置系统冲击仿真-瞬时模态动态分析与响应谱分析
最短移动距离和形态复合体的熵
背包模型 AcWing 423. 采药
How to build a real-time development platform to deeply release the value of enterprise real-time data?
最长上升子序列模型 AcWing 1010. 拦截导弹
tensorflow运行报错解决方法
最长上升子序列模型 AcWing 1014. 登山
6 find the smallest letter larger than the target letter
如何创建一个带诊断工具的.NET镜像
Local and overall differences between emergence and morphology
IMG SRC is empty or SRC does not exist, and the picture has a white border