第一周
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 | Given nums = [2, 7, 11, 15], target = 9, |
刚开始使用暴力解法直接双重循环
1 | Public int[] twoSum(int[] nums, int target) { |
写完之后看了下被人的解法
其中
1 | public int[] twoSum(int[] nums, int target) { |
这个是如果没有刷过自己是想不到的利用map的containsKey减少了一层循环然后去看了HashMap的这个的实现是调用了如下方法1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19final 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 即可解决