zoukankan      html  css  js  c++  java
  • 2018-10-10-weekly

    Algorithm

    字典序排数

    • What 给定一个整数n,返回从1到n的字典顺序,例如,给定 n =13,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] ,尽可能的优化算法的时间复杂度和空间复杂度。

    • How 问题的关键在于理清“词典顺序”是什么样的,词典顺序就是10比2靠前,101比11靠前,110比11靠后。做法大致顺序:

    • 1.如果一个数乘以十以后没有超过n,那它后面紧挨着的应该是它的十倍,比如1,10,100。
    • 2.如果不满足1,那就应该是直接加一,比如n为13的时候,前一个数为12,120超过了n,那接着的应该是13。但是这里要注意如果前一个数的个位已经是9或者是它就是n了,那就不能加一了,比如 n = 25,前一个数为19,下一个数应该为2而不是19+1=20。25的下一个也没有26了。
    • 3.如果不满足2,比如19后面应该接2而不是20,这时候应该将19除以10再加一,比如n=500,399的下一个应该是4,那就是除以十,个位还是9,继续除以10,得到3,加一得到4。
    • Key Codes
    class Solution {
        public List<Integer> lexicalOrder(int n) {
            List<Integer> list = new ArrayList<>(n);
            int res = 1;
            for (int i = 1; i <= n; i++) {
                list.add(res);
                if (res * 10 <= n) {
                    res *= 10;
                } else if (res % 10 != 9 && res + 1 <= n) {
                    res++;
                }else {
                    while ((res / 10) % 10 == 9) {
                        res /= 10;
                    }
                    res = res / 10 + 1;
                }
            }
            return list;
        }
    }
    

    Review

    如何掌控你的自由时间?

    • What 每个不起眼的时刻都潜力无限。你可以用零星的时间,来获得零星的快乐。那么,如何合理的利用自己的时间吗?怎么样才能高效控制好时间?
    • How 先建立我们想要的生活,时间就会自然而然节省出来,时间是有弹性的。我们不能创造更多时间,但是时间会自己调整去适应我们选择去做的事情。

    Tip

    • What 常用SQL语句优化技巧总结
    • How
    • 1.通过变量的方式来设置参数
      • good:stringsql = "select * from orders where customer_name not exist (select customer_name from customer)";
      • bad:stringsql = "select * from orders where customer_name not in(select customer_name from customer)";
      • why:如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。
    • *2.不要使用(select )
      • good:stringsql = "select people_name,pepole_age from people ";
      • bad:stringsql = "select * from people ";
      • why:使用select *的话会增加解析的时间,另外会把不需要的数据也给查询出来,数据传输也是耗费时间的,比如text类型的字段通常用来保存一些内容比较繁杂的东西,如果使用select *则会把该字段也查询出来。
    • 3.谨慎使用模糊查询
      • good:stringsql = "select * from people p where p.id like 'parm1%' ";
      • bad:stringsql = "select * from people p where p.id like '%parm1%' ";
      • why:当模糊匹配以%开头时,该列索引将失效,若不以%开头,该列索引有效。
    • 4.不要使用列号
      • good:stringsql = "select people_name,pepole_age from people order by name,age";
      • bad:stringsql = "select people_name,pepole_age from people order by 6,8";
      • why:使用列号的话,将会增加不必要的解析时间。
    • 5.优先使用UNION ALL,避免使用UNION
      • good:stringsql = "select name from student union all select name from teacher";
      • bad:stringsql = "select name from student union select name from teacher";
      • why:UNION 因为会将各查询子集的记录做比较,故比起UNION ALL ,通常速度都会慢上许多。一般来说,如果使用UNION ALL能满足要求的话,务必使用UNION ALL。还有一种情况,如果业务上能够确保不会出现重复记录。
    • 6.在where语句或者order by语句中避免对索引字段进行计算操作
      • good:stringsql = "select people_name,pepole_age from people where create_date=date1 ";
      • bad:stringsql = "select people_name,pepole_age from people where trunc(create_date)=date1";
      • why:当在索引列上进行操作之后,索引将会失效。正确做法应该是将值计算好再传入进来。
    • 7.使用not exist代替not in
      • good:stringsql = "select * from orders where customer_name not exist (select customer_name from customer)";
      • bad:stringsql = "select * from orders where customer_name not in(select customer_name from customer)";
      • why:如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。
    • 8.exist和in的区别
      • in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。因此,in用到的是外表的索引, exists用到的是内表的索引。如果查询的两个表大小相当,那么用in和exists差别不大。如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:
      • 例如:表A(小表),表B(大表):
        *效率低,用到了A表上cc列的索引:select * from A where exists(select cc from B where cc=A.cc)
        *效率高,用到了B表上cc列的索引:select * from B where exists(select cc from A where cc=B.cc)
    • 9.避免在索引列上做如下操作:(当在索引列上使用如下操作时,索引将会失效,造成全表扫描。)
      • 避免在索引字段上使用<>,!=
      • 避免在索引列上使用IS NULL和IS NOT NULL
      • 避免在索引列上出现数据类型转换(比如某字段是String类型,参数传入时是int类型)
    • 10.复杂操作可以考虑适当拆成几步
      • 有时候会有通过一个SQL语句来实现复杂业务的例子出现,为了实现复杂的业务,嵌套多级子查询。造成SQL性能问题。对于这种情况可以考虑拆分SQL,通过多个SQL语句实现,或者把部分程序能完成的工作交给程序完成。

    Share

    神经网络最大的优点,以及最严重的缺陷

  • 相关阅读:
    FZU OJ 1056 :扫雷游戏
    HPU 1166: 阶乘问题(一)
    常用的一些模板
    PAT天梯:L1-019. 谁先倒
    HPU 1437: 王小二的求值问题
    《编程珠玑》阅读小记(7) — 代码调优与节省空间
    《编程珠玑》阅读小记(6) — 算法设计技术
    《编程珠玑》阅读小记(5) — 编程小事
    《编程珠玑》阅读小记(4) — 编写正确的程序
    《C/C++专项练习》— (1)
  • 原文地址:https://www.cnblogs.com/lanqingyu/p/9828587.html
Copyright © 2011-2022 走看看