寒い

  • Home

  • About

  • Tags

  • Categories

  • Archives

Timer

Posted on 2019-02-03 | Edited on 2019-04-03

Timer

变量:

  • TaskQueue queue 在TashQueue中 private TimerTask[] queue = new TimerTask[128];
  • TimerThread thread class TimerThread extends Thread

缺点:只有一个thread来执行所有的任务,如果有个任务抛出异常,那么所有的任务就会停止,

如果存在多个任务,切任务的时间很长,导致执行结果于预期不符

构造函数
1
2
3
4
5
public Timer(String name, boolean isDaemon) {
thread.setName(name);
thread.setDaemon(isDaemon);
thread.start();
}

TimerThread的运行:每次执行之后之后都会对数组中排序,最小的放在最上面,有可能有多个任务,任务的执行时间长的话,就推到下个周期。

Read more »

循环和递归

Posted on 2019-01-14

找出集合的子集,有重复元素的情况下。讨论区看到不同的代码,自己写的时候用第一种,在遍历下一个元素的时候用递归的方方式,这样比较慢。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public void helper(List<List<Integer>> res, List<Integer> each, int pos, int[] n) {
if (pos <= n.length) {
res.add(each);
}
int i = pos;
while(i < n.length) {
each.add(n[i]);
helper(res, new ArrayList<>(each), i + 1, n);
each.remove(each.size() - 1);
i++;
while (i < n.length && n[i] == n[i - 1]) {i++;}
//helper(res,new ArrayList<>(each),i,n);
}
return;
}
和
public void helper(List<List<Integer>> res, List<Integer> each, int pos, int[] n) {
if (pos == n.length) {
res.add(each);
}
int i = pos;
if(i < n.length) {
each.add(n[i]);
helper(res, new ArrayList<>(each), i + 1, n);
each.remove(each.size() - 1);
i++;
while (i < n.length && n[i] == n[i - 1]) {i++;}
helper(res,new ArrayList<>(each),i,n);
}
return;
}

上面两种方法的结果是一样的,第二个的思路很好理解,和无重复数字时的相同,一个数字要么加,要么不加,但是递归的次数要多一些,。而第一个方法递归次数和结果一样。第一个种方法是把不加的数字,在循环中跳过,而第二中通过递归来实现。第一个是先把集合找齐,然后一个个去掉数字进行实现。第二个是从一开始添加数字时就带或者不带进行选择,当遍历到最后的数字时,完成了。第二种方法快一点。

二分查找

Posted on 2019-01-08

左闭右开,缩小边界时,左边需要加1,右边不需要。

1
2
3
4
5
6
7
8
public int search(int[] a,int start,int end,int target){/ /前闭后开,
while(start<end){
int mid=start+(end-start)/2;
if(a[mid]<target)start=mid+1;
else
end=mid;
}
return start;
Read more »

数据结构与算法II

Posted on 2018-12-14 | Edited on 2018-12-15

图

用邻接表来表示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Graph{
private final int V;
private int E;
private Bag<Integer>[] adj;
public Graph(int V){
this.V=V;thisE=0;
adj=(Bag<Integer>[])new Bag[V];
for(int v=0;v<V;v++)
adj[v]=new Bag<Integer>();
}
public int V(){return V;}
public int E(){return E;}

public void addEdge(int v,int w){
adj[v].add(w);
adj[w].add(v);
E++;
}









int v();
int E();
public String toString();
public Graph(int v);

public static int degree(Graph G,int v){
int degree=0;
for(int W:G.adj(v))degree++;
return degree;
}
public static int maxDegree(Graph G){
int max=0;
for(int v=0;v<G.V();v++)
if(degree(G,v)>max)
max=degree(G,v);
return max;
}
public static int numberOfSelfLoops(Graph G){
int count=0;
for(int v=0;v<G.V();v++)
for(int w:G.adj(v))
if(v==w)count++;
return count/2;
}

public String toString(){
String
}
}

面试后

Posted on 2018-12-12

今天头条二面,两道算法题,一是:交换数字位置变为最大,NO670,要求O(n),之前在leetCode做题很少想过找出最优的解法,今天遇到了,怎么也想不出来,一开始用的$O(n^2)$,提示说O(n),想着怎么一次遍历过来,其实有时候思维定势,看到$O(n)$就想着一次遍历,其实只要不是n次遍历,都行,第二题是个简单的二分,但是其中一个条件,mid多加了1,有bug,应该是没了,最好的一次机会白白丢失了。明明很简单的题,就像考试一样,做题时候脑子里一片空白,老是犯错误,从大三到现在还是没有找到方法去改变。

  • 有时候,在想边界问题的时候一定要清晰是怎么回事。整体有了思路之后,再开始写代码,不然会来回修改代码,很容易造成有变量遗漏,从而出现问题。

  • 做完题之后,进行思考是很有帮助的,不然每次做题的收获很少,没过两天就忘记了。

  • 一定要定时复习,不然看过的东西跟没看一样,就像之前的英语和日语单词一样,很眼熟但是就是想不起来。

现在时间已经不够了,多多利用吧。面试之后找到不足,也找到了努力的方向,春招加油吧。

数据结构

Posted on 2018-12-09

平衡二叉树


递归就是把只考虑一层,然后考虑边界条件,就像数学归纳法一样。注意的是不要每回合都算一遍。

1
2
3
4
5
6
7
8
9
10
11
12
13
public boolean isBalanced(TreeNode root){
return dfs(root)!=-1;

}
public int dfs(TreeNode root){
if(root==null)return 0;
int leftHeight=dfs(root.left);
if(leftHeight==-1)return -1;
int rightHeight=dfs(root.right);
if(rightHeight==-1)return -1;
if(Math.abs(leftHeight-rightHeight)>1)return -1;
return Math.max(leftHeight,rightHeight)+1;
}
Read more »

algorithm

Posted on 2018-11-27

backtracking


1. Permutation

在当前的进度上,接下来有很多的元素,并且是等价的,需要,接受一个,然后替换另一种可能性,过程中失败回到上一个状态。

Read more »

CollectionAndMap

Posted on 2018-10-15

ArrayList


ArrayList基于可变数组: Object[] ={ },初始容量为10。private static final int DEFAULT_CAPACITY = 10;

1.扩容

newCapacity = oldCapacity + (oldCapacity >> 1) 正整数 向下取整
10 15 22 33
elementData = Arrays.copyOf(elementData, newCapacity);新建一个更大的数组,将旧的数组数据copy,如果数据很多,尽量初始化时设定容量大小,不用在添加的过程中不断扩容。

Read more »

n sum

Posted on 2018-09-30 | Edited on 2018-10-15
  1. Two Sum
    Example:

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


思路:排序后,双指针遍历,可以去重

1
while (i < nums.length - 1 && nums[i] == nums[i + 1]) i++;

或者存入HashMap<nums[i],i>,查找target-nums[i],

Read more »

第一篇

Posted on 2018-09-28 | Edited on 2018-10-15

基本语法

预定义字符类

符号 意思
. 任意一个非\字符
\d 字母,数字,下划线,汉字
\s 任意空白字符
\D 非数字
\S 非空白数字
\w 单词字符(字母,数字)
\W 非单词字符
Read more »
1234
YAHIKO

YAHIKO

34 posts
5 categories
16 tags
© 2019 YAHIKO
Powered by Hexo v3.7.1
|
Theme – NexT.Gemini v6.4.2