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 ...
随机推荐
- Java如何解决form表单上传文件,以及页面返回处理结果通知!
前端JSP代码 <form id='formSumbit' class='form-horizontal' action='/ncpay/route/chlsubmcht/batchImpor' ...
- wordpress 基础文件
需要用到的PHP基础文件有: 404.php 404模板 rtl.css 如果网站的阅读方向是自右向左的,会被自动包含进来 comments.php 评论模板 single.php 文章模板.显 ...
- Apache Derby数据库系统使用方法
Apache Derby数据库系统使用方法 最近由于项目要求,试用了一下Apache Derby数据库,这里对了解到的内容做一个记录. Apache Derby是一个开源的关系型数据库管理系统,用Ja ...
- Java虚拟机(一):JVM的运行机制
一.JVM启动流程 通过java +xxx(或javaw)启动java虚拟机 装载配置,会在当前路径中寻找jvm的config配置文件. 根据查找jvm.dll文件.这个文件就是java虚拟机的主要实 ...
- Entitlements
[Entitlements] Entitlements confer specific capabilities or security permissions to your iOS or OS X ...
- SpringMVC总结三:请求Controller返回视图类型以及请求方式、参数介绍
视图解析,请求Controller返回的视图类型: @Controller @RequestMapping("/test") public class TestController ...
- Ant工具 ant的安装与配置 ant作用
原文出自:http://blog.csdn.net/zhuche110/article/details/2663904点击打开链接 Ant是一种基于Java的build工具.理论上来说,它有些类似于( ...
- Eclipse右击jsp没有运行选项
maven项目低级错误,没有更新maven资源库.....更新后就运行起来了
- c# 如何制作RealPlayer 视频播放器
c# 如何制作RealPlayer 视频播放器 主要介绍了如何使用 RealPlayer G2 Control 控件 那么我们怎么获得到这个控件呢,很简单,操作方法如下 右单击工具箱对话框的[所有 ...
- css常见问题解决方法
设置方法: div内的img和span都需要设置vertical-align:middle; 解决inline-block的空格: http://www.w3cplus.com/css/fightin ...