Hugenye的个人博客

面试题

字数统计: 6.3k阅读时长: 27 min
2019/09/22 Share

杭州智库

1 set集合的底层结构

1
红黑树

2 hashmap的底层结构

1
底层结构是hash表数据结构,可以存入null键和null值,该集合线程不同步,效率较高,代替hashTable。实际上是一个“链表的数组”的数据结构。

3 案例 图片上传的优化方法

4 Redis有几种数据类型,分别是什么?

1
2
3
4
5
6
5种,分别是String、list、set、hash、sort set;
string 字符串(可以为整形、浮点型和字符串,统称为元素)
list 列表(实现队列,元素不唯一,先入先出原则)
set 集合(各不相同的元素)
hash hash散列值(hash的key必须是唯一的)
sort set 有序集合

5 Mysql是什么锁机制

1
行锁

6 联合索引
$$
实例问题:有ABC联合的索引,那么哪种情况会触发联合索引。
1,AB 2.BC 3.AC 4.CBA
$$
本文主要总结查询语句触发联合索引(索引定义中至少包含两个索引列)的几种条件。

示例如下。首先创建表:

1
CREATE TABLE E (e1 INT, e2 VARCHAR(9), e3 INT, PRIMARY KEY(e1, e3));

这样就建立了一个联合索引:e1,e3

触发联合索引是有条件的:

1、使用联合索引的全部索引键,可触发索引的使用。

1
例如:SELECT E.* FROM E WHERE E.e1=1 AND E.e3=2

2、使用联合索引的前缀部分索引键,如“key_part_1常量,可触发索引的使用。

1
例如:SELECT E.* FROM E WHERE E.e1=1

【注意】

3、使用部分索引键,但不是联合索引的前缀部分,如“key_part_2常量,不可触发索引的使用。

1
SELECT E.* FROM E WHERE E.e3=1

4、使用联合索引的全部索引键,但索引键不是AND操作,不可触发索引的使用。

罗万信息笔试题

  1. ==和equals的区别

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     1)对于==,比较的是值是否相等

                如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等;

        如果作用于引用类型的变量,则比较的是所指向的对象的地址

      2)对于equals方法,注意:equals方法不能作用于基本数据类型的变量,equals继承Object类,比较的是是否是同一个对象

        如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;

        诸如String、Date等类对equals方法进行了重写的话,比较的是所指向的对象的内容。
    原文链接:https://blog.csdn.net/qq_27471405/article/details/81010094
  2. abstract class和interface 的区别

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    从形态上看
    抽象类可以给出一些成员的实现,接口却不包含成员的实现;
    抽象类的抽象成员可被子类部分实现,接口的成员需要实现类完全实现;
    一个类只能继承一个抽象类,但可实现多个接口等等。

    如何区分
    1.类是对对象的抽象,抽象类是对类的抽象;
    接口是对行为的抽象。
    2.若行为跨越不同类的对象,可使用接口;
    对于一些相似的类对象,用继承抽象类。
    3.抽象类是从子类中发现了公共的东西,泛化出父类,然后子类继承父类;
    接口是根本不知子类的存在,方法如何实现还不确认,预先定义。
    原文链接:https://blog.csdn.net/MrBaymax/article/details/84328918
  3. 怎么唤醒一个阻塞的线程

    如果线程是因为调用了wait()、sleep()或者join()方法而导致的阻塞,可以中断线程,并且通过抛出InterruptedException来唤醒它;如果线程遇到了IO阻塞,无能为力,因为IO是操作系统实现的,Java代码并没有办法直接接触到操作系统。

  4. String,StringBuffer和StringBuilder的区别;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    1 .三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String
    2 .String <(StringBuffer,StringBuilder)的原因
    String:字符串常量
    StringBuffer:字符串常量
    StringBuilder:字符串常量
    StringBuilder:线程非安全的
    StringBuffer:线程安全的
    当我们在字符串缓冲去被多个线程使用是,JVM不能保证StringBuilder的操作是安全的,虽然他的速度最快,但是可以保证StringBuffer是可以正确操作的。
    当然大多数情况下就是我们是在单线程下进行的操作,所以大多数情况下是建议用StringBuilder而不用StringBuffer的,就是速度的原因。
    对于三者使用的总结: 1.如果要操作少量的数据用 = String
    2.单线程操作字符串缓冲区 下操作大量数据 = StringBuilder
    3.多线程操作字符串缓冲区 下操作大量数据 = StringBuffer
  5. 实现多线程有几种方法?分别是什么?实现线程同步有几种方法,分别是什么?

