美文网首页
Leetcode1440. 计算布尔表达式的值(中等)

Leetcode1440. 计算布尔表达式的值(中等)

作者: kaka22 | 来源:发表于2020-07-24 10:44 被阅读0次

题目
Table Variables:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| name          | varchar |
| value         | int     |
+---------------+---------+

name is the primary key for this table.
This table contains the stored variables and their values.

Table Expressions:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| left_operand  | varchar |
| operator      | enum    |
| right_operand | varchar |
+---------------+---------+

(left_operand, operator, right_operand) is the primary key for this table.
This table contains a boolean expression that should be evaluated.
operator is an enum that takes one of the values ('<', '>', '=')
The values of left_operand and right_operand are guaranteed to be in the Variables table.

Write an SQL query to evaluate the boolean expressions in Expressions table.

Return the result table in any order.

The query result format is in the following example.

Variables table:

+------+-------+
| name | value |
+------+-------+
| x    | 66    |
| y    | 77    |
+------+-------+

Expressions table:

+--------------+----------+---------------+
| left_operand | operator | right_operand |
+--------------+----------+---------------+
| x            | >        | y             |
| x            | <        | y             |
| x            | =        | y             |
| y            | >        | x             |
| y            | <        | x             |
| x            | =        | x             |
+--------------+----------+---------------+

Result table:

+--------------+----------+---------------+-------+
| left_operand | operator | right_operand | value |
+--------------+----------+---------------+-------+
| x            | >        | y             | false |
| x            | <        | y             | true  |
| x            | =        | y             | false |
| y            | >        | x             | true  |
| y            | <        | x             | false |
| x            | =        | x             | true  |
+--------------+----------+---------------+-------+

As shown, you need find the value of each boolean exprssion in the table using the variables table.

解答
连接两表 需要两次

select V.value as left_value, V1.value as right_value,
E.*
from  Expressions as E
join Variables as V
on E.left_operand = V.name
joinVariables as V1
on E.left_operand = V1.name

判断

select tmp.left_operand, tmp.operator, tmp.right_operand,
(Case when operator = '>' then
        (Case when  left_value >= right_value then 'True' else 'False' end)
    when operator = '<' then
        (Case when  left_value <= right_value then 'True' else 'False' end)
    else
        (case when  left_value = right_value then 'True' else 'False' end)
    End) as value
from (select V.value as left_value, V1.value as right_value,
E.*
from  Expressions as E
join Variables as V
on E.left_operand = V.name
joinVariables as V1
on E.left_operand = V1.name) as tmp

相关文章

  • Leetcode1440. 计算布尔表达式的值(中等)

    题目Table Variables: name is the primary key for this table...

  • 运算符

    运算符 逻辑运算符 与语法:布尔值(表达式) && 布尔值(表达式) 或语法:布尔值(表达式) || 布尔值(表达...

  • Groovy 布尔求值

      Groovy中的布尔求值与Java不同。根据上下文,Groovy会自动把表达式计算为布尔值。  如果在需要布尔...

  • Java--while循环结构

    语法结构:while(布尔表达式){  循环体;}  在循环刚开始时,会计算一次“布尔表达式”的值,若条件为真,执...

  • JavaScript的基本语法2

    条件语句 if条件语句 if结构先判断一个表达式的布尔值,然后根据布尔值的真伪,执行不同的语句。所谓布尔值,指的是...

  • JS笔记数据类型

    &&与的计算规则是,先判断符号前的布尔值,若是true取符号后的表达式值即为运算结果,若是false则直接取当前表...

  • java终极菜鸟总结3

    1.java条件运算符: ?: 语法形式:布尔表达式 ? 表达式1 :表达式2 运算过程:如果布尔表达式的值为tr...

  • 2-6 Go --- 基本数据类型 (字符串 string、布尔

    布尔值 bool: 布尔值只可以是 true 或 false。布尔值一般用来反映表达式的结果真还是假。真返回tru...

  • python_布尔值与空值

    布尔值 简单理解为布尔值用于判断一个表达式是否正确一个布尔值只有True、False两种值,要么是True(真),...

  • 条件运算符

    x?y:z(其中x为布尔类型表达式)表示先计算x的值,若为true,则整个运算的结果为y,否则则为z

网友评论

      本文标题:Leetcode1440. 计算布尔表达式的值(中等)

      本文链接:https://www.haomeiwen.com/subject/hntjlktx.html