Работа над упражнением с leetcode, которое вы можете увидеть здесь: https://leetcode.com/problems/unique-morse-code-words/
У меня проблемы с получением правильного ответа, но еще больше проблем с поиском проблемы.Я пытаюсь использовать cout для печати векторов, с которыми я работаю, чтобы выяснить, в чем дело, но, похоже, по какой-то причине он выводит пустую строку.
Вот мой код ...
#include <array>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
int num_of_uniq_words = 0;
string arr[] = {"a","b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m","n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
string maps[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..",
"--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
vector<string> all_words_morse;
for (int i = 0; i < words.size(); i++) {
string morse;
for (int j = 0; j < words[i].length(); j++){
for(int q = 0; q < sizeof(arr)/sizeof(arr[0]); q++) {
if (arr[q] == to_string(words[i].at(j)))
morse.append(maps[q]);
}
}
//cout << morse << endl;
all_words_morse.push_back(morse);
}
vector<string> uniq_words;
for(int i = 0; i < all_words_morse.size(); i++) {
if (find(uniq_words.begin(), uniq_words.end(), all_words_morse[i]) == uniq_words.end()) //not present
uniq_words.push_back(all_words_morse[i]);
}
//printing
for (int i = 0; i < all_words_morse.size(); i++)
cout << all_words_morse[i] << " ";
cout << "\n" << endl;
for (int i = 0; i < uniq_words.size(); i++)
cout << uniq_words[i] << " ";
cout << "\n" << endl;
num_of_uniq_words = uniq_words.size();
return num_of_uniq_words;
}
};
и при вводе тестового примера ["gin", "zen", "gig", "msg"] sdtout будет ... "
"
, что составляет около 4 пустых строкСтрока и я не понимаю почему.У кого-нибудь есть совет или знаете, что я делаю не так ??Спасибо