1
2
3
4
5
6
7
8
9
10
11
12
实现多线程有三种方法:
1.继承thread类,重写run方法;
2.实现Runnable接口;
3.实现Callable接口。
实现线程同步有五种方法:
1.同步方法(synchronized关键字修饰的方法);
2.同步代码块(synchronized关键字修饰的语句块);
3.使用特殊域变量(volatile)实现线程同步;
4.使用重入锁实现线程同步(ReenreantLock);
5.使用局部变量实现线程同步(ThreadLocal )。
原文链接:
https://www.cnblogs.com/jiansen/p/7351872.html

用友笔试题

  1. Java语言中,处理IO的方式都有_ , _和____三种。(NIO,BIO,AIO)

  2. 求交换两个变量的值,不使用第三个变量。即a=3,b=5,交换之后a=5,b=3; ?

    看到这个题在没有考虑必须用JAVA的情况下,我用了C语言的移位的骚操作(转化为二进制3=011,5=101),面试官都笑了。

    百度了一下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //有两种解法, 一种用算术算法, 一种用^(异或) 
    a = a + b;
    b = a - b;
    a = a - b;
    or
    a = a^b;// 只能对int,char..
    b = a^b;
    a = a^b;
    or
    a ^= b ^= a;
  3. 求两个字符串的最长公共字符串

  4. 数据库笔试题

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
Student(Sid,Sname,Sage,Ssex)学生表
Sid:学号
Sname:学生姓名
Sage:学生年龄
Ssex:学生性别
Course(Cid,Cname,T#)课程表
Cid:课程编号
Cname:课程名称
Tid:教师编号
SC(Sid,Cid,score)成绩表
Sid:学号
Cid:课程编号
score:成绩
Teacher(Tid,Tname)教师表
Tid:教师编号:
Tname:教师名字
问题:

1、查询“001”课程比“002”课程成绩高的所有学生的学号
select a.sid from
(select sid,score from sc where cid='001')a,
(select sid,score from sc where cid='002')b
where a.sid = b.sid and a.score>b.score;
2、查询平均成绩大于60分的同学的学号和平均成绩
select sid,avg(score) from sc
group by sid
having avg(score)>60;
3、查询所有同学的学号、姓名、选课数、总成绩
select s.sid,s.sname,count_cid as 选课数,
sum_score as 总成绩
from student s
left join
(select sid,count(cid) as count_cid,sum(score) as sum_score
from sc group by sid )sc
on s.sid = sc.sid;
4、查询姓‘李’的老师的个数:
select count(tname)
from teacher
where tname like '李%';
5、查询没有学过“叶平”老师可的同学的学号、姓名:
select s.sid,s.sname
from student as s
where s.sid not in (
select DISTINCT sid
from sc as sc
where sc.cid in (
select cid
from course as c
left join teacher as t on c.tid = t.tid
where t.tname = '叶平')
);
6、查询学过“叶平”老师所教的所有课的同学的学号、姓名:
select s.sid,s.sname
from student as s
where s.sid in (
select distinct sc.sid
from sc as sc
where sc.cid in (
select cid
from course as c
left join teacher as t on c.tid = t.tid
where t.tname = '叶平')
group by sc.sid
HAVING count(cid)=
(select count(cid)
from course as c left join teacher as t on c.tid = t.tid
where t.tname = '叶平')
);
7、查询学过“011”并且也学过编号“002”课程的同学的学号、姓名:
SELECT s.sid,s.sname
from student as s
left join sc as sc on s.sid = sc.sid
where sc.cid = '001'
and EXISTS(
select * from sc as sc_2
where sc.sid = sc_2.sid
and sc_2.cid='002');

select s.sid,s.sname
from student as s
left join sc as sc
on sc.sid = s.sid
where sc.cid = '001'
and s.sid in (
select sid from sc as sc_2
where sc_2.cid='002'
and sc_2.sid = sc.sid);
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名:
select sid,sname
from (select student.sid,student.sname,score,
(select score from sc as sc_2
where sc_2.sid = student.sid
and sc_2.cid = '002') as score2
from student,sc
where student.sid=sc.sid and cid = '001') s_2
where score2<score;
9、查询所有课程成绩小于60的同学的学号、姓名:
select sid,sname
from student
where sid not in
(select s.sid
from student s,sc
where s.sid=sc.sid and score>60 );

select sid,sname
from student s
where not EXISTS (
select s.sid from sc
where sc.sid = s.sid and sc.score>60);
10、查询没有学全所有课的同学的学号、姓名:
select s.sid,s.sname
from student s ,sc sc
where s.sid = sc.sid
group by s.sid,s.sname
having count(sc.cid)<(
select count(cid)
from course);

select s.sid,s.sname
from student s
right join sc sc on s.sid = sc.sid
group by s.sid,s.sname
having count(sc.cid)<
(select count(cid) from course);
11、查询至少有一门课与学号为“1001”同学所学相同的同学的学号和姓名:
select student.sid,sname
from student,sc
where student.sid = sc.sid
and cid in
(select cid from sc where sid='1001');

select s.sid,s.sname
from sc sc left join student as s
on sc.sid = s.sid
where sc.cid in (select cid from sc where sid='1001');

select sc_1.sid,s.sname
from sc sc_1 left join student as s
on sc_1.sid = s.sid
where
exists (select sc_2.cid from sc as sc_2
where sc_1.cid = sc_2.cid
and sc_2.sid = '1001');
12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩:
update sc set score =
(select avg(sc_2.score) from sc sc_2
where sc_2.cid = sc.cid)
where cid in
(select c.cid from course c
left join teacher t on t.tid = c.tid
where t.tname = '叶平');
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名:
select sc_1.sid
from (select cid from sc where sid='1002')a
left join sc sc_1 on a.cid = sc_1.cid
where sc_1.sid<>'1002'
group by sc_1.sid
having count(sc_1.cid) =
(select count(cid) from sc where sid='1002');

select a.sid,s.sname from
(select sid,GROUP_CONCAT(cid order by cid separator ',') as cid_str
from sc where sid='1002')b,
(select sid,GROUP_CONCAT(cid order by cid separator ',') as cid_str
from sc group by sid)a
left join student s
on a.sid = s.sid
where a.cid_str = b.cid_str and a.sid<>'1002';
15、删除学习“叶平”老师课的SC表记录:
delete from sc WHERE
cid in (
select c.cid from course c
LEFT JOIN teacher t on c.tid=t.tid
where t.tname = '叶平');
16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、002号课的平均成绩:
insert into sc select sid,'002',
(select avg(score) from sc where cid='0022')
from student
where sid not in (select sid from sc where cid='002');
17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分:
select sid as 学生id,
(SELECT score from sc
where sc.sid = t.sid and cid='004') as 数据库,
(select score from sc
where sc.sid = t.sid and cid='001') as 企业管理,
(select score from sc
where sc.sid = t.sid and cid='015') as 英语,
count(cid) as 有效课程数, avg(t.score) as 平均成绩
from sc as t
group by sid
order by avg(t.score);
18、查询各科成绩最高和最低的分:以如下的形式显示:课程ID,最高分,最低分
select l.cid as 课程id,l.score as 最高分,
r.score as 最低分
from sc l,sc r
where l.cid = r.cid
and l.score =
(select max(t.score) from sc t
where l.cid = t.cid group by t.cid)
and r.score = (select min(t.score) from sc t
where r.cid = t.cid group by t.cid)
order by l.cid;

select cid as 课程id,max(score) as 最高分,
min(score) as 最低分
from sc
group by cid;
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序:
SELECT t.cid as 课程号,
c.cname as 课程名,
COALESCE(avg(score),0) as 平均成绩,
100*sum(case
when COALESCE(score,0)>=60
then 1 else 0 END)/count(*) as 及格百分数
from sc t
left join course c
on t.cid = c.cid
group by t.cid
order by 100*sum(case
when COALESCE(score,0)>=60
then 1 else 0 END)/count(*);
20、查询如下课程平均成绩和及格率的百分数(用”1行”显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004):
21、查询不同老师所教不同课程平均分从高到低显示:
select t.tid as 教师id,
t.tname as 教师姓名,
sc.cid as 课程id,
avg(score) as 平均成绩
from sc as sc
LEFT JOIN course c on sc.cid = c.cid
left join teacher t on c.tid = t.tid
group by sc.cid
order by avg(sc.score) desc;
22、查询如下课程成绩第3名到第6名的学生成绩单:企业管理(001),马克思(002),UML(003),数据库(004):
23、统计下列各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ 小于60] :
select sc.cid as 课程id,cname as 课程名称,
sum(case when score between 85 and 100 then 1 else 0 end) as '[100-85]',
sum(case when score between 70 and 85 then 1 else 0 end) as '[85-70]',
sum(case when score between 60 and 70 then 1 else 0 end) as '[70-60]',
sum(case when score<60 then 1 else 0 end) as '[60-0]'
from sc as sc
left join course as c
on sc.cid = c.cid
group by sc.cid;
24、查询学生平均成绩及其名次:
select 1+(select count(distinct 平均成绩)
from (select sid,avg(score) as 平均成绩
from sc group by sid)t1
where 平均成绩>t2.平均成绩) as 名次,
sid as 学生学号,平均成绩
from (select sid,avg(score) 平均成绩 from sc group by sid) as t2
order by 平均成绩 desc;
25、查询各科成绩前三名的记录(不考虑成绩并列情况):
select sid,cid,score
from sc sc_1
where (
select count(3) from sc sc_2
where sc_1.cid = sc_2.cid
and sc_2.score>=sc_1.score)<=2
order by sc_1.cid
);
26、查询每门课程被选修的学生数:
select cid, count(sid)
from sc
group by cid;
27、查询出只选修一门课程的全部学生的学号和姓名:
select sc.sid,s.sname,
count(sc.cid) as 课程数
from sc as sc
LEFT JOIN student as s
on sc.sid = s.sid
group by sc.sid
having count(sc.cid)=1;
28、查询男生、女生人数:
select count(ssex) as 男生人数
from student
group by ssex
having ssex = '男';
select count(2) from student
where ssex = '女';
29、查询姓“张”的学生名单:
select sid,sname
from student
where sname like '张%';
30、查询同名同姓的学生名单,并统计同名人数:
select sname,count(8)
from student
group by sname
having count(8)>1;
31、1981年出生的学生名单(注:student表中sage列的类型是datetime):
32、查询平均成绩大于85的所有学生的学号、姓名和平均成绩:
select s.sname,sc.sid,avg(sc.score) as 平均成绩
from sc as sc
left join student as s
on sc.sid = s.sid
group by sc.sid
having avg(sc.score)>85;
33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列:
select cid,avg(score)
from sc
group by cid
order by avg(score),cid desc;
34、查询课程名称为“数据库”,且分数低于60的学生名字和分数:
select c.cname,s.sid,s.sname,sc.score
from course c
left join sc on sc.cid = c.cid
LEFT JOIN student s on s.sid = sc.sid
where c.cname = '数据库' and sc.score<60;
35、查询所有学生的选课情况:
select sc.sid,sc.cid,s.sname,c.cname
from sc
LEFT JOIN course c on sc.cid = c.cid
left join student s on sc.sid = s.sid;
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数:
select distinct s.sid,s.sname,sc.cid,sc.score
from sc
left join student s on sc.sid = s.sid
left join course c on sc.cid = c.cid
where sc.score>70;
37、查询不及格的课程,并按课程号从大到小的排列:
select cid
from sc
where score<60
ORDER BY cid;
38、查询课程编号为“003”且课程成绩在80分以上的学生的学号和姓名:
select sc.sid,s.sname
from sc
left join student s on sc.sid = s.sid
where sc.cid = '003' and sc.score>80;
39、求选了课程的学生人数:
select count(2) from
(select distinct sid from sc)a;
40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩:
select s.sname,sc.score
from sc sc
left join student s on sc.sid = s.sid
left join course c on sc.cid = c.cid
left join teacher t on c.tid = t.tid
where t.tname = '叶平'
and sc.score = (
select max(score)
from sc sc_1
where sc.cid = sc_1.cid);
41、查询各个课程及相应的选修人数:
select cid,count(*) from sc group by cid;
42、查询不同课程成绩相同的学生和学号、课程号、学生成绩:
select DISTINCT a.sid,a.cid,a.score
from sc as a ,sc as b
where a.score = b.score
and a.cid <> b.cid;
43、查询每门课程成绩最好的前两名:
SELECT s1.* FROM SC s1 WHERE
(
SELECT COUNT(1) FROM SC s2 WHERE
s1.cid=s2.cid AND s2.score>=s1.score
)<=2
ORDER BY s1.cid,s1.score DESC;
44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号升序排序:
select cid as 课程号,count(8) as 选修人数
from sc
group by cid
HAVING count(sid)>10
order by count(8) desc,cid;
45、检索至少选修两门课程的学生学号:
select sid
from sc
group by sid
having count(8)>=2;
46、查询全部学生选修的课程和课程号和课程名:
select cid,cname
from course
where cid in (select cid from sc group by cid);
47、查询没学过”叶平”老师讲授的任一门课程的学生姓名:
select sname
from student
where sid not in (
select sid
from sc,course,teacher
where course.tid = teacher.tid and sc.cid = course.cid
and teacher.tname='叶平'
);
48、查询两门以上不及格课程的同学的学号以及其平均成绩:
select sid,avg(COALESCE(score,0))
from sc
where sid in (
select sid
from sc
where score<60
group by sid
having count(8)>2
)
group by sid;
49、检索“004”课程分数小于60,按分数降序排列的同学学号:
select sid,score from sc where cid='004' and score<60 order by score desc;
50、删除“002”同学的“001”课程的成绩:
delete from sc where sid = '002'and cid = '001';

