и посмотреть медиа
Golang | LeetCode
и посмотреть медиа
LeetCode tasks in Golang. Solutions with code and explanations.
LeetCode tasks in Golang. Solutions with code and explanations.
AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.
Channel with solutions to popular LeetCode tasks in Go. Focus on optimized algorithms, efficient memory usage, and idiomatic code. Helps prepare for technical interviews in companies that use Golang.
Задача: 647. Palindromic Substrings Сложность: medium Реализуйте структуру данных MapSum, поддерживающую: - Insert(key string, val int) — вставляет или обновляет значение по ключу. - Sum(prefix string) — возвращает сумму значений всех ключей, начинающихся с указанного префикса. Пример: mapSum := Constructor() mapSum.Insert("apple", 3) mapSum.Sum("ap") // 3 mapSum.Insert("app", 2) mapSum.Sum("ap") // 5 (3 + 2) 👨💻 Алгоритм: 1⃣Хранение данных Используем map[string]int для хранения всех ключей и их значений. 2⃣Операция Insert Если ключ уже существует — просто перезаписываем новое значение. 3⃣Операция Sum Проходим по всем ключам. Если ключ начинается с prefix, прибавляем его значение к результату. 😎 Решение: package main import "strings" type MapSum struct { mapData map[string]int } func Constructor() MapSum { return MapSum{mapData: make(map[string]int)} } func (this *MapSum) Insert(key string, val int) { this.mapData[key] = val } func (this *MapSum) Sum(prefix string) int { ans := 0 for key, val := range this.mapData { if strings.HasPrefix(key, prefix) { ans += val } } return ans } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!