From bf3fadba8e89f6cc9aa0039247656dc0a5699b4c Mon Sep 17 00:00:00 2001 From: youyou Date: Wed, 10 Aug 2022 23:10:06 +0800 Subject: [PATCH] =?UTF-8?q?83.=E5=88=A0=E9=99=A4=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E9=87=8D=E5=A4=8D=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E3=80=81108.=E5=B0=86=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E3=80=81181.=E8=B6=85=E8=BF=87=E7=BB=8F?= =?UTF-8?q?=E7=90=86=E6=94=B6=E5=85=A5=E7=9A=84=E5=91=98=E5=B7=A5=E3=80=81?= =?UTF-8?q?182.=E6=9F=A5=E6=89=BE=E9=87=8D=E5=A4=8D=E7=9A=84=E7=94=B5?= =?UTF-8?q?=E5=AD=90=E9=82=AE=E7=AE=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/src/implements/mod.rs | 2 + rust/src/implements/q0083.rs | 65 ++++++++++++++++++++ rust/src/implements/q0108.rs | 54 +++++++++++++++++ sql/q0181.md | 114 +++++++++++++++++++++++++++++++++++ sql/q0182.md | 74 +++++++++++++++++++++++ 5 files changed, 309 insertions(+) create mode 100644 rust/src/implements/q0083.rs create mode 100644 rust/src/implements/q0108.rs create mode 100644 sql/q0181.md create mode 100644 sql/q0182.md diff --git a/rust/src/implements/mod.rs b/rust/src/implements/mod.rs index 2d4c499..b5a89b1 100644 --- a/rust/src/implements/mod.rs +++ b/rust/src/implements/mod.rs @@ -39,6 +39,7 @@ mod q0070; mod q0078; mod q0080; mod q0081; +mod q0083; mod q0086; mod q0087; mod q0088; @@ -49,6 +50,7 @@ mod q0100; mod q0101; mod q0102; mod q0104; +mod q0108; mod q0112; mod q0110; mod q0111; diff --git a/rust/src/implements/q0083.rs b/rust/src/implements/q0083.rs new file mode 100644 index 0000000..58c17be --- /dev/null +++ b/rust/src/implements/q0083.rs @@ -0,0 +1,65 @@ +use crate::Solution; +use crate::structure::ListNode; + +impl Solution { + /// [83.删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/) + /// + /// 2022-08-10 21:10:32 + /// + /// 给定一个已排序的链表的头 `head` , _删除所有重复的元素,使每个元素只出现一次_ 。返回 _已排序的链表_ 。 + /// + /// + **示例 1:** + /// + ![](https://assets.leetcode.com/uploads/2021/01/04/list1.jpg)``` + /// + **输入:**head = \[1,1,2\] + /// + **输出:**\[1,2\] + /// + **示例 2:** + /// + ![](https://assets.leetcode.com/uploads/2021/01/04/list2.jpg)``` + /// + **输入:**head = \[1,1,2,3,3\] + /// + **输出:**\[1,2,3\] + /// + **提示:** + /// * 链表中节点数目在范围 `[0, 300]` 内 + /// * `-100 <= Node.val <= 100` + /// * 题目数据保证链表已经按升序 **排列** + /// + Related Topics + /// * 链表 + pub fn delete_duplicates(mut head: Option>) -> Option> { + /*let mut vec = Vec::::new(); + while let Some(n) = head { + if Some(&n.val) != vec.last() { + vec.push(n.val); + } + head = n.next; + } + let mut node = None; + for val in vec.into_iter().rev() { + node = Some(Box::new(ListNode { val, next: node })); + } + node*/ + if head.is_none() { + return None + } + let mut node = head.as_mut().unwrap(); + let mut pre = node.val; + while let Some(mut next) = node.next.take() { + if pre == next.val { //去掉当前节点 + node.next = next.next; + } else { // 还原 + pre = next.val; + node.next = Some(next); + node = node.next.as_mut().unwrap(); + } + } + head + } +} + +#[cfg(test)] +mod test { + use crate::Solution; + use crate::structure::ListNode; + + #[test] + fn test_q0083() { + assert_eq!(Solution::delete_duplicates(Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: None })) })) }))), Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: None })) }))); + } +} diff --git a/rust/src/implements/q0108.rs b/rust/src/implements/q0108.rs new file mode 100644 index 0000000..40b2d7b --- /dev/null +++ b/rust/src/implements/q0108.rs @@ -0,0 +1,54 @@ +use crate::Solution; +use crate::structure::TreeNode; + +use std::rc::Rc; +use std::cell::RefCell; + +impl Solution { + /// [108.将有序数组转换为二叉搜索树](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/) + /// + /// 2022-08-10 21:43:44 + /// + /// 给你一个整数数组 `nums` ,其中元素已经按 **升序** 排列,请你将其转换为一棵 **高度平衡** 二叉搜索树。 + /// + /// **高度平衡** 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。 + /// + /// + **示例 1:** + /// + ![](https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg) + /// + **输入:** nums = \[-10,-3,0,5,9\] + /// + **输出:** \[0,-3,9,-10,null,5\] + /// + **解释:** \[0,-10,5,null,-3,null,9\] 也将被视为正确答案: + /// + ![](https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg) + /// + **示例 2:** + /// + ![](https://assets.leetcode.com/uploads/2021/02/18/btree.jpg) + /// + **输入:** nums = \[1,3\] + /// + **输出:** \[3,1\] + /// + **解释:** \[1,null,3\] 和 \[3,1\] 都是高度平衡二叉搜索树。 + /// + **提示:** + /// * `1 <= nums.length <= 104` + /// * `-104 <= nums[i] <= 104` + /// * `nums` 按 **严格递增** 顺序排列 + /// + Related Topics + /// * 树 + /// * 二叉搜索树 + /// * 数组 + /// * 分治 + /// * 二叉树 + pub fn sorted_array_to_bst(nums: Vec) -> Option>> { + match nums.len() { + 0 => None, + l => Some(Rc::new(RefCell::new(TreeNode { val: nums[l / 2], left: Self::sorted_array_to_bst(nums[..l / 2].to_vec()), right: Self::sorted_array_to_bst(nums[l / 2 + 1..].to_vec()) }))) + } + } +} + +#[cfg(test)] +mod test { + use crate::Solution; + + #[test] + fn test_q0108() { + println!("{:?}", Solution::sorted_array_to_bst(vec![1, 3])); + println!("{:?}", Solution::sorted_array_to_bst(vec![-10, -3, 0, 5, 9])); + } +} diff --git a/sql/q0181.md b/sql/q0181.md new file mode 100644 index 0000000..2d4fa6f --- /dev/null +++ b/sql/q0181.md @@ -0,0 +1,114 @@ +# [181.超过经理收入的员工](https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/) + +2022-08-10 22:05:28 + +## SQL架构 + +```mysql +Create table If Not Exists Employee +( + id int, + name varchar(255), + salary int, + managerId int +); +Truncate table Employee; + +insert into Employee (id, name, salary, managerId) +values ('1', 'Joe', '70000', '3'); + +insert into Employee (id, name, salary, managerId) +values ('2', 'Henry', '80000', '4'); + +insert into Employee (id, name, salary, managerId) +values ('3', 'Sam', '60000', 'None'); + +insert into Employee (id, name, salary, managerId) +values ('4', 'Max', '90000', 'None'); +``` + +## 题 + +表:`Employee` + +``` ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| id | int | +| name | varchar | +| salary | int | +| managerId | int | ++-------------+---------+ +Id是该表的主键。 +该表的每一行都表示雇员的ID、姓名、工资和经理的ID。 + +``` + +编写一个SQL查询来查找收入比经理高的员工。 + +以 **任意顺序** 返回结果表。 + +查询结果格式如下所示。 + +**示例 1:** + +``` +**输入:** +Employee 表: ++----+-------+--------+-----------+ +| id | name | salary | managerId | ++----+-------+--------+-----------+ +| 1 | Joe | 70000 | 3 | +| 2 | Henry | 80000 | 4 | +| 3 | Sam | 60000 | Null | +| 4 | Max | 90000 | Null | ++----+-------+--------+-----------+ +**输出:** ++----------+ +| Employee | ++----------+ +| Joe | ++----------+ +**解释:** Joe 是唯一挣得比经理多的雇员。 +``` + +Related Topics + +* 数据库 + +## 解 + +```mysql +SELECT a.Name AS 'Employee' +FROM Employee AS a, + Employee AS b +WHERE a.ManagerId = b.Id + AND a.Salary > b.Salary; +``` + +```mysql +SELECT a.Name AS 'Employee' +FROM Employee a, + Employee b +WHERE a.ManagerId = b.Id + AND a.Salary > b.Salary; +``` + +```mysql +SELECT a.NAME AS Employee +FROM Employee AS a + JOIN Employee AS b + ON a.ManagerId = b.Id + AND a.Salary > b.Salary; +``` + +```mysql +SELECT a.NAME AS Employee +FROM Employee a + JOIN Employee b + ON a.ManagerId = b.Id + AND a.Salary > b.Salary; +``` + +不加`AS`的快一点好像。 \ No newline at end of file diff --git a/sql/q0182.md b/sql/q0182.md new file mode 100644 index 0000000..cb9d005 --- /dev/null +++ b/sql/q0182.md @@ -0,0 +1,74 @@ +# [182.查找重复的电子邮箱](https://leetcode-cn.com/problems/duplicate-emails/) + +2022-08-10 22:24:58 + +## SQL架构 + +```mysql +Create table If Not Exists Person +( + id int, + email varchar(255) +); +Truncate table Person; + +insert into Person (id, email) +values ('1', 'a@b.com'); + +insert into Person (id, email) +values ('2', 'c@d.com'); + +insert into Person (id, email) +values ('3', 'a@b.com'); +``` + +## 题目 + +编写一个 SQL 查询,查找 `Person` 表中所有重复的电子邮箱。 + +**示例:** + +``` ++----+---------+ +| Id | Email | ++----+---------+ +| 1 | a@b.com | +| 2 | c@d.com | +| 3 | a@b.com | ++----+---------+ + +``` + +根据以上输入,你的查询应返回以下结果: + +``` ++---------+ +| Email | ++---------+ +| a@b.com | ++---------+ + +``` + +**说明:**所有电子邮箱都是小写字母。 + +Related Topics + +* 数据库 + +## 解 + +```mysql +select Email +from (select Email, count(Email) as num + from Person + group by Email) as statistic +where num > 1; +``` + +```mysql +select Email +from Person +group by Email +having count(Email) > 1; +``` \ No newline at end of file