呆萝卜面试题

  1. list和map的区别?map的底层结构?

    1
    2
    3
    List:是存储单列数据的集合,存储的数据是有序并且是可以重复的
    Map:存储双列数据的集合,通过键值对存储数据,存储的数据是无序的,Key值不能重复,value值可以重复
    map的底层是由数组和链表组成。
  2. HashMap中通过key获取value源码剖析

    1
    2


  3. 在一个长度为n的数组中插入一个元素的算法复杂度是多少?

    1
    o(n)
  4. int、double、float的大小?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    一般情况下:

    32位编译器:

    char      short      int      long      float      double      指针

       1            2           4         4            4              8       

    64位编译器:

    char      short      int      long      float      double      指针

       1            2           4         8            4              8
  5. 堆和栈的区别?什么是“堆”,”栈”,”堆栈”,”队列”,它们的区别?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    1.堆栈空间分配

    ①栈(操作系统):由操作系统自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。

    ②堆(操作系统): 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收,分配方式倒是类似于链表。

    2.堆栈缓存方式

    ①栈使用的是一级缓存, 他们通常都是被调用时处于存储空间中,调用完毕立即释放。

    ②堆则是存放在二级缓存中,生命周期由虚拟机的垃圾回收算法来决定(并不是一旦成为孤儿对象就能被回收)。所以调用这些对象的速度要相对来得低一些。

    3.堆栈数据结构区别

    ①堆(数据结构):堆可以被看成是一棵树,如:堆排序。

    ②栈(数据结构):一种先进后出的数据结构。
  6. mysql有那些存储引擎?哪一种支持事务?数据库事务

    1
    在mysql中用的最多的存储引擎有:innodb,bdb,myisam ,memory 等。其中innodb和bdb支持事务而myisam等不支持事务。
  7. mysql的索引有哪些什么是索引?Mysql目前主要的几种索引类型?优化索引的方法有哪些索引性能优化

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    索引有:
    普通类型(CREATE INDEX)
    唯一索引,索引列的值必须唯一(CREATE UNIQUE INDEX)
    多列索引
    主键索引(PRIMARY KEY),一个表只能有一个
    全文索引(FULLTEXT INDEX),InnoDB 不支持
    索引的优化方法:
    非空字段 NOT NULL,Mysql 很难对空值作查询优化
    区分度高,离散度大,作为索引的字段值尽量不要有大量相同值
    索引的长度不要太长(比较耗费时间)
  8. 二叉树的优势(查询速度快)

  9. String IOC的理解Spring中IOC与AOP的理解?它是如何如何实现的(用了哪些设计模式)?

    Spring的IOC原理[通俗解释一下]

    1
    2
    3
    4
    5
    6
    7
    IOC也称控制反转,其实是和依赖注入的含义是一样的,就是把原先控制代码对象的生产由代码转换到IOC容器中去实现。作用是为了解耦,降低类之间的耦合度,其设计思想就是设计模式的工厂模式,不懂什么是工厂模式的点击此:。。。。。,我们并不需要知道其生产的具体过程,我们只要其产出的对象即可。其工作流程就是:在Spring容器启动的时候,Spring会把你在application.xml中配置好的bean都初始化,在你需要调用的时候,把已经初始化的bean分配给你要调用这些bean的类,而不用去创建一个对象的实例。

    1 工厂模式:BeanFactory、ApplicationContext创建中
    2 模板模式:BeanFactory、ApplicationContext实现中
    3 代理模式:在AOP实现中用到了JDK的动态代理
    4 策略模式:加载资源文件的方式;AOP实现中采用了两种不同的代理,如JDK代理和CGLIB代理
    5 单例模式:创建bean
  10. 用过哪一些设计模式在项目中,如何在具体的项目中使用的?(一般单例经常被使用)

