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 <= 50001 <= queries.length <= 50001 <= wordlist[i].length <= 71 <= queries[i].length <= 7- All strings in
wordlistandqueriesconsist 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 ...
随机推荐
- JavaScript笔记——正则表达式
正则表达式(regular expression)是一个描述字符模式的对象.JavaScript的 RegExp 类 表示正则表达式,而 String 和 RegExp 都定义了使用正则表达式进行强大 ...
- nios pio interrupt 的使能
关于nios 中的中断,因为要16c550中需要nios的中断环境去测试,所以就用到了中断. 硬件:在nios中添加硬件PIO,但是要使能中断功能.如下图所示: 系统列化,PIO的连接就不说了.但是要 ...
- Python 面向对象的进阶
类的成员 类的成员可以分为三大类 : 字段 , 方法 和 属性 注 : 所有的成员中,只有普通字段的内容保存对象中, 即 : 根据此类创建了对象,在内存就有多少个普通字段. 而其他的成员,则 ...
- struts2中s:iterator的使用(2个list嵌套循环)
<s:iterator value="packagePlateTbls" id="plateTbls"> <tr> <td cla ...
- C++风格与C风格文件读写效率测试-vs2015,vs2017
void test_write() { ; const char* c_plus_write_file = "H://c_plus_write_file.txt"; const c ...
- 数据仓库-数据采集-ETL漫谈
数据仓库之ETL漫谈ETL,Extraction-Transformation-Loading的缩写,中文名称为数据抽取.转换和加载.大多数据仓库的数据架构可以概括为:数据源-->ODS(操作型 ...
- HDR
[HDR] 什么是 HDR? 高动态范围拍摄(HDR)现在已经得到广泛使用,被用来补偿大多数数码成像传感器有限的动态范围.照片的动态范围是指最暗的色彩与最亮的色彩之间的亮度范围——也可以一并表示色调范 ...
- js点击按钮获取验证码倒计时
//发送验证码倒计时 var clock = ''; var nums = 60; var btn; $("#btnGetVerCode").click(function () { ...
- 使用Nuget发布自己的类库包
NuGet是一个为大家所熟知的Visual Studio扩展,通过这个扩展,开发人员可以非常方便地在Visual Studio中安装或更新项目中所需要的第三方组件,同时也可以通过NuGet来安装一些V ...
- Linux pkg-config命令
一.简介 pkg-config用来检索系统中安装库文件的信息.典型的是用作库的编译和连接. 二.实例 http://blog.chinaunix.net/uid-20595934-id-1918368 ...