378. Kth Smallest Element in a Sorted Matrix
Дана матрица размером n x n, где каждая строка и каждый столбец отсортированы в порядке возрастания. Верните k-й наименьший элемент в матрице.
Заметьте, что это k-й наименьший элемент в отсортированном порядке, а не k-й уникальный элемент.
C# решение
сопоставлено/оригиналusing System;
using System.Collections.Generic;
public class Solution {
private bool Dfs(string word, int length, bool[] visited, HashSet<string> dictionary) {
if (length == word.Length) {
return true;
}
if (visited[length]) {
return false;
}
visited[length] = true;
for (int i = word.Length - (length == 0 ? 1 : 0); i > length; --i) {
if (dictionary.Contains(word.Substring(length, i - length)) &&
Dfs(word, i, visited, dictionary)) {
return true;
}
}
return false;
}
public IList<string> FindAllConcatenatedWordsInADict(string[] words) {
HashSet<string> dictionary = new HashSet<string>(words);
List<string> answer = new List<string>();
foreach (string word in words) {
bool[] visited = new bool[word.Length];
if (Dfs(word, 0, visited, dictionary)) {
answer.Add(word);
}
}
return answer;
}
}
C++ решение
auto-draft, проверить перед отправкой#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
private bool Dfs(string word, int length, bool[] visited, HashSet<string> dictionary) {
if (length == word.size()) {
return true;
}
if (visited[length]) {
return false;
}
visited[length] = true;
for (int i = word.size() - (length == 0 ? 1 : 0); i > length; --i) {
if (dictionary.Contains(word.Substring(length, i - length)) &&
Dfs(word, i, visited, dictionary)) {
return true;
}
}
return false;
}
public vector<string> FindAllConcatenatedWordsInADict(vector<string> words) {
HashSet<string> dictionary = new HashSet<string>(words);
List<string> answer = new List<string>();
foreach (string word in words) {
bool[] visited = new bool[word.size()];
if (Dfs(word, 0, visited, dictionary)) {
answer.push_back(word);
}
}
return answer;
}
}
Java решение
auto-draft, проверить перед отправкойimport java.util.*;
import java.math.*;
// Auto-generated Java draft from the C# solution. Review API differences before LeetCode submit.
public class Solution {
private boolean Dfs(String word, int length, boolean[] visited, HashSet<String> dictionary) {
if (length == word.length) {
return true;
}
if (visited[length]) {
return false;
}
visited[length] = true;
for (int i = word.length - (length == 0 ? 1 : 0); i > length; --i) {
if (dictionary.Contains(word.Substring(length, i - length)) &&
Dfs(word, i, visited, dictionary)) {
return true;
}
}
return false;
}
public List<String> FindAllConcatenatedWordsInADict(String[] words) {
HashSet<String> dictionary = new HashSet<String>(words);
List<String> answer = new List<String>();
foreach (String word in words) {
boolean[] visited = new boolean[word.length];
if (Dfs(word, 0, visited, dictionary)) {
answer.add(word);
}
}
return answer;
}
}
JavaScript решение
сопоставлено/оригиналclass Solution {
dfs(word, length, visited, dictionary) {
if (length === word.length) {
return true;
}
if (visited[length]) {
return false;
}
visited[length] = true;
for (let i = word.length - (length === 0 ? 1 : 0); i > length; i--) {
if (dictionary.has(word.slice(length, i)) && this.dfs(word, i, visited, dictionary)) {
return true;
}
}
return false;
}
findAllConcatenatedWordsInADict(words) {
const dictionary = new Set(words);
const answer = [];
for (const word of words) {
const visited = Array(word.length).fill(false);
if (this.dfs(word, 0, visited, dictionary)) {
answer.push(word);
}
}
return answer;
}
}
const solution = new Solution();
const words = ["cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"];
console.log(solution.findAllConcatenatedWordsInADict(words));
Go решение
сопоставлено/оригиналpackage main
import (
"strings"
)
type Solution struct{}
func (s Solution) dfs(word string, length int, visited []bool, dictionary map[string]bool) bool {
if length == len(word) {
return true
}
if visited[length] {
return false
}
visited[length] = true
for i := len(word) - 1; i > length; i-- {
if dictionary[word[length:i]] && s.dfs(word, i, visited, dictionary) {
return true
}
}
return false
}
func (s Solution) findAllConcatenatedWordsInADict(words []string) []string {
dictionary := make(map[string]bool)
for _, word := range words {
dictionary[word] = true
}
var answer []string
for _, word := range words {
visited := make([]bool, len(word))
if s.dfs(word, 0, visited, dictionary) {
answer = append(answer, word)
}
}
return answer
}
func main() {
solution := Solution{}
words := []string{"cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"}
result := solution.findAllConcatenatedWordsInADict(words)
for _, word := range result {
println(word)
}
}
Вакансии для этой задачи
Показаны активные вакансии с пересечением по тегам задачи.
Активных вакансий пока нет.