正方

  1. 一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合.

    1
    2
    3
    SELECT  a.name,b.name from team AS a,team AS b where a.name<b.name
    --如果觉得顺序有些乱可以排一下序
    SELECT a.name,b.name from team AS a,team AS b where a.name<b.name ORDER BY a.name
  2. 写一个ArrayList的动态代理类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package TestProxy;

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.util.ArrayList;
    import java.util.List;

    public class ProxyTest {
    public static void main(String[] args) {
    final ArrayList<String> list = new ArrayList<>();
    List<String> listProxy=(List<String>) Proxy.newProxyInstance(list.getClass().getClassLoader(), list.getClass().getInterfaces(), new InvocationHandler() {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    return method.invoke(list,args);
    }
    });
    listProxy.add("你好") ;
    System.out.println(list);
    }
    }

    leetcode习题

算法思想

双指针

题目:在有序组中找出两个数,是他们的和为target。

使用双面指针,一个指针指向较小的元素,一个指针指向较大的元素。指向较小元素的指针从头向尾遍历,指向较大元素的指针从尾向头遍历。

如果要判断sum==target,那么就可以等到要求的结果

如果sum>target,移动较大的元素,使sum变小,

反之sum<target,移动较小元素,是sum变大

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public int[] twoSum(int[] numbers,int target){  
if(numbers==null)
return null;
int i=0;j=numbers.length-1;
while(i<j){  
int sum=numbers[i]+numbers[j];  
if(sum==target){
return new int[]{i+1,j+1};
} else if(sum<target){
i++;
}else{
j++;
}
}
return null;
}

