0%

最近在做一个项目, 需要用到log4j来生成日志, 方便debug

但是log4j是分版本的, 现在的最新版是log4j2, 而不是log4j.

我一开始以为只是版本新旧有不同, 后来发现配置文件log4j.properties怎么都加载不进去. 查了好多资料才发现log4j2和log4j是不兼容的.

阅读全文 »

Hashtable阅读笔记

这是我读的第2个源码, mark一下吧.

建议如果阅读代码中的笔记, 将下面的代码块拷贝到vs code或idea上. 因为在markdown的代码块中宽度有限制, 会影响观看效果.

阅读全文 »

HashMap阅读笔记

这是我读的第1个源码, mark一下吧.

强烈推荐 JDK8 HashMap源码行级解析 红黑树操作 史上最全最详细图解_anlian523的博客-CSDN博客. 关于红黑树的部分讲解的很好.

建议如果阅读代码中的笔记, 将下面的代码块拷贝到vs code或idea上. 因为在markdown的代码块中宽度有限制, 会影响观看效果.

阅读全文 »

Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

Follow up: Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

Example 1:

1
2
3
Input: nums = [1,2,1,3,2,5]
Output: [3,5]
Explanation: [5, 3] is also a valid answer.

Example 2:

1
2
Input: nums = [-1,0]
Output: [-1,0]

Example 3:

1
2
Input: nums = [0,1]
Output: [1,0]

Constraints:

  • 1 <= nums.length <= 30000
  • Each integer in nums will appear twice, only two integers will appear once.
阅读全文 »

1. 前言

今天刷leetcode 349题的时候, 发现了一个有趣的事情.

原题是求两个数组的交集, public int[] intersection(int[] nums1, int[] nums2). 我觉得你们应该也不会觉得这个题难. 转化为两个集合之后很简单就做出来了. 但是, 题目要求返回的是int[].

一开始我也没当回事, 以为调用一个toArray()方法就可以的. 但是这种方法根本不行, 只能得到Integer[] 而不是int[]. 并且Integer[]没法强制转换成为int[].

事实上, 如果我们想将一个Set<Integer>转化成int[], 还是需要一番操作的.

首先最笨的方法就是new一个int[]然后逐个元素添加. 但是这种方法显得太古老了一些.

在网上查了一番资料之后, 找到了解决这个问题的办法, 可以实现Integer[]int[]互转. 那就是用Java 8中新引入的特性, Stream.

阅读全文 »

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

Example:

Consider the following matrix:

1
2
3
4
5
6
7
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

阅读全文 »

0. 前言

“java接口和抽象类的异同”这个题绝对是java面试中基础的, 高频的问题. 之前零零散散的复习过很多关于接口和抽象类讲解的文章, 但是没有系统的自己整理下来. 导致知识体系不全面并且很容易忘. 这次花点时间全面的分析一下两者的异同.

阅读全文 »

Given a string s and a string t, check if s is subsequence of t.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).

Follow up:
If there are lots of incoming S, say S1, S2, … , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

Example 1:

1
2
Input: s = "abc", t = "ahbgdc"
Output: true

Example 2:

1
2
Input: s = "axc", t = "ahbgdc"
Output: false

Constraints:

  • 0 <= s.length <= 100
  • 0 <= t.length <= 10^4
  • Both strings consists only of lowercase characters.
阅读全文 »

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Implement the NumArray class:

  • NumArray(int[] nums) Initializes the object with the integer array nums.
  • int sumRange(int i, int j) Return the sum of the elements of the nums array in the range [i, j] inclusive (i.e., sum(nums[i], nums[i + 1], ... , nums[j]))

Example 1:

1
2
3
4
5
6
7
8
9
10
11
Input
["NumArray", "sumRange", "sumRange", "sumRange"]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
Output
[null, 1, -1, -3]

Explanation
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3)
numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1))
numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))

Constraints:

  • 0 <= nums.length <= 104
  • -105 <= nums[i] <= 105
  • 0 <= i <= j < nums.length
  • At most 104 calls will be made to sumRange.
阅读全文 »

Given a non-empty array of integers, return the k\ most frequent elements.

Example 1:

1
2
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

1
2
Input: nums = [1], k = 1
Output: [1]

Note:

  • You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  • Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.
  • It’s guaranteed that the answer is unique, in other words the set of the top k frequent elements is unique.
  • You can return the answer in any order.
阅读全文 »