Skip to content

Commit 8149862

Browse files
committed
Chapter 04 section 02 C++ codes updated. multiset codes added. Java codes added.
1 parent 7a6e872 commit 8149862

File tree

6 files changed

+189
-38
lines changed

6 files changed

+189
-38
lines changed

04-Using-Hash-Table/Course Code (C++)/02-Intersection-of-Two-Arrays-II/basic_map_main.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,42 @@ using namespace std;
77
int main() {
88

99
map<int,int> myMap;
10-
if( myMap.find( 42 ) == myMap.end() )
11-
cout<<"Can not find element 42"<<endl;
10+
if(myMap.find(42) == myMap.end())
11+
cout << "Can not find element 42" << endl;
1212
else
13-
cout<<"Element 42 is in the map"<<endl;
13+
cout << "Element 42 is in the map" << endl;
1414

15-
cout<<myMap[42]<<endl;
16-
if( myMap.find( 42 ) == myMap.end() )
17-
cout<<"Can not find element 42"<<endl;
15+
cout << myMap[42] << endl; // 输出 0
16+
17+
// 注意: 在C++中, map的key一旦被访问过, 就会添加默认的(key, value)对在map中!
18+
// 以下代码将找到42, 因为之前使用myMap[42]的方式对42进行了访问!
19+
if(myMap.find(42) == myMap.end())
20+
cout << "Can not find element 42" << endl;
1821
else
19-
cout<<"Element 42 is in the map"<<endl;
22+
cout << "Element 42 is in the map" << endl;
2023

2124
myMap[42] ++;
22-
cout<<myMap[42]<<endl;
23-
if( myMap.find( 42 ) == myMap.end() )
24-
cout<<"Can not find element 42"<<endl;
25+
cout << myMap[42] << endl; // 输出 1
26+
if(myMap.find(42) == myMap.end())
27+
cout << "Can not find element 42" << endl;
2528
else
26-
cout<<"Element 42 is in the map"<<endl;
29+
cout << "Element 42 is in the map" << endl;
2730

2831
myMap[42] --;
29-
cout<<myMap[42]<<endl;
30-
if( myMap.find( 42 ) == myMap.end() )
31-
cout<<"Can not find element 42"<<endl;
32+
cout << myMap[42] << endl; // 输出 0
33+
34+
// 注意: key对应的值为0, 不代表key不存在
35+
if(myMap.find(42) == myMap.end())
36+
cout << "Can not find element 42" << endl;
3237
else
33-
cout<<"Element 42 is in the map"<<endl;
38+
cout << "Element 42 is in the map" << endl;
3439

35-
myMap.erase( 42 );
36-
if( myMap.find( 42 ) == myMap.end() )
37-
cout<<"Can not find element 42"<<endl;
40+
// 使用erase删除一个key
41+
myMap.erase(42);
42+
if(myMap.find(42) == myMap.end())
43+
cout << "Can not find element 42" << endl;
3844
else
39-
cout<<"Element 42 is in the map"<<endl;
45+
cout << "Element 42 is in the map" << endl;
4046

4147
return 0;
4248
}