ps:时间空间复杂度–》判断一个算法的好坏

时间复杂度BigO

大O表示法是算法的渐进时间复杂度

T(n)=O(f(n))—》f(n)代表代码执行的次数,大O表示正比例关系;

比如O(logN)

1
2
3
4
int i=1;
while(i<n){
i=i*2; //i*2 i++次后会越来越接近n ,设循环次数为k:2的k次方等于n==》k=log2n ,n取极限为,为logN
}

O(n)–单层for循环;

O(n^2)–双层for循环;

有特殊情况

1
2
3
4
5
6
7
8
9
10
for(int i=1;i<=n;i++){
i++;
}//f(3n+1),n取极限,为O(n)
for(int i=1;i<=n;i++){
i++;//f(3n+1),n取极限,为O(n)
for(int j=1;j<=n;j++){
j++;
}//f(3n+1),n取极限,为O(n)
}//两层循环,就是O(n^2)
一个代码块是这个样子,T(n)=O(n^2+n) ,n在这里取极限相当于常量,所以T(n)=O(n^2)

普通代码数据操作–O(1);

其它复杂度的指标有:

O(Big O) 最差情况

Ω(big Omega) 最好情况

θ(big theta) 一个算法区间

2、空间复杂度—》内存空间增长的趋势

常用的空间复杂度O(1)、O(n)、O(n^2)

时间空间复杂度=时间和空间增长的趋势

CATALOG
  1. 1. 杭州智库
  2. 2. 罗万信息笔试题
  3. 3. 用友笔试题
  4. 4. 呆萝卜面试题
  5. 5. 正方
  • leetcode习题
    1. 1. 算法思想
      1. 1.1. 双指针