首页 > 其他分享 >【LeetCode】1873. 计算特殊奖金

【LeetCode】1873. 计算特殊奖金

时间:2024-03-28 20:55:27浏览次数:37  
标签:LeetCode 奖金 1873 开头 employee REGEXP 雇员 id

题目

表: Employees
+-------------+---------+
| 列名        | 类型     |
+-------------+---------+
| employee_id | int     |
| name        | varchar |
| salary      | int     |
+-------------+---------+
employee_id 是这个表的主键(具有唯一值的列)。
此表的每一行给出了雇员id ,名字和薪水。

编写解决方案,计算每个雇员的奖金。
如果一个雇员的 id 是 奇数 并且他的名字不是以 'M' 开头,
那么他的奖金是他工资的 100% ,否则奖金为 0 。
返回的结果按照 employee_id 排序。
返回结果格式如下面的例子所示。

示例 1:
输入:
Employees 表:
+-------------+---------+--------+
| employee_id | name    | salary |
+-------------+---------+--------+
| 2           | Meir    | 3000   |
| 3           | Michael | 3800   |
| 7           | Addilyn | 7400   |
| 8           | Juan    | 6100   |
| 9           | Kannon  | 7700   |
+-------------+---------+--------+
输出:
+-------------+-------+
| employee_id | bonus |
+-------------+-------+
| 2           | 0     |
| 3           | 0     |
| 7           | 7400  |
| 8           | 0     |
| 9           | 7700  |
+-------------+-------+
解释:
因为雇员id是偶数,所以雇员id 是2和8的两个雇员得到的奖金是0。
雇员id为3的因为他的名字以'M'开头,所以,奖金是0。
其他的雇员得到了百分之百的奖金。

不以M开头的字符串,用正则表达式
REGEXP 是 SQL 中的一个关键字组合,用于匹配符合某个正则表达式规则的数据。
先找出以M开头的字符串,REGEXP 'M',''表示字符串的开始,则'^M'表示以M开头的字符串。
那么,不以M开头的字符串可以写为NOT REGEXP '^M'
SQL的IF语句:
IF(condition, true_value, false_value),如果condition成立,则取左边的值,否则取右边的值

SELECT 
employee_id, IF(employee_id % 2 != 1 AND name NOT REGEXP '^M', salary, 0) as bonus
FROM Employees
ORDER BY employee_id

标签:LeetCode,奖金,1873,开头,employee,REGEXP,雇员,id
From: https://www.cnblogs.com/basilicata/p/18102595

相关文章

  • 《leetcode hot100》142. 环形链表 II
    Accode:publicclassSolution{publicListNodedetectCycle(ListNodehead){ListNodeslow=head,fast=head;while(true){if(fast==null||fast.next==null)returnnull;slow=slow.next;......
  • Leetcode 【930. 和相同的二元子数组】【统计「优美子数组」】【974. 和可被 K 整除的
    这道题目是经典的求子数组之和=goal的个数,用map维护。但是笔者在实现的过程中发现0的情况不是很好出来,问题在于mp[sum]和sum+=num的代码语句存在位置问题。后来看了下代码还是自己没有考虑清楚。这种类型的题目就是要想清楚你的做法,以及边界条件。classSolution{public:......
  • 算法打卡day28|贪心算法篇02|Leetcode 122.买卖股票的最佳时机 II、55. 跳跃游戏、45.
    算法题Leetcode122.买卖股票的最佳时机II题目链接:122.买卖股票的最佳时机II 大佬视频讲解:买卖股票的最佳时机II视频讲解 个人思路因为只有一只股票,且两天作一个交易单元,那每次只收集正利润就可以最终最多可以获取的利润,可以用贪心。解法贪心法从下图可以发......
  • LeetCode-1 Two Sum
    Givenanarrayofintegers nums andaninteger target,return indicesofthetwonumberssuchthattheyaddupto target.Youmayassumethateachinputwouldhave exactly onesolution,andyoumaynotusethe same elementtwice.Youcanreturnthea......
  • LeetCode-9 Palindrome Number
    9.PalindromeNumber easyGivenaninteger x,return true if x isa palindrome ,and false otherwise. Example1:Input:x=121Output:trueExplanation:121readsas121fromlefttorightandfromrighttoleft.Example2:Input:x=-......
  • LeetCode-21 Merge Two Sorted Lists
    21.MergeTwoSortedLists EasyYouaregiventheheadsoftwosortedlinkedlists list1 and list2.Mergethetwolistsintoone sorted list.Thelistshouldbemadebysplicingtogetherthenodesofthefirsttwolists.Return theheadofthemerg......
  • 【力扣】36. 有效的数独 - 力扣(LeetCode)
    这里主要是记录一些我做这个题遇到的问题及解决办法目录1.问题描述1.1思路分析:2.程序代码及注释:1.问题描述1.1思路分析:  对于个题目而言,我只使用了一个参数,其中我的主要思路就是暴力循环求解,总体思路就是先判断每行是否符合要求,在判断每列是否符合要求、然后......
  • Leetcode 回文链表
    Day12第一题用数组存储链表的数值,在检测是否是回文数组,数组长度不可变,所以用listclassSolution{publicbooleanisPalindrome(ListNodehead){List<Integer>list=newArrayList<>();while(head!=null){list.add(head.val);......
  • leetcode-75
    给定一个包含红色、白色和蓝色、共n个元素的数组nums,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。我们使用整数0、1和2分别表示红色、白色和蓝色。必须在不使用库内置的sort函数的情况下解决这个问题。示例1:输入:nums=[2,0,2,1,1,......
  • LeetCodeHot100 链表 160. 相交链表 206. 反转链表 234. 回文链表 141. 环形链表
    160.相交链表https://leetcode.cn/problems/intersection-of-two-linked-lists/description/?envType=study-plan-v2&envId=top-100-likedpublicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB){intlenA=0;intlenB=0;L......