ARTS-Week5 括号校验、Stack Overflow开发者调查、SQL

Algorithm


LeetCode原题链接: 有效的括号

给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
    注意空字符串可被认为是有效字符串。

示例 1:
输入: "()"
输出: true


示例 2:
输入: "()[]{}"
输出: true


示例 3:
输入: "(]"
输出: false


示例 4:
输入: "([)]"
输出: false


示例 5:
输入: "{[]}"
输出: true

我的解法:

public boolean isValid(String s) {
        if(s.length() == 0){
            return true;
        }else if(s.length() % 2 == 1){
            return false;
        }

        Map parentthesesMap = new HashMap<>(3);
        parentthesesMap.put(')','(');
        parentthesesMap.put(']','[');
        parentthesesMap.put('}','{');

        char[] chars = s.toCharArray();
        Stack leftParenttheses = new Stack<>();
        for (char parenttheses : chars) {
            switch (parenttheses){
                case '(':
                case '[':
                case '{':
                    leftParenttheses.push(parenttheses);
                    break;
                case ')':
                case ']':
                case '}':
                    if( !leftParenttheses.isEmpty() && leftParenttheses.peek().equals(parentthesesMap.get(parenttheses)) ){
                        leftParenttheses.pop();
                    }else {
                        return false;
                    }
                    break;
                default:
                    break;
            }
        }

        if(leftParenttheses.isEmpty()){
            return true;
        }else {
            return false;
        }
    }

Review


Summary of the Stack Overflow Developer Survey 2019

  • Javascript, MySQL 和 Linux 在各自的技术分类中仍然最受欢迎。
  • JavaScript从2011年以来就一直是最受欢迎的编程语言,python是增长趋势最快的。
  • 四大云供应商,AWS居首位。(AWS,Google Cloud Platform, Azure, IBM)
  • 今年首次调查了容器技术,Docker and Kubernetes这两种新技术将会更受欢迎。linux仍是最受欢迎的平台,windows紧随其后。
  • MySQL和PostgresSQL继续领先,MongoDB在NoSQL数据中最受欢迎。

附: StackOverflow 开发者调查结果2019

Tip


SQL常规优化

  • 避免隐含的类型转换
select id from employee where emp_id='8'  (错)

select id from employee where emp_id=8    (对)

emp_id是整数型,用'8'会默认启动类型转换,增加查询的开销。

  • 使用关键字代替函数
select id from employee where UPPER(dept) like 'TECH_DB'  (错)

select id from employee where SUBSTR(dept,1,4)='TECH'    (错)

select id from employee where dept like 'TECH%'         (对)
  • 不要在字段上用转换函数,尽量在常量上用
select id from employee where to_char(create_date,'yyyy-mm-dd') = '2012-10-31'  (错)

select id from employee where create_date = to_date('2012-10-31','yyyy-mm-dd')   (对)
  • in 与 exists 的使用
select * from A where id in(select id from B)

select a.* from A a where exists(select 1 from B b where a.id=b.id)

确定给定的值是否与子查询或列表中的值相匹配

  • in在查询的时候,首先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选。所以相对内表(B)比较小的时候,in的速度较快。
  • 遍历循环外表,然后看外表中的记录有没有和内表的数据一样的。匹配上就将结果放入结果集中。exists()适合B表比A表数据大的情况
    A表数据量大时用in,B表数据量大时用exists
  • 使用not exists 而非not in
  • 在经常进行连接,但是没有指定为外键的列上建立索引
  • 在频繁进行排序会分组的列上建立索引,如经常做group by 或 order by 操作的字段
  • 在条件表达式中经常用到的不同值较多的列上建立检索,在不同值少的列上不建立索引

Share


分享一篇来自关于mybatis中常用sql相关的文章
关于Mybatis中SQL语句的整理

你可能感兴趣的:(ARTS-Week5 括号校验、Stack Overflow开发者调查、SQL)