Skip to content

Commit 67e98c2

Browse files
authored
Merge pull request #1250 from barryxt/master
536-Week 08
2 parents be28a8a + cff2977 commit 67e98c2

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <map>
3+
4+
using namespace std;
5+
//时间复杂度:O(n),n为字符串的长度
6+
//空间复杂度:O(n)
7+
class Solution {
8+
public:
9+
int firstUniqChar(string s) {
10+
//unordered_map<char,int> mapS;
11+
map<char,int> mapS;
12+
for(auto c : s)
13+
++ mapS[c];
14+
for(int i = 0; i < s.size(); ++i) {
15+
if(mapS[s[i]] == 1)
16+
return i;
17+
}
18+
return -1;
19+
}
20+
};
21+
22+
int main()
23+
{
24+
Solution s;
25+
int res = s.firstUniqChar("lol");
26+
cout << res << endl;
27+
return 0;
28+
}

Week 08/id_536/leetcode_58_536.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int lengthOfLastWord(string s) {
9+
int res = 0;
10+
for(int i = s.length()-1; i >= 0; --i) {
11+
if(s[i] != ' ')
12+
++ res;
13+
else {
14+
if(res)
15+
break;
16+
}
17+
}
18+
return res;
19+
}
20+
};
21+
22+
int main()
23+
{
24+
Solution s;
25+
int res = s.lengthOfLastWord("djfks djsjd dd 23234");
26+
cout << res << endl;
27+
return 0;
28+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <set>
4+
#include <map>
5+
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
/*****
11+
解法1:暴力法
12+
时间复杂度:O(J*S)
13+
空间复杂度:O(1)
14+
*****/
15+
int helper1(string J, string S) {
16+
int res = 0;
17+
for(int i = 0; i < S.length(); ++ i) {
18+
for(int j = 0; j < J.length(); ++ j) {
19+
if(S[i] == J[j]) {
20+
++ res;
21+
}
22+
}
23+
}
24+
return res;
25+
}
26+
27+
/*****
28+
解法2:set实现
29+
时间复杂度:O(J+S)
30+
空间复杂度:O(J)
31+
*****/
32+
int helper2(string J, string S) {
33+
int res = 0;
34+
//unordered_set<char> setJ(J.begin(),J.end());
35+
set<char> setJ(J.begin(),J.end());
36+
for(int i = 0; i < S.length(); ++i) {
37+
if(setJ.count(S[i]))
38+
++ res;
39+
}
40+
return res;
41+
}
42+
43+
/*****
44+
解法3:map实现
45+
时间复杂度:O(J+S)
46+
空间复杂度:O(J)
47+
*****/
48+
int helper3(string J, string S) {
49+
int res = 0;
50+
//unordered_map<char,int> mapJ;
51+
map<char,int> mapJ;
52+
for(auto c : J)
53+
mapJ[c] = 1;
54+
for(auto c : S)
55+
res += mapJ[c];
56+
return res;
57+
}
58+
59+
int numJewelsInStones(string J, string S) {
60+
int res = 0;
61+
//res = helper1(J,S);
62+
//res = helper2(J,S);
63+
res = helper3(J,S);
64+
return res;
65+
}
66+
};
67+
68+
int main()
69+
{
70+
Solution s;
71+
int res = s.numJewelsInStones("aA","aAAbbbb");
72+
cout << res << endl;
73+
return 0;
74+
}

0 commit comments

Comments
 (0)