Weekly Contest 117
965. Univalued Binary Tree
A binary tree is univalued if every node in the tree has the same value.
Return true
if and only if the given tree is univalued.
Example 1:
Input: [1,1,1,1,1,null,1]
Output: true
Example 2:
Input: [2,2,2,5,2]
Output: false
Note:
- The number of nodes in the given tree will be in the range
[1, 100]
. - Each node's value will be an integer in the range
[0, 99]
.
Code:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isUnivalTree(TreeNode* root) {
solve(root, root->val);
return ans;
} void solve(TreeNode* root, int num) {
if (root->val != num) ans = false;
if (root->left != NULL) solve(root->left, num);
if (root->right != NULL) solve(root->right, num);
} private:
bool ans = true;
};
967. Numbers With Same Consecutive Differences
Return all non-negative integers of length N
such that the absolute difference between every two consecutive digits is K
.
Note that every number in the answer must not have leading zeros except for the number 0
itself. For example, 01
has one leading zero and is invalid, but 0
is valid.
You may return the answer in any order.
Example 1:
Input: N = 3, K = 7
Output: [181,292,707,818,929]
Explanation: Note that 070 is not a valid number, because it has leading zeroes.
Example 2:
Input: N = 2, K = 1
Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]
Code:
class Solution {
public:
vector<int> numsSameConsecDiff(int N, int K) {
vector<int> ans;
if (N == 1) {
for (int i = 0; i < 10; ++i)
ans.push_back(i);
return ans;
}
for (int i = 1; i < 10; ++i) {
string s = to_string(i);
dfs(s, N, K, ans);
}
return ans;
} void dfs(string s, const int N, const int K, vector<int>& ans) {
if (s.length() == N) {
ans.push_back(stoi(s));
return ;
}
int lastNum = s[s.length()-1] - '0';
int temp = lastNum + K;
string dummy = s;
if (temp >= 0 && temp < 10) {
dummy += to_string(temp);
dfs(dummy, N, K, ans);
}
if (K != 0) {
int temp = lastNum - K;
if (temp >= 0 && temp < 10) {
s += to_string(temp);
dfs(s, N, K, ans);
}
}
}
};
966. Vowel Spellchecker
Given a wordlist
, we want to implement a spellchecker that converts a query word into a correct word.
For a given query
word, the spell checker handles two categories of spelling mistakes:
- Capitalization: If the query matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the case in the wordlist.
- Example:
wordlist = ["yellow"]
,query = "YellOw"
:correct = "yellow"
- Example:
wordlist = ["Yellow"]
,query = "yellow"
:correct = "Yellow"
- Example:
wordlist = ["yellow"]
,query = "yellow"
:correct = "yellow"
- Example:
- Vowel Errors: If after replacing the vowels ('a', 'e', 'i', 'o', 'u') of the query word with any vowel individually, it matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the match in the wordlist.
- Example:
wordlist = ["YellOw"]
,query = "yollow"
:correct = "YellOw"
- Example:
wordlist = ["YellOw"]
,query = "yeellow"
:correct = ""
(no match) - Example:
wordlist = ["YellOw"]
,query = "yllw"
:correct = ""
(no match)
- Example:
In addition, the spell checker operates under the following precedence rules:
- When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.
- When the query matches a word up to capitlization, you should return the first such match in the wordlist.
- When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
- If the query has no matches in the wordlist, you should return the empty string.
Given some queries
, return a list of words answer
, where answer[i]
is the correct word for query = queries[i]
.
Example 1:
Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
Output: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
Note:
1 <= wordlist.length <= 5000
1 <= queries.length <= 5000
1 <= wordlist[i].length <= 7
1 <= queries[i].length <= 7
- All strings in
wordlist
andqueries
consist only of english letters.
Code:
class Solution {
public:
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
unordered_map<string, vector<string>> seen, tran;
unordered_set<string> matches;
for (int i = 0; i < wordlist.size(); ++i) {
string temp_tolower = _tolower(wordlist[i]);
string temp_todev = _todev(wordlist[i]);
seen[temp_tolower].push_back(wordlist[i]);
tran[temp_todev].push_back(wordlist[i]);
matches.insert(wordlist[i]);
}
vector<string> ans;
for (int i = 0; i < queries.size(); ++i) {
// match
if (matches.count(queries[i])) {
ans.push_back(queries[i]);
continue;
} string temp = _tolower(queries[i]); // capitalization
if (seen.count(temp)) {
ans.push_back(seen[temp][0]);
continue;
} // vowel errors
string ant = _todev(queries[i]);
if (tran.count(ant)) {
ans.push_back(tran[ant][0]);
} else {
ans.push_back("");
}
} return ans;
} string _tolower(string s) {
for (auto& c : s)
c = tolower(c); return s;
} string _todev(string s) {
s = _tolower(s);
for (auto& c : s)
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
c = '#';
return s;
}
};
In this question I reference the function of _todev with @lee215.
968. Binary Tree Cameras
Given a binary tree, we install cameras on the nodes of the tree.
Each camera at a node can monitor its parent, itself, and its immediate children.
Calculate the minimum number of cameras needed to monitor all nodes of the tree.
Example 1:
Input: [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.
Example 2:
Input: [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
Note:
- The number of nodes in the given tree will be in the range
[1, 1000]
. - Every node has value 0.
Code:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minCameraCover(TreeNode* root) {
int state = dfs(root);
return res + (state < 1 ? 1 : 0);
} int dfs(TreeNode* root) {
int needCamera = 0;
int covered = 0; if (root->left == NULL && root->right == NULL)
return 0; if (root->left != NULL) {
int state = dfs(root->left);
if (state == 0) {
needCamera = 1;
covered = 1;
} else if (state == 1) {
covered = 1;
}
} if (root->right != NULL) {
int state = dfs(root->right);
if (state == 0) {
needCamera = 1;
covered = 1;
} else if (state == 1) {
covered = 1;
}
} if (needCamera > 0) {
res++;
return 1;
} if (covered > 0) {
return 2;
} return 0;
} private:
int res = 0;
};
Analysis:
https://leetcode.com/problems/binary-tree-cameras/discuss/211180/JavaC%2B%2BPython-Greedy-DFS
Weekly Contest 117的更多相关文章
- LeetCode Weekly Contest 117
已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode. 这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~). 第一题: 96 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
随机推荐
- 第14 章 Spring MVC的工作机制与设计模式
14.1 Spring MVC的总体设计 要使用SPring MVC,只要在web.xml中配置一个DispatcherServlet. 再定义一个dispatcherServlet-servlet. ...
- U-boot分析与移植(1)----bootloader分析
一.Boot Loader 概念 就是在操作系统内核运行之前运行的一段小程序.通过这段小程序,我们可以初始化硬件设备.建立内存空间的映射图,从而将系统的软硬件环境带到一个合适的状态,以便为最终调用操作 ...
- python学习笔记(三):文件操作和集合
对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 文件基本操作: f = open('file.txt','r') #以只读方式打开一个 ...
- MySQL简述
Mysql是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一. ...
- VMWARE三种网络配置
由于linux目前很热门,越来越多的人在学习linux,但是买一台服务放家里来学习,实在是很浪费.那么如何解决这个问题?虚拟机软件是很好的选择,常用的虚拟机软件有vmware workstations ...
- Mycat实战之连续分片
1 按照日期(天)分片: 从开始日期算起,按照天数来分片 例如,从2017-11-01,每10天一个分片且可以指定结束日期 注意事项:需要提前将分片规划好,建好,否则有可能日期超出实际配置分片数 1. ...
- linux中ftp配置文件详解
vsftpd配置文件采用"#"作为注释符,以"#"开头的行和空白行在解析时将被忽略,其余的行被视为配置命令行,每个配置命令的"="两边不要留 ...
- Android Architecture Components
https://developer.android.com/topic/libraries/architecture/index.html ViewModel 有LiveData Activity 监 ...
- 【总结整理】如何判断伪需求(摘自pmcafe)
1.客户不会直接提需求,都是给解决方案,所以得到用户的反馈之后,先反推一下是很必要的,为什么客户会有这样的方案 总结:方案不合适 例如:客户只会说我要快马,反推一下,其实客户是想要更快,这样的话,解决 ...
- c语言函数是怎么传递参数的
其实就是把变量或常量复制了一份给函数中的变量,简单说来就是复制的过程. 有一个很经典的问题:用函数交换两个变量的值. int a=1; int b=2; swap(a,b) 有一个函数是这样实现的 v ...