Skip to content

Commit e89c036

Browse files
authored
Add files via upload
1 parent d6acebb commit e89c036

6 files changed

+367
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class TrieNode {
2+
public:
3+
TrieNode* child[26];
4+
bool isWord;
5+
TrieNode() {
6+
isWord = false;
7+
for (auto& c : child) {
8+
c = nullptr;
9+
}
10+
}
11+
};
12+
13+
class WordDictionary {
14+
TrieNode* root;
15+
public:
16+
WordDictionary() {
17+
root = new TrieNode();
18+
}
19+
20+
void addWord(string word) {
21+
TrieNode* node = root;
22+
for (char& c : word) {
23+
int i = c - 'a';
24+
if (!node->child[i]) {
25+
node->child[i] = new TrieNode();
26+
}
27+
node = node->child[i];
28+
}
29+
node->isWord = true;
30+
}
31+
32+
bool help(const string& word, int pos, TrieNode* node) {
33+
if (pos == word.size()) {
34+
return node->isWord;
35+
}
36+
char c = word[pos];
37+
int token = c - 'a';
38+
if (c != '.') {
39+
return node -> child[token] != nullptr && help(word, pos + 1, node->child[token]);
40+
}
41+
for (int i = 0; i < 26; i++) {
42+
if (node->child[i] != nullptr && help(word, pos + 1, node->child[i])) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
49+
bool search(string word) {
50+
return help(word, 0, root);
51+
}
52+
};
53+
54+
/**
55+
* Your WordDictionary object will be instantiated and called as such:
56+
* WordDictionary* obj = new WordDictionary();
57+
* obj->addWord(word);
58+
* bool param_2 = obj->search(word);
59+
*/

C++/0212. Word Search II.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class TrieNode {
2+
public:
3+
bool isWord;
4+
TrieNode* child[26];
5+
6+
TrieNode() {
7+
isWord = false;
8+
for (auto& c : child) {
9+
c = nullptr;
10+
}
11+
}
12+
};
13+
14+
class Solution {
15+
int row;
16+
int col;
17+
TrieNode* root;
18+
public:
19+
void addWord(string word) {
20+
TrieNode* node = root;
21+
for (char& c : word) {
22+
int i = c - 'a';
23+
if (!node->child[i]) {
24+
node->child[i] = new TrieNode();
25+
}
26+
node = node->child[i];
27+
}
28+
node->isWord = true;
29+
}
30+
31+
void dfs(const vector<vector<char>>& board, int y, int x, TrieNode* node, string word, vector<vector<bool>>& visit, vector<string>& ans) {
32+
if (x < 0 || y < 0 || x >= col || y >= row || visit[y][x] == true) {
33+
return;
34+
}
35+
36+
char c = board[y][x];
37+
int token = c - 'a';
38+
39+
node = node->child[token];
40+
41+
if (!node) {
42+
return;
43+
}
44+
45+
word += board[y][x];
46+
47+
if (node->isWord) {
48+
ans.push_back(word);
49+
node->isWord = false; // Mark the word as found
50+
}
51+
52+
53+
visit[y][x] = true;
54+
dfs(board, y - 1, x , node, word, visit, ans);
55+
dfs(board, y , x + 1, node, word, visit, ans);
56+
dfs(board, y + 1, x , node, word, visit, ans);
57+
dfs(board, y , x - 1, node, word, visit, ans);
58+
visit[y][x] = false;
59+
}
60+
61+
62+
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
63+
vector<string> ans;
64+
row = board.size();
65+
col = board[0].size();
66+
67+
root = new TrieNode();
68+
69+
for (string& str : words) {
70+
addWord(str);
71+
}
72+
73+
string key;
74+
vector<vector<bool>> visit(row, vector<bool>(col, false));
75+
for (int i = 0; i < row; i++) {
76+
for (int j = 0; j < col; j++) {
77+
dfs(board, i, j, root, key, visit, ans);
78+
}
79+
}
80+
81+
return ans;
82+
}
83+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
class TrieNode {
3+
public:
4+
bool isEnd;
5+
TrieNode* child[2];
6+
TrieNode() {
7+
isEnd = false;
8+
for (auto& c : child) {
9+
c = nullptr;
10+
}
11+
}
12+
};
13+
class Solution {
14+
TrieNode* root;
15+
public:
16+
void addNum(int num) {
17+
TrieNode* node = root;
18+
int token = 0;
19+
for (int i = 31; i >= 0; i--) {
20+
token = (num >> i) & 1;
21+
if (!node->child[token]) {
22+
node->child[token] = new TrieNode();
23+
}
24+
node = node->child[token];
25+
}
26+
}
27+
28+
int search(int num) {
29+
int sum = 0;
30+
TrieNode* node = root;
31+
for (int i = 31; i >= 0; i--) {
32+
int key = (num >> i) & 1;
33+
int other = key ? 0 : 1;
34+
if (!node->child[other]) {
35+
node = node->child[key];
36+
}
37+
else {
38+
sum += (1 << i);
39+
node = node->child[other];
40+
}
41+
}
42+
return sum;
43+
}
44+
45+
int findMaximumXOR(vector<int>& nums) {
46+
root = new TrieNode();
47+
for (const auto& n : nums) {
48+
addNum(n);
49+
}
50+
51+
int ans = 0;
52+
for (const auto& n : nums) {
53+
ans = max(ans, search(n));
54+
}
55+
56+
return ans;
57+
}
58+
};
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
class TrieNode {
3+
public:
4+
TrieNode* child[2];
5+
TrieNode() {
6+
for (auto& c : child) {
7+
c = nullptr;
8+
}
9+
}
10+
};
11+
12+
bool cmp(vector<int>&a, vector<int>& b) {
13+
return a[1] < b[1];
14+
}
15+
16+
class Solution {
17+
TrieNode* root;
18+
19+
public:
20+
21+
void addNum(int num) {
22+
TrieNode* node = root;
23+
int token = 0;
24+
for (int i = 31; i >= 0; i--) {
25+
token = (num >> i) & 1;
26+
if (!node->child[token]) {
27+
node->child[token] = new TrieNode();
28+
}
29+
node = node->child[token];
30+
}
31+
}
32+
33+
int search(int num) {
34+
int sum = 0;
35+
TrieNode* node = root;
36+
for (int i = 31; i >= 0; i--) {
37+
int key = (num >> i) & 1;
38+
int other = key ? 0 : 1;
39+
if (!node->child[other]) {
40+
node = node->child[key];
41+
}
42+
else {
43+
sum += (1 << i);
44+
node = node->child[other];
45+
}
46+
}
47+
return sum;
48+
}
49+
50+
vector<int> maximizeXor(vector<int>& nums, vector<vector<int>>& queries) {
51+
root = new TrieNode();
52+
53+
vector<int> ans(queries.size(), -1);
54+
55+
for (int i = 0; i < queries.size(); i++) {
56+
queries[i].push_back(i);
57+
}
58+
59+
sort(nums.begin(), nums.end());
60+
sort(queries.begin(), queries.end(), cmp);
61+
62+
int pos = 0;
63+
for (int i = 0; i < queries.size(); i++) {
64+
while (pos < nums.size() && nums[pos] <= queries[i][1]) {
65+
addNum(nums[pos]);
66+
pos += 1;
67+
}
68+
cout << "yes";
69+
if (pos != 0) {
70+
ans[queries[i][2]] = search(queries[i][0]);
71+
}
72+
}
73+
74+
75+
return ans;
76+
}
77+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
class TrieNode {
3+
public:
4+
TrieNode* child[26];
5+
int num;
6+
TrieNode() {
7+
num = 0;
8+
for (auto& c : child) {
9+
c = nullptr;
10+
}
11+
}
12+
};
13+
14+
class Trie {
15+
TrieNode* root;
16+
public:
17+
Trie() {
18+
root = new TrieNode();
19+
}
20+
21+
void insert(string word) {
22+
TrieNode* node = root;
23+
for (char& c : word) {
24+
int i = c - 'a';
25+
if (!node->child[i]) {
26+
node->child[i] = new TrieNode();
27+
}
28+
node = node->child[i];
29+
node->num += 1;
30+
}
31+
}
32+
33+
int search(string word) {
34+
TrieNode* node = root;
35+
int sum = 0;
36+
for (char& c : word) {
37+
int i = c - 'a';
38+
if (!node->child[i]) {
39+
return -1;
40+
}
41+
node = node->child[i];
42+
sum += node->num;
43+
}
44+
return sum;
45+
}
46+
47+
48+
};
49+
50+
class Solution {
51+
public:
52+
vector<int> sumPrefixScores(vector<string>& words) {
53+
vector<int> ans(words.size(), 0);
54+
Trie t;
55+
56+
for (int i = 0; i < words.size(); i++) {
57+
t.insert(words[i]);
58+
}
59+
60+
for (int i = 0; i < words.size(); i++) {
61+
ans[i] = t.search(words[i]);
62+
}
63+
return ans;
64+
}
65+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
long long ans;
3+
public:
4+
void cal(int& k, priority_queue<int> v) { // 注意:這裡傳值而不是引用
5+
int n = 0;
6+
while (k) {
7+
// cout << v.top() << ' ';
8+
n = v.top();
9+
ans += n;
10+
v.pop();
11+
v.push(ceil(n / 3.0));
12+
k--;
13+
}
14+
// cout << '\n';
15+
}
16+
17+
long long maxKelements(vector<int>& nums, int k) {
18+
ans = 0;
19+
priority_queue<int> pq(nums.begin(), nums.end());
20+
cal(k, pq);
21+
22+
return ans;
23+
}
24+
};

0 commit comments

Comments
 (0)