Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- input: vetor de caracteres
- retorna: o comprimento do vetor de caracteres comprimido
- após a repetição de caracteres iguais, deve-se colocar apenas uma ocorrencia deste caractere, e sua quantidade
- 2
- R
- ["a","2","b","2","c","3","c"]
- R
- ["a", "3", "b", "b"]
- x
- ["a", "b"]
- ["a", "b"]
- ["a", "b", "b"]
- ["a", "b", "b"]
- */
- // input = 123
- //
- vector<char> intToCharVector(int input) {
- if (input == 0) {
- answer.push_back('0');
- return answer;
- }
- while (input > 0) {
- int lastDigit = input % 10; // 1
- char lastDigitChar = lastDigit + '0';
- answer.push_back(lastDigitChar);
- input /= 10;
- }
- reverse(answer.begin(), answer.end());
- return answer;
- }
- Ri
- // ["a","2","b","2","c","3","c"]
- int solve(vector<char> &input) {
- if (input.size() == 0) {
- return input.size();
- }
- int currentCharCount = 0; // 3
- int writePointer = 0; // 0
- for (int i = 0; i < input.size(); i++) {
- currentCharCount += 1;
- if (i == input.size() - 1 || s[i] != s[i+1]) {
- input[writePointer] = currentChar;
- writePointer++;
- if (currentCharCount > 1) {
- vector<char> digits = intToCharVector(currentCharCount);
- for (auto d : digits) {
- input[writePointer] = d;
- writePointer++;
- }
- }
- currentCharCount = 0;
- }
- }
- return writePointer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement