-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.cpp
36 lines (32 loc) · 864 Bytes
/
compress.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
#include<vector>
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
int compress(vector<char>& chars) {
int length = 0;
int i = 0;
//cout << chars.size() << endl;
while(i < chars.size()){
int j = i ;
int count = 0;
chars[length++] = chars[i];
for(;j < chars.size(); ++j){
cout << "i: " << i << ", j: " << j << endl;
if(chars[i] == chars[j]){
count++;
}else
break;
}
cout << count << endl;
if(count > 1){
string s = to_string(count);
for(auto &k : s)
chars[length++] = k;
}
i = j;
}
return length;
}
};