main.cpp Güncelle

This commit is contained in:
Yavuz Sava 2025-03-29 19:20:55 +00:00
parent 42222089e3
commit 80db0c5f3e

View File

@ -1,17 +1,19 @@
#include <unordered_set>
#include <string>
#include <vector> #include <vector>
#include <unordered_map>
class Solution { class Solution {
public: public:
std::vector<int> twoSum(std::vector<int>& nums, int target) { int numUniqueEmails(std::vector<std::string>& emails) {
std::unordered_map<int, int> num_map; // Stores number and its index std::unordered_set<std::string> uniqueEmails;
for (int i = 0; i < nums.size(); ++i) { for (const auto& email : emails) {
int complement = target - nums[i]; int atPos = email.find('@');
if (num_map.find(complement) != num_map.end()) { std::string local = email.substr(0, atPos);
return {num_map[complement], i}; // Found the two indices std::string domain = email.substr(atPos);
} local.erase(std::remove(local.begin(), local.end(), '.'), local.end()); // Remove '.' characters in the local part
num_map[nums[i]] = i; // Store index of the current number local = local.substr(0, local.find('+')); // Remove everything after the first '+' in the local part
uniqueEmails.insert(local + domain); // Combine back to form the unique email
} }
return {}; return uniqueEmails.size();
} }
}; };