04-Using-Hash-Table/Course Code (C++)/02-Intersection-of-Two-Arrays-II/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class Solution {
99
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
1010

1111
map<int, int> record;
12-
for( int i = 0 ; i < nums1.size() ; i ++ )
12+
for(int i = 0 ; i < nums1.size() ; i ++)
1313
record[nums1[i]] += 1;
1414

1515
vector<int> resultVector;
16-
for( int i = 0 ; i < nums2.size() ; i ++ )
17-
if( record[ nums2[i] ] > 0 ){
18-
resultVector.push_back( nums2[i] );
16+
for(int i = 0 ; i < nums2.size() ; i ++)
17+
if(record[nums2[i]] > 0){
18+
resultVector.push_back(nums2[i]);
1919
record[nums2[i]] --;
2020
}
2121

@@ -32,9 +32,9 @@ int main() {
3232
vector<int> vec2(nums2, nums2 + sizeof(nums2)/sizeof(int));
3333

3434
vector<int> res = Solution().intersect(vec1, vec2);
35-
for(int i = 0 ; i < res.size() ; i ++ )
36-
cout<<res[i]<<" ";
37-
cout<<endl;
35+
for(int i = 0 ; i < res.size() ; i ++)
36+
cout << res[i] << " ";
37+
cout << endl;
3838

3939
return 0;
4040
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <vector>
33
#include <map>
4+
45
using namespace std;
56

67
/// 350. Intersection of Two Arrays II
@@ -9,20 +10,21 @@ class Solution {
910
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
1011

1112
map<int, int> record;
12-
for( int i = 0 ; i < nums1.size() ; i ++ )
13-
if( record.find(nums1[i]) == record.end() )
14-
record.insert( make_pair(nums1[i],1));
13+
for(int i = 0 ; i < nums1.size() ; i ++)
14+
if(record.find(nums1[i]) == record.end())
15+
record.insert(make_pair(nums1[i],1));
1516
else
1617
record[nums1[i]] += 1;
1718

1819
vector<int> resultVector;
19-
for( int i = 0 ; i < nums2.size() ; i ++ )
20-
if( record.find(nums2[i]) != record.end() &&
21-
record[ nums2[i] ] > 0 ){
22-
resultVector.push_back( nums2[i] );
20+
for(int i = 0 ; i < nums2.size() ; i ++)
21+
if(record.find(nums2[i]) != record.end() &&
22+
record[nums2[i]] > 0){
23+
24+
resultVector.push_back(nums2[i]);
2325
record[nums2[i]] --;
24-
if( record[nums2[i]] == 0 )
25-
record.erase( nums2[i] );
26+
if(record[nums2[i]] == 0)
27+
record.erase(nums2[i]);
2628
}
2729

2830
return resultVector;
@@ -38,9 +40,9 @@ int main() {
3840
vector<int> vec2(nums2, nums2 + sizeof(nums2)/sizeof(int));
3941

4042
vector<int> res = Solution().intersect(vec1, vec2);
41-
for(int i = 0 ; i < res.size() ; i ++ )
42-
cout<<res[i]<<" ";
43-
cout<<endl;
43+
for(int i = 0 ; i < res.size() ; i ++)
44+
cout << res[i] << " ";
45+
cout << endl;
4446

4547
return 0;
4648
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <set>
4+
5+
using namespace std;
6+
7+
/// 350. Intersection of Two Arrays II
8+
/// Using multiset
9+
class Solution {
10+
public:
11+
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
12+
13+
multiset<int> record;
14+
for(int num: nums1)
15+
record.insert(num);
16+
17+
multiset<int> result;
18+
for(int num: nums2){
19+
multiset<int>::iterator iter = record.find(num);
20+
if( iter != record.end()){
21+
result.insert(num);
22+
record.erase(iter);
23+
}
24+
}
25+
26+
return vector<int>(result.begin(), result.end());
27+
}
28+
};
29+
30+
int main() {
31+
32+
int nums1[] = {1, 2, 2, 1};
33+
vector<int> vec1(nums1, nums1 + sizeof(nums1)/sizeof(int));
34+
35+
int nums2[] = {2, 2};
36+
vector<int> vec2(nums2, nums2 + sizeof(nums2)/sizeof(int));
37+
38+
vector<int> res = Solution().intersect(vec1, vec2);
39+
for(int i = 0 ; i < res.size() ; i ++)
40+
cout << res[i] << " ";
41+
cout << endl;
42+
43+
return 0;
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/// 让我们来测试使用Java中的TreeMap:)
2+
3+
import java.util.TreeMap;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
9+
TreeMap<Integer, Integer> myMap = new TreeMap<Integer, Integer>();
10+
if(myMap.containsKey(42))
11+
System.out.println("Element 42 is in the map");
12+
else
13+
System.out.println("Can not find element 42");
14+
15+
System.out.println(myMap.get(42)); // 输出 null
16+
17+
// Java不存在C++中默认的访问key即添加默认(key, value)的行为
18+
// 以下代码仍然无法找到42
19+
if(myMap.containsKey(42))
20+
System.out.println("Element 42 is in the map");
21+
else
22+
System.out.println("Can not find element 42");
23+
24+
myMap.put(42, 0);
25+
myMap.put(42, myMap.get(42) + 1);
26+
System.out.println(myMap.get(42)); // 输出 1
27+
if(myMap.containsKey(42))
28+
System.out.println("Element 42 is in the map");
29+
else
30+
System.out.println("Can not find element 42");
31+
32+
myMap.put(42, myMap.get(42) - 1);
33+
System.out.println(myMap.get(42)); // 输出 0
34+
35+
// 注意: key对应的值为0, 不代表key不存在
36+
if(myMap.containsKey(42))
37+
System.out.println("Element 42 is in the map");
38+
else
39+
System.out.println("Can not find element 42");
40+
41+
// 注意: 也不可以为key对应的值设置null来删除一个key
42+
myMap.put(42, null);
43+
if(myMap.containsKey(42))
44+
System.out.println("Element 42 is in the map");
45+
else
46+
System.out.println("Can not find element 42");
47+
48+
// 使用remove删除一个key
49+
myMap.remove(42);
50+
if(myMap.containsKey(42))
51+
System.out.println("Element 42 is in the map");
52+
else
53+
System.out.println("Can not find element 42");
54+
}
55+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.TreeMap;
2+
import java.util.ArrayList;
3+
4+
/// 350. Intersection of Two Arrays II
5+
public class Solution {
6+
7+
public int[] intersect(int[] nums1, int[] nums2) {
8+
9+
TreeMap<Integer, Integer> record = new TreeMap<Integer, Integer>();
10+
for(int num: nums1)
11+
if(!record.containsKey(num))
12+
record.put(num, 1);
13+
else
14+
record.put(num, record.get(num) + 1);
15+
16+
ArrayList<Integer> result = new ArrayList<Integer>();
17+
for(int num: nums2)
18+
if(record.containsKey(num) && record.get(num) > 0){
19+
result.add(num);
20+
record.put(num, record.get(num) - 1);
21+
}
22+
23+
int[] ret = new int[result.size()];
24+
int index = 0;
25+
for(Integer num: result)
26+
ret[index++] = num;
27+
28+
return ret;
29+
}
30+
31+
private static void printArr(int[] arr){
32+
for(int e: arr)
33+
System.out.print(e + " ");
34+
System.out.println();
35+
}
36+
37+
public static void main(String[] args) {
38+
39+
int[] nums1 = {1, 2, 2, 1};
40+
int[] nums2 = {2, 2};
41+
int[] res = (new Solution()).intersect(nums1, nums2);
42+
printArr(res);
43+
}
44+
}

0 commit comments

Comments
 (0)