Fork me on GitHub

arts1

第一周

Algorithm

先从刷LeetCode开始吧!之前一直没刷过,坚持 刷 刷 刷

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

1
2
3
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

刚开始使用暴力解法直接双重循环

1
2
3
4
5
6
7
8
9
10
Public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
throw new IllegalArgumentException("No two sum solution");
}

写完之后看了下被人的解法
其中

1
2
3
4
5
6
7
8
9
10
11
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0;i < nums.length;i++){
int v = target - nums[i];
if(map.containsKey(v)){
return new int[]{map.get(v),i};
}
map.put(nums[i],i);
}
throw new IllegalArgumentException("No two sum solution");
}

这个是如果没有刷过自己是想不到的利用map的containsKey减少了一层循环然后去看了HashMap的这个的实现是调用了如下方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}

这就是所谓的用空间换时间吧

Rewiew

Tip

在用apache搭建 webdav 时遇到文件名称中文乱码
在这里插入图片描述
看这里主要描述了IndexOptions配置的作用
在httpd.conf 加上 IndexOptions Charset=UTF-8 即可解决
在这里插入图片描述