-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1002.最大宽度坡.cpp
81 lines (80 loc) · 1.88 KB
/
1002.最大宽度坡.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* @lc app=leetcode.cn id=1002 lang=cpp
*
* [1002] 最大宽度坡
*
* https://leetcode-cn.com/problems/find-common-characters/description/
*
* algorithms
* Easy (59.95%)
* Total Accepted: 697
* Total Submissions: 1.2K
* Testcase Example: '["bella","label","roller"]'
*
* 给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3
* 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。
*
* 你可以按任意顺序返回答案。
*
*
*
* 示例 1:
*
* 输入:["bella","label","roller"]
* 输出:["e","l","l"]
*
*
* 示例 2:
*
* 输入:["cool","lock","cook"]
* 输出:["c","o"]
*
*
*
*
* 提示:
*
*
* 1 <= A.length <= 100
* 1 <= A[i].length <= 100
* A[i][j] 是小写字母
*
*
*/
class Solution {
public:
vector<string> commonChars(vector<string>& A) {
map<char,int> charmap;
for(int i=0; i<A.at(0).size(); i++)
{
charmap[A.at(0).at(i)]++;
}
for(int i=0; i<A.size(); i++)
{
map<char,int> newcharmap;
for(int k=0; k<A.at(i).size(); k++)
{
if(charmap[A.at(i).at(k)] >0)
{
charmap[A.at(i).at(k)]--;
newcharmap[A.at(i).at(k)]++;
}
}
charmap.clear();
charmap = newcharmap;
}
map<char,int>::iterator ii;
vector<string> out;
for(ii=charmap.begin(); ii!=charmap.end(); ii++)
{
while(ii->second>0)
{
ii->second--;
stringstream ss;
ss<<ii->first;
out.push_back(ss.str());
}
}
return out;
}
};