ARTS打卡第十三周

Algorithm:Leetcode 739 每日温度

https://leetcode-cn.com/problems/daily-temperatures/
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].

暴力解法时间复杂度是O(n^2),使用栈可以减少到O(2n)=O(n),空间复杂度是O(n)。

复杂度分析:

首先数组的元素会挨个遍历一遍,时间复杂度是O(n)。其次,在遍历数组中第i个元素的时候,会对栈做弹出操作,弹出元素的个数是数组前i-1个元素中小于第i个元素的个数。栈中的元素最多就是n个,出栈一次的时间复杂度是O(1),所有出栈时间加起来复杂度就是O(n)。虽然从代码上看,是两层循环嵌套,但并不是没遍历一个数组元素就要出栈n次,而是所有出栈次数加起来是n次,所以总体时间复杂度是O(2n)=O(n)。

因为额外申请了一个栈空间,最坏情况下,栈中会存放n-1个元素,所以空间复杂度是O(n).

// 时间复杂度O(n),空间复杂度O(n)
    public int[] dailyTemperatures(int[] T) {
        ArrayDeque<Integer> stack = new ArrayDeque<>();
        final int len = T.length;
        int[] ans = new int[len];

        for(int i=0; i<len; i++) {
            int t = T[i];
            Integer top;
            while(!stack.isEmpty() && T[(top = stack.getLast())] < t) {
                ans[top] = i - top;
                stack.removeLast();
            }
            stack.addLast(i);

        }
        return ans;
    }

Tip: 使用Hibernate写库中文出现乱码的解决办法

  1. 检查mysql的字符集。mysql有多个级别的字符集设置,要保证都是utf8.
mysql> show variables like 'char%';
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8                                                      |
| character_set_connection | utf8                                                      |
| character_set_database   | utf8                                                      |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8                                                      |
| character_set_server     | utf8                                                      |
| character_set_system     | utf8                                                      |
| character_sets_dir       | /usr/local/mysql-5.7.25-macos10.14-x86_64/share/charsets/ |
+--------------------------+-----------------------------------------------------------+
8 rows in set (0.02 sec)

可以通过如下命令设置:

mysql> set global character_set_database=utf8;
mysql> set global character_set_server=utf8;
  1. 检查my.cnf配置文件
[mysqld]
character-set-server=utf8 
[client]
default-character-set=utf8 
[mysql]
default-character-set=utf8
  1. 如果上述字符集设置没有问题,那么在连接数据库的url上加上如下内容:
jdbc:mysql://127.0.0.1:3306/dbname?useUnicode=true&characterEncoding=UTF-8

Share: java源码Integer.bitCount算法解析,分析原理(统计二进制bit位)
原文链接:https://segmentfault.com/a/1190000015763941

这篇文章详细解释了JDK源码中计算“二进制整数包含的1bit位的个数”的算法。其实这个需求我们很容易想出一个迭代的算法,Leetcode官方题解里,也给出了优化版的迭代算法。但是当看到JDK中使用的算法之后,不得不感叹,JDK的研发工程师们在背后做了很多优化的工作。我们平时在使用JDK时,如果能多关注一下JDK源码里的一些实现,还是能学到很多东西的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值