Skip to content

Commit f26ebcf

Browse files
authored
Merge pull request gzc426#172 from gityjx/master
leetcode443
2 parents 373999b + 7617104 commit f26ebcf

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

2018.11.29-leetcode443/halfofwater.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Leetcode 443
2+
3+
```
4+
class Solution {
5+
public:
6+
int compress(vector<char>& chars) {
7+
8+
if (chars.size() <= 1) return chars.size();
9+
10+
vector<char>::iterator it = chars.begin();
11+
++it;
12+
int count = 1;
13+
char tmp = chars[0];
14+
15+
char ccc = *(chars.end() - 1);
16+
while (it < chars.end())
17+
{
18+
if (*it == tmp)
19+
{
20+
21+
++count;
22+
if (it == chars.end() - 1) {
23+
if (count != 1) {
24+
25+
string s = to_string(count);
26+
for (char c : s) {
27+
it = chars.insert(it, c);
28+
++it;
29+
}
30+
it = chars.erase(it);
31+
}
32+
//++it;
33+
}
34+
else {
35+
it = chars.erase(it);
36+
}
37+
}
38+
else
39+
{
40+
if (count != 1)
41+
{
42+
string s = to_string(count);
43+
for (char c : s)
44+
{
45+
it = chars.insert(it, c);
46+
++it;
47+
}
48+
}
49+
50+
tmp = *it;
51+
count = 1;
52+
++it;
53+
}
54+
}
55+
return chars.size();
56+
}
57+
};
58+
59+
```

2018.12.1-leetcode387/halfofwater.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Leetcode 387
2+
3+
``` C++
4+
class Solution {
5+
public:
6+
int firstUniqChar(string s) {
7+
if (s.size() == 0) return -1;
8+
if (s.size() < 2) return 0;
9+
string alphabet = "abcdefghijklmnopqrstuvwxyz";
10+
map<char, int> countMap;
11+
s += '\0';
12+
char *sHead = (char *)s.data();
13+
char *head = sHead;
14+
while (*sHead != '\0')
15+
{
16+
countMap[*sHead]++;
17+
++sHead;
18+
}
19+
for (int i = 0; i < s.size(); i++) {
20+
if (countMap[s[i]] == 1){
21+
return i;
22+
}
23+
}
24+
return -1;
25+
}
26+
};
27+
```

0 commit comments

Comments
 (0)