-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
479 lines (291 loc) · 447 KB
/
atom.xml
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>这是一个记录成长的博客</title>
<link href="http://example.com/atom.xml" rel="self"/>
<link href="http://example.com/"/>
<updated>2023-09-26T13:13:16.660Z</updated>
<id>http://example.com/</id>
<author>
<name>wind</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>算法积累</title>
<link href="http://example.com/2023/09/26/%E7%AE%97%E6%B3%95%E7%A7%AF%E7%B4%AF/"/>
<id>http://example.com/2023/09/26/%E7%AE%97%E6%B3%95%E7%A7%AF%E7%B4%AF/</id>
<published>2023-09-26T12:34:13.872Z</published>
<updated>2023-09-26T13:13:16.660Z</updated>
<content type="html"><![CDATA[<h1 id="二分算法"><a href="#二分算法" class="headerlink" title="二分算法"></a>二分算法</h1><p>二分算法基本框架</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">searchInsert</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>,right=nums.length-<span class="number">1</span>,ans=right+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(left<=right){</span><br><span class="line"> <span class="keyword">int</span> mid = (left + right)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(nums[mid]>=target){</span><br><span class="line"> ans=mid;</span><br><span class="line"> right=mid-<span class="number">1</span>;}</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> left=mid+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>分析二分查找的一个技巧是:不要出现 else,而是把所有情况用 else if 写清楚,这样可以清楚地展现所有细节。</strong>本文都会使用 else if,旨在讲清楚,读者理解后可自行简化。</p><span id="more"></span><p>其中 … 标记的部分,就是可能出现细节问题的地方,当你见到一个二分查找的代码时,首先注意这几个地方。后文用实例分析这些地方能有什么样的变化。</p><p>另外声明一下,计算 mid 时需要技巧防止溢出,即 mid=left+(right-left)/2。本文暂时忽略这个问题。</p><p><strong>一、寻找一个数(基本的二分搜索)</strong></p><p>这个场景是最简单的,可能也是大家最熟悉的,即搜索一个数,如果存在,返回其索引,否则返回 -1。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">binarySearch</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> right = nums.length - <span class="number">1</span>; <span class="comment">// 注意</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span>(left <= right) {</span><br><span class="line"> <span class="keyword">int</span> mid = (right + left) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(nums[mid] == target)</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (nums[mid] < target)</span><br><span class="line"> left = mid + <span class="number">1</span>; <span class="comment">// 注意</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (nums[mid] > target)</span><br><span class="line"> right = mid - <span class="number">1</span>; <span class="comment">// 注意</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>1.</strong> 为什么 while 循环的条件中是 <=,而不是 < ?</p><p>答:因为初始化 right 的赋值是 nums.length-1,即最后一个元素的索引,而不是 nums.length。</p><p>这二者可能出现在不同功能的二分查找中,区别是:前者相当于两端都闭区间 [left, right],后者相当于左闭右开区间 [left, right),因为索引大小为 nums.length 是越界的。</p><p>我们这个算法中使用的是前者 [left, right] 两端都闭的区间。<strong>这个区间其实就是每次进行搜索的区间,我们不妨称为「搜索区间」</strong>。</p><p>什么时候应该停止搜索呢?当然,找到了目标值的时候可以终止:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">if(nums[mid] == target)</span><br><span class="line"> return mid;</span><br></pre></td></tr></table></figure><p>但如果没找到,就需要 while 循环终止,然后返回 -1。那 while 循环什么时候应该终止?<strong>搜索区间为空的时候应该终止</strong>,意味着你没得找了,就等于没找到嘛。</p><p>while(left <= right) 的终止条件是 left == right + 1,写成区间的形式就是 [right + 1, right],或者带个具体的数字进去 [3, 2],可见这时候搜索区间为空,因为没有数字既大于等于 3 又小于等于 2 的吧。所以这时候 while 循环终止是正确的,直接返回 -1 即可。</p><p>while(left < right) 的终止条件是 left == right,写成区间的形式就是 [left, right],或者带个具体的数字进去 [2, 2],这时候搜索区间非空,还有一个数 2,但此时 while 循环终止了。也就是说这区间 [2, 2] 被漏掉了,索引 2 没有被搜索,如果这时候直接返回 -1 就是错误的。</p><p>当然,如果你非要用 while(left < right) 也可以,我们已经知道了出错的原因,就打个补丁好了:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//...</span></span><br><span class="line"><span class="keyword">while</span>(left < right) {</span><br><span class="line"> <span class="comment">// ...</span></span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> nums[left] == target ? left : -<span class="number">1</span>;</span><br></pre></td></tr></table></figure><p>详解解析看 [<a href="https://www.cnblogs.com/mxj961116/p/11945444.html]">https://www.cnblogs.com/mxj961116/p/11945444.html]</a></p><p>leetcode_<a href="https://leetcode-cn.com/problems/B1IidL/">剑指 Offer II 069. 山峰数组的顶部</a></p><p>二分题解</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">peakIndexInMountainArray</span><span class="params">(<span class="keyword">int</span>[] arr)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span> , rigth = arr.length-<span class="number">1</span>,ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(left<=rigth)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> mid = (left+rigth)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(arr[mid]>arr[mid+<span class="number">1</span>])</span><br><span class="line"> {ans = mid;</span><br><span class="line"> rigth = mid-<span class="number">1</span>;}</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> left = mid+<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>leetcode_35</p><h1 id="35-搜索插入位置"><a href="#35-搜索插入位置" class="headerlink" title="35. 搜索插入位置"></a><a href="https://leetcode-cn.com/problems/search-insert-position/">35. 搜索插入位置</a></h1><p>题解</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">searchInsert</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>,right=nums.length-<span class="number">1</span>,ans=right+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(left<=right){</span><br><span class="line"> <span class="keyword">int</span> mid = (left + right)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(nums[mid]>=target){</span><br><span class="line"> ans=mid;</span><br><span class="line"> right=mid-<span class="number">1</span>;}</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> left=mid+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="33-搜索旋转排序数组"><a href="#33-搜索旋转排序数组" class="headerlink" title="33. 搜索旋转排序数组"></a><a href="https://leetcode-cn.com/problems/search-in-rotated-sorted-array/">33. 搜索旋转排序数组</a></h1><p>题解</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">search</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>, right = nums.length - <span class="number">1</span>,ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(left <= right)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> mid=(left+right);</span><br><span class="line"> <span class="keyword">if</span>(nums[mid] > nums[right]){</span><br><span class="line"> <span class="keyword">if</span>(target > nums[mid])</span><br><span class="line"> {</span><br><span class="line"> left = mid+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(target < nums[mid])</span><br><span class="line"> {</span><br><span class="line"> right = mid-<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> ans = mid;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(nums[mid] <nums[right]){</span><br><span class="line"> <span class="keyword">if</span>(target<nums[mid])</span><br><span class="line"> {</span><br><span class="line"> left=mid+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(target > nums[mid])</span><br><span class="line"> {</span><br><span class="line"> right=mid-<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> ans=mid;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="剑指-Offer-II-069-山峰数组的顶部"><a href="#剑指-Offer-II-069-山峰数组的顶部" class="headerlink" title="剑指 Offer II 069. 山峰数组的顶部"></a><a href="https://leetcode-cn.com/problems/B1IidL/">剑指 Offer II 069. 山峰数组的顶部</a></h4><p>题目</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">符合下列属性的数组 arr 称为 山峰数组(山脉数组) :</span><br><span class="line"></span><br><span class="line">arr.length >= 3</span><br><span class="line">存在 i(0 < i < arr.length - 1)使得:</span><br><span class="line">arr[0] < arr[1] < ... arr[i-1] < arr[i]</span><br><span class="line">arr[i] > arr[i+1] > ... > arr[arr.length - 1]</span><br><span class="line">给定由整数组成的山峰数组 arr ,返回任何满足 arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 的下标 i ,即山峰顶部。</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">示例 1:</span><br><span class="line"></span><br><span class="line">输入:arr = [0,1,0]</span><br><span class="line">输出:1</span><br><span class="line">示例 2:</span><br><span class="line"></span><br><span class="line">输入:arr = [1,3,5,4,2]</span><br><span class="line">输出:2</span><br><span class="line">示例 3:</span><br><span class="line"></span><br><span class="line">输入:arr = [0,10,5,2]</span><br><span class="line">输出:1</span><br><span class="line">示例 4:</span><br><span class="line"></span><br><span class="line">输入:arr = [3,4,5,1]</span><br><span class="line">输出:2</span><br><span class="line">示例 5:</span><br><span class="line"></span><br><span class="line">输入:arr = [24,69,100,99,79,78,67,36,26,19]</span><br><span class="line">输出:2</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">提示:</span><br><span class="line"></span><br><span class="line">3 <= arr.length <= 104</span><br><span class="line">0 <= arr[i] <= 106</span><br><span class="line">题目数据保证 arr 是一个山脉数组</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">进阶:很容易想到时间复杂度 O(n) 的解决方案,你可以设计一个 O(log(n)) 的解决方案吗?</span><br></pre></td></tr></table></figure><p>我写的想法 第一次就想到for循环遍历 写出了O(n)的学法</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">peakIndexInMountainArray</span><span class="params">(<span class="keyword">int</span>* arr, <span class="keyword">int</span> arrSize)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> temp=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(;temp<arrSize-<span class="number">1</span>;temp++){</span><br><span class="line"> <span class="keyword">if</span>(arr[temp]>arr[temp+<span class="number">1</span>])</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> temp;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>三叶姐姐的解法</p><p>二分<br>往常我们使用「二分」进行查值,需要确保序列本身满足「二段性」:当选定一个端点(基准值)后,结合「一段满足 & 另一段不满足」的特性来实现“折半”的查找效果。</p><p>但本题求的是峰顶索引值,如果我们选定数组头部或者尾部元素,其实无法根据大小关系“直接”将数组分成两段。</p><p>但可以利用题目发现如下性质:由于 arr 数值各不相同,因此峰顶元素左侧必然满足严格单调递增,峰顶元素右侧必然不满足。</p><p>因此 以峰顶元素为分割点的 arr 数组,根据与 前一元素/后一元素 的大小关系,具有二段性:</p><p>峰顶元素左侧满足 arr[i-1] < arr[i]arr[i−1]<arr[i] 性质,右侧不满足<br>峰顶元素右侧满足 arr[i] > arr[i+1]arr[i]>arr[i+1] 性质,左侧不满足<br>因此我们可以选择任意条件,写出若干「二分」版本。</p><p>代码:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="comment">// 根据 arr[i-1] < arr[i] 在 [1,n-1] 范围内找值</span></span><br><span class="line"> <span class="comment">// 峰顶元素为符合条件的最靠近中心的元素</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">peakIndexInMountainArray</span><span class="params">(<span class="keyword">int</span>[] arr)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = arr.length;</span><br><span class="line"> <span class="keyword">int</span> l = <span class="number">1</span>, r = n - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (l < r) {</span><br><span class="line"> <span class="keyword">int</span> mid = l + r + <span class="number">1</span> >> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span> (arr[mid - <span class="number">1</span>] < arr[mid]) l = mid;</span><br><span class="line"> <span class="keyword">else</span> r = mid - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> r;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li><p>时间复杂度:O(\log{n})<em>O</em>(log<em>n</em>)</p></li><li><p>空间复杂度:O(1)<em>O</em>(1)</p></li><li><p>三分和k分</p></li><li><p>```java<br>class Solution {</p><pre><code>public int peakIndexInMountainArray(int[] arr) { int n = arr.length; int l = 0, r = n - 1; while (l < r) { int m1 = l + (r - l) / 3; int m2 = r - (r - l) / 3; if (arr[m1] > arr[m2]) r = m2 - 1; else l = m1 + 1; } return r;}</code></pre><p>}</p><p>作者:AC_OIer<br>链接:<a href="https://leetcode-cn.com/problems/B1IidL/solution/gong-shui-san-xie-er-fen-san-fen-ji-zhi-lc8zl/">https://leetcode-cn.com/problems/B1IidL/solution/gong-shui-san-xie-er-fen-san-fen-ji-zhi-lc8zl/</a><br>来源:力扣(LeetCode)<br>著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">题干</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>秋天快到啦,天气慢慢凉爽了下来,所以实验室要组织去骊山进行一次野餐活动。</p><p>最底层的Lofipure被迫背背包给大家装各种零食,但是实验室的大佬们并不打算轻易放过Lofipure,他们打算把Lofipure的背包装的尽量满</p><p>现在知道Lofipure的背包容量为 V(正整数,0 <= V <= 20000),同时有 n 件小零食(0<n<=30),每个小零食的重量。<br>现在在 n 个小零食中,任取若干个装入Lofipure的背包内,使得Lofipure背包的剩余空间为最小。借此达到压榨Lofipure的目的。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Input</span><br><span class="line"></span><br><span class="line">输入:一个整数v,表示背包容量 一个整数n,表示有n个物品 接下来 n 个整数,分别表示这 n 个物品的各自体积</span><br><span class="line"></span><br><span class="line">Output</span><br><span class="line"></span><br><span class="line">输出:一个整数,表示背包最小的剩余空间</span><br><span class="line"></span><br><span class="line">Sample Input</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>24<br>6<br>8<br>3<br>12<br>7<br>9<br>7</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Sample Output</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>0</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">看到这道理的第一时刻想的是暴力枚举出所有零食混合装的重量 可复杂度太高 遂放弃。</span><br><span class="line"></span><br><span class="line">然后看了一下大佬们的代码</span><br><span class="line"></span><br><span class="line">```c++</span><br><span class="line">#include<bits/stdc++.h></span><br><span class="line">#define int long long</span><br><span class="line">#define MAXN 1000005</span><br><span class="line">using namespace std;</span><br><span class="line">int dp[20005];</span><br><span class="line">signed main()</span><br><span class="line">{</span><br><span class="line"> dp[0]=1;</span><br><span class="line"> int v,n;cin>>v>>n;</span><br><span class="line"> for(int i=1;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> int t;cin>>t;</span><br><span class="line"> for(int j=v;j>=t;j--)</span><br><span class="line"> dp[j]|=dp[j-t];</span><br><span class="line"> }</span><br><span class="line"> for(int i=v;i>=0;i--)</span><br><span class="line"> {</span><br><span class="line"> if(dp[i])</span><br><span class="line"> {</span><br><span class="line"> cout<<v-i<<endl;</span><br><span class="line"> return 0;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>刚开始我用Java实现的时候,</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">后面经过自己 ~~人眼比对~~ 发现这个错误 发现之后感到不解。</span><br><span class="line"></span><br><span class="line">```dp [j]|=dp[j-t]```这个式子用来干嘛的呢</span><br><span class="line"></span><br><span class="line">后面经过输出</span><br><span class="line"></span><br><span class="line">```java</span><br><span class="line">System.out.println(j+" "+(j-t)+" "+dp[j]+" "+dp[j - t]);</span><br></pre></td></tr></table></figure><p>形式一下子就清楚了 dp是为了统计零食能够组成的重量</p><p>拿本题例子来说 </p><p>输入 t = 8 时 只有dp[8] = dp[8] | dp[0] 才变成1 这代表着能够组成8的重量</p><p>输入 t = 3 时 有了dp[11] = dp[11] | dp[11 - 3] dp[3] = dp[3] | dp[0] 这次增加 11(8+3) 3(3+0)l两种可能</p><p>输入t = 12时 有dp[23] = 1 ,dp[20] = 1 dp[15] =1 dp[12] = 1</p><p>输入t = 7时 有 dp[22] = 1,dp[22] = 1 dp[19] = 1 dp[18] = 1 dp[15] = 1 dp[10] = 1</p><p>。。。。</p><p>到最后会使所有能够凑出重量的dp数都为1</p><p>从最大的量递减便利 当dp[i] 不为0 时 也就是为1 代表着这是能够装载着最大的重量 直接输出V-i</p><p>感叹算法的奇妙与精彩,只恨自己接触晚与学校氛围不好,蹉跎了许久时光。</p><p>JAVA版代码</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.VG;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">C</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">int</span> V = scanner.nextInt();</span><br><span class="line"> <span class="keyword">int</span> N = scanner.nextInt();</span><br><span class="line"> <span class="keyword">int</span>[] dp = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">200005</span>];</span><br><span class="line"> dp[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= N; i++) {</span><br><span class="line"> <span class="keyword">int</span> t = scanner.nextInt();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = V; j >=t ; j--) {</span><br><span class="line"> dp[j] |= dp[j - t];</span><br><span class="line"> System.out.println(j+<span class="string">" "</span>+(j-t)+<span class="string">" "</span>+dp[j]+<span class="string">" "</span>+dp[j - t]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = V; i >= <span class="number">0</span> ; i--) {</span><br><span class="line"> <span class="keyword">if</span>(dp[i]!=<span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> System.out.println(V- i);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>快速幂</p><h4 id="50-Pow-x-n"><a href="#50-Pow-x-n" class="headerlink" title="50. Pow(x, n)"></a><a href="https://leetcode-cn.com/problems/powx-n/">50. Pow(x, n)</a></h4><p>难度中等8 </p><p>实现 <a href="https://www.cplusplus.com/reference/valarray/pow/">pow(<em>x</em>, <em>n</em>)</a> ,即计算 x 的 n 次幂函数(即,xn)。</p><p><strong>示例 1:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:x = 2.00000, n = 10</span><br><span class="line">输出:1024.00000</span><br></pre></td></tr></table></figure><p><strong>示例 2:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:x = 2.10000, n = 3</span><br><span class="line">输出:9.26100</span><br></pre></td></tr></table></figure><p><strong>示例 3:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">输入:x = 2.00000, n = -2</span><br><span class="line">输出:0.25000</span><br><span class="line">解释:2-2 = 1/22 = 1/4 = 0.25</span><br></pre></td></tr></table></figure><p><strong>提示:</strong></p><ul><li><code>-100.0 < x < 100.0</code></li><li><code>-231 <= n <= 231-1</code></li><li><code>-104 <= xn <= 104</code></li></ul><p>如果要计算2 的 1024 次方 我们用朴素算法要乘以1024次 算法复杂法O(1024) O(10)</p><p>如果每次都平方一下</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1->2->4->8->16->32->64->128->256->512->1024 </span><br></pre></td></tr></table></figure><p>时间复杂的为O(log 1024)也就是10</p><p><img src="https://s2.loli.net/2021/12/05/LwoEReQn2zqDHuJ.png"></p><p>算法板子</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">myPow</span><span class="params">(<span class="keyword">double</span> x, <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> N = n;</span><br><span class="line"> <span class="keyword">return</span> N >= <span class="number">0</span> ? quickMul(x, N) : <span class="number">1.0</span> / quickMul(x, -N);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">quickMul</span><span class="params">(<span class="keyword">double</span> x, <span class="keyword">long</span> N)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (N == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1.0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">double</span> y = quickMul(x, N / <span class="number">2</span>);</span><br><span class="line"> <span class="keyword">return</span> N % <span class="number">2</span> == <span class="number">0</span> ? y * y : y * y * x;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">myPow</span><span class="params">(<span class="keyword">double</span> x, <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> N = n;</span><br><span class="line"> <span class="keyword">double</span> ans = quickMul(x,N);</span><br><span class="line"> <span class="keyword">if</span>(N>=<span class="number">0</span>) <span class="keyword">return</span> ans;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> <span class="number">1.0</span>/ans;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">quickMul</span><span class="params">(<span class="keyword">double</span> x, <span class="keyword">long</span> N)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (N == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1.0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">double</span> y = quickMul(x, N / <span class="number">2</span>);</span><br><span class="line"> <span class="keyword">if</span>(N%<span class="number">2</span>==<span class="number">0</span>) <span class="keyword">return</span> y*y;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> y*y*x;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>进阶</p><h1 id="372-超级次方"><a href="#372-超级次方" class="headerlink" title="372. 超级次方"></a><a href="https://leetcode-cn.com/problems/super-pow/">372. 超级次方</a></h1><p>难度 中等</p><p>你的任务是计算 <code>ab</code> 对 <code>1337</code> 取模,<code>a</code> 是一个正整数,<code>b</code> 是一个非常大的正整数且会以数组形式给出。</p><p><strong>示例 1:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:a = 2, b = [3]</span><br><span class="line">输出:8</span><br></pre></td></tr></table></figure><p><strong>示例 2:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:a = 2, b = [1,0]</span><br><span class="line">输出:1024</span><br></pre></td></tr></table></figure><p><strong>示例 3:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:a = 1, b = [4,3,3,8,5,2]</span><br><span class="line">输出:1</span><br></pre></td></tr></table></figure><p><strong>示例 4:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:a = 2147483647, b = [2,0,0]</span><br><span class="line">输出:1198</span><br></pre></td></tr></table></figure><p><strong>提示:</strong></p><ul><li><code>1 <= a <= 231 - 1</code></li><li><code>1 <= b.length <= 2000</code></li><li><code>0 <= b[i] <= 9</code></li><li><code>b</code> 不含前导 0</li></ul><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MOD = <span class="number">1337</span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">superPow</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span>[] b)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = b.length - <span class="number">1</span>; i >= <span class="number">0</span>; --i) {</span><br><span class="line"> ans = (<span class="keyword">int</span>) ((<span class="keyword">long</span>) ans * pow(a, b[i]) % MOD);</span><br><span class="line"> a = pow(a, <span class="number">10</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">pow</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (n != <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">if</span> (n % <span class="number">2</span> != <span class="number">0</span>) {</span><br><span class="line"> res = (<span class="keyword">int</span>) ((<span class="keyword">long</span>) res * x % MOD);</span><br><span class="line"> }</span><br><span class="line"> x = (<span class="keyword">int</span>) ((<span class="keyword">long</span>) x * x % MOD);</span><br><span class="line"> n /= <span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure><p>作者:LeetCode-Solution<br>链接:<a href="https://leetcode-cn.com/problems/super-pow/solution/chao-ji-ci-fang-by-leetcode-solution-ow8j/">https://leetcode-cn.com/problems/super-pow/solution/chao-ji-ci-fang-by-leetcode-solution-ow8j/</a></p></li></ul><h1 id="回溯问题"><a href="#回溯问题" class="headerlink" title="回溯问题"></a>回溯问题</h1><p>解决一个回溯问题,实际上就是一个决策树的遍历过程,需要思考三个问题</p><ul><li>路径:也就是已经做出的选择</li><li>选择列表:也就是你当前可以做的选择</li><li>也就是到达策树底层,无法再做选择的条件。</li></ul><p>回溯算法框架</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">result = []</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">backtrack</span>(<span class="params">路径, 选择列表</span>):</span></span><br><span class="line"> <span class="keyword">if</span> 满足结束条件:</span><br><span class="line"> result.add(路径)</span><br><span class="line"> <span class="keyword">return</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span> 选择 <span class="keyword">in</span> 选择列表:</span><br><span class="line"> 做选择</span><br><span class="line"> backtrack(路径, 选择列表)</span><br><span class="line"> 撤销选择</span><br></pre></td></tr></table></figure><p>核心在于for循环里面的递归,在递归调用之前做选择,在递归调用之后撤销选择。</p><h2 id="例子1-全排列问题"><a href="#例子1-全排列问题" class="headerlink" title="例子1 全排列问题"></a>例子1 全排列问题</h2><p>有n个数 每个数都只能用一次 求出所有能排列的可能性。全排列个数为n!个</p><p>PS:<strong>为了简单清晰起见,我们这次讨论的全排列问题不包含重复的数字</strong>。</p><p>如果已知有多少个数的情况下,我们通常可以使用n层for循环暴力遍历所有数</p><p>例如3个数 我们可以暴力写法为</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i < n;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>;j < n;j++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>;k < n;k++)</span><br><span class="line"> {</span><br><span class="line"><span class="keyword">if</span>(i!=j && i!=k && j!=k)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 1 2 3 1 3 2 2 1 3 2 3 1 3 2 1 3 1 2</span></span><br><span class="line">list.add(nums[i]);</span><br><span class="line"> list.add(nums[j]);</span><br><span class="line"> list.add(nums[k]);</span><br><span class="line"> res.add(list);</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>回溯数如下图所示,只要从根遍历这棵树,记录路径下的数字,其实就是所有的全排列,我们不妨把这棵树称之为回溯算法的<strong>决策树</strong></p><p>为什么叫决策树呢,顾名思义,每次遍历的时候都需要做决策来去避开那些已经走过的路。</p><p><img src="https://s2.loli.net/2022/04/02/OGgqp9doCzDSvcW.png"></p><p>现在可以解答开头的几个名词:[2] 就是「路径」,记录你已经做过的选择;[1,3] 就是「选择列表」,表示你当前可以做出的选择;「结束条件」就是遍历到树的底层,在这里就是选择列表为空的时候。</p><p>如果明白了这几个名词,可以把「路径」和「选择」列表作为决策树上每个节点的属性,比如下图列出了几个节点的属性:</p><p><img src="https://s2.loli.net/2022/04/02/JLtS3YvCMZBa4rx.png"></p><p>我们定义的backtrack函数就是指针一样,在这棵树上游走,同时要正确维护每个节点的属性,每当走到树的底层,其路径就是一个全排列。</p><p>再进一步,如何遍历一颗树,</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">traverse</span><span class="params">(TreeNode root)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span> (TreeNode child : root.childern)</span><br><span class="line"> <span class="comment">// 前序遍历需要的操作</span></span><br><span class="line"> traverse(child);</span><br><span class="line"> <span class="comment">// 后序遍历需要的操作</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src="https://s2.loli.net/2022/04/02/wMfcCt31OVvUx5L.png"></p><p>前序遍历的代码在进入某一个节点之前的那个时间点执行,后序遍历代码在离开某个节点之后的那个时间点执行。</p><p>回想我们刚才说的,「路径」和「选择」是每个节点的属性,函数在树上游走要正确维护节点的属性,那么就要在这两个特殊时间点搞点动作:</p><p><img src="https://s2.loli.net/2022/04/02/XBxvT5IMA1Odk7P.png"></p><p>回溯代码核心框架</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> 选择 <span class="keyword">in</span> 选择列表:</span><br><span class="line"> <span class="comment"># 做选择</span></span><br><span class="line"> 将该选择从选择列表移除</span><br><span class="line"> 路径.add(选择)</span><br><span class="line"> backtrack(路径, 选择列表)</span><br><span class="line"> <span class="comment"># 撤销选择</span></span><br><span class="line"> 路径.remove(选择)</span><br><span class="line"> 将该选择再加入选择列表</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>我们只要在递归之前做出选择,在递归之后撤销自己的选择,就能得到每个节点的选择路径和列表。</p><h1 id="全排列代码"><a href="#全排列代码" class="headerlink" title="全排列代码"></a>全排列代码</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">List<List<Integer>> res = <span class="keyword">new</span> LinkedList<>();</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 主函数,输入一组不重复的数字,返回它们的全排列 */</span></span><br><span class="line">List<List<Integer>> permute(<span class="keyword">int</span>[] nums) {</span><br><span class="line"> <span class="comment">// 记录「路径」</span></span><br><span class="line"> LinkedList<Integer> track = <span class="keyword">new</span> LinkedList<>();</span><br><span class="line"> backtrack(nums, track);</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 路径:记录在 track 中</span></span><br><span class="line"><span class="comment">// 选择列表:nums 中不存在于 track 的那些元素</span></span><br><span class="line"><span class="comment">// 结束条件:nums 中的元素全都在 track 中出现</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">backtrack</span><span class="params">(<span class="keyword">int</span>[] nums, LinkedList<Integer> track)</span> </span>{</span><br><span class="line"> <span class="comment">// 触发结束条件</span></span><br><span class="line"> <span class="keyword">if</span> (track.size() == nums.length) {</span><br><span class="line"> res.add(<span class="keyword">new</span> LinkedList(track));</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < nums.length; i++) {</span><br><span class="line"> <span class="comment">// 排除不合法的选择</span></span><br><span class="line"> <span class="keyword">if</span> (track.contains(nums[i]))</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> <span class="comment">// 做选择</span></span><br><span class="line"> track.add(nums[i]);</span><br><span class="line"> <span class="comment">// 进入下一层决策树</span></span><br><span class="line"> backtrack(nums, track);</span><br><span class="line"> <span class="comment">// 取消选择</span></span><br><span class="line"> track.removeLast();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>我们这里稍微做了些变通,没有显式记录「选择列表」,而是通过 <code>nums</code> 和 <code>track</code> 推导出当前的选择列表:</p><p>通过contains函数来判断该数是否已经被使用。</p><p><img src="https://s2.loli.net/2022/04/02/kYzE8UDyHFMSq1K.png"></p><p>至此,我们就通过全排列问题详解了回溯算法的底层原理。当然,这个算法解决全排列不是很高效,应为对链表使用 contains 方法需要 O(N) 的时间复杂度。有更好的方法通过交换元素达到目的,但是难理解一些,这里就不写了,有兴趣可以自行搜索一下。</p><p>但是必须说明的是,不管怎么优化,都符合回溯框架,而且时间复杂度都不可能低于 O(N!),因为穷举整棵决策树是无法避免的。这也是回溯算法的一个特点,不像动态规划存在重叠子问题可以优化,回溯算法就是纯暴力穷举,复杂度一般都很高。</p><p>明白了全排列问题,就可以直接套回溯算法框架了,下面简单看看 N 皇后问题。</p><h1 id="例子2-N皇后问题"><a href="#例子2-N皇后问题" class="headerlink" title="例子2 N皇后问题"></a>例子2 N皇后问题</h1><h3 id="题意"><a href="#题意" class="headerlink" title="题意"></a>题意</h3><p>给你一个N*N的棋盘,,让你放置 N 个皇后,使得它们不能互相攻击。</p><p>PS:皇后可以攻击同一行、同一列、左上左下右上右下四个方向的任意单位。</p><p>这个问题本质上跟全排列问题差不多,决策树的每一层表示棋盘上的每一行;每个节点可以做出的选择是,在该行的任意一列放置一个皇后。</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">vector<vector<string>> res;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 输入棋盘边长 n,返回所有合法的放置 */</span></span><br><span class="line">vector<vector<string>> <span class="built_in">solveNQueens</span>(<span class="keyword">int</span> n) {</span><br><span class="line"> <span class="comment">// '.' 表示空,'Q' 表示皇后,初始化空棋盘。</span></span><br><span class="line"> <span class="function">vector<string> <span class="title">board</span><span class="params">(n, string(n, <span class="string">'.'</span>))</span></span>;</span><br><span class="line"> <span class="built_in">backtrack</span>(board, <span class="number">0</span>);</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 路径:board 中小于 row 的那些行都已经成功放置了皇后</span></span><br><span class="line"><span class="comment">// 选择列表:第 row 行的所有列都是放置皇后的选择</span></span><br><span class="line"><span class="comment">// 结束条件:row 超过 board 的最后一行</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">backtrack</span><span class="params">(vector<string>& board, <span class="keyword">int</span> row)</span> </span>{</span><br><span class="line"> <span class="comment">// 触发结束条件</span></span><br><span class="line"> <span class="keyword">if</span> (row == board.<span class="built_in">size</span>()) {</span><br><span class="line"> res.<span class="built_in">push_back</span>(board);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">int</span> n = board[row].<span class="built_in">size</span>();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> col = <span class="number">0</span>; col < n; col++) {</span><br><span class="line"> <span class="comment">// 排除不合法选择</span></span><br><span class="line"> <span class="keyword">if</span> (!<span class="built_in">isValid</span>(board, row, col)) </span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> <span class="comment">// 做选择</span></span><br><span class="line"> board[row][col] = <span class="string">'Q'</span>;</span><br><span class="line"> <span class="comment">// 进入下一行决策</span></span><br><span class="line"> <span class="built_in">backtrack</span>(board, row + <span class="number">1</span>);</span><br><span class="line"> <span class="comment">// 撤销选择</span></span><br><span class="line"> board[row][col] = <span class="string">'.'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>IsValid()函数</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 是否可以在 board[row][col] 放置皇后? */</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isValid</span><span class="params">(vector<string>& board, <span class="keyword">int</span> row, <span class="keyword">int</span> col)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = board.<span class="built_in">size</span>();</span><br><span class="line"> <span class="comment">// 检查列是否有皇后互相冲突</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> <span class="keyword">if</span> (board[i][col] == <span class="string">'Q'</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 检查右上方是否有皇后互相冲突</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = row - <span class="number">1</span>, j = col + <span class="number">1</span>; </span><br><span class="line"> i >= <span class="number">0</span> && j < n; i--, j++) {</span><br><span class="line"> <span class="keyword">if</span> (board[i][j] == <span class="string">'Q'</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 检查左上方是否有皇后互相冲突</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = row - <span class="number">1</span>, j = col - <span class="number">1</span>;</span><br><span class="line"> i >= <span class="number">0</span> && j >= <span class="number">0</span>; i--, j--) {</span><br><span class="line"> <span class="keyword">if</span> (board[i][j] == <span class="string">'Q'</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>函数 backtrack 依然像个在决策树上游走的指针,通过 row 和 col 就可以表示函数遍历到的位置,通过 isValid 函数可以将不符合条件的情况剪枝:</p><p><img src="https://s2.loli.net/2022/04/02/qryPswAifQe1UvN.png"></p><p>某种程度上说,动态规划的暴力求解阶段就是回溯算法。只是有的问题具有重叠子问题性质,可以用 dp table 或者备忘录优化,将递归树大幅剪枝,这就变成了动态规划。而今天的两个问题,都没有重叠子问题,也就是回溯算法问题了,复杂度非常高是不可避免的。</p><p>参考链接:<a href="https://leetcode-cn.com/problems/permutations/solution/hui-su-suan-fa-xiang-jie-by-labuladong-2/">https://leetcode-cn.com/problems/permutations/solution/hui-su-suan-fa-xiang-jie-by-labuladong-2/</a></p><h1 id="26-删除有序数组中的重复项"><a href="#26-删除有序数组中的重复项" class="headerlink" title="26. 删除有序数组中的重复项"></a><a href="https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/">26. 删除有序数组中的重复项</a></h1><p>这题典型的双指针 一个指针是用来遍历 另外一个用来存储</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">removeDuplicates</span><span class="params">(<span class="keyword">int</span>[] nums)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> t = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < nums.length; i ++ ) {</span><br><span class="line"> <span class="keyword">if</span> (i == <span class="number">0</span> || nums[i] != nums[i - <span class="number">1</span>]) </span><br><span class="line"> {</span><br><span class="line"> nums[t] = nums[i];</span><br><span class="line"> t++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> t;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p> i用来遍历数组 而t用来存储新的数组</p><p>通用解法拓展</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">removeDuplicates</span><span class="params">(<span class="keyword">int</span>[] nums)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = nums.length;</span><br><span class="line"> <span class="keyword">if</span> (n <= <span class="number">1</span>) <span class="keyword">return</span> n; </span><br><span class="line"> <span class="keyword">return</span> process(nums, <span class="number">1</span>, nums[n - <span class="number">1</span>]);</span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">process</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> k, <span class="keyword">int</span> max)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> idx = <span class="number">0</span>; </span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> x : nums) {</span><br><span class="line"> <span class="keyword">if</span> (idx < k || nums[idx - k] != x) nums[idx++] = x;</span><br><span class="line"> <span class="keyword">if</span> (idx - k >= <span class="number">0</span> && nums[idx - k] == max) <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> idx;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">作者:AC_OIer</span><br><span class="line">链接:https:<span class="comment">//leetcode-cn.com/problems/remove-duplicates-from-sorted-array/solution/shua-chuan-lc-jian-ji-shuang-zhi-zhen-ji-2eg8/</span></span><br><span class="line">来源:力扣(LeetCode)</span><br><span class="line">著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。</span><br></pre></td></tr></table></figure><h1 id="507-完美数"><a href="#507-完美数" class="headerlink" title="507. 完美数"></a><a href="https://leetcode-cn.com/problems/perfect-number/">507. 完美数</a></h1><p>两种方法</p><p>方法一 打表 </p><p><img src="https://s2.loli.net/2021/12/31/tD6SVr2EosC3KBj.png"></p><p>我试了一下我的打表方式 20分钟还没打出最后一个数 </p><p>直接搬官方的吧</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">checkPerfectNumber</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> num == <span class="number">6</span> || num == <span class="number">28</span> || num == <span class="number">496</span> || num == <span class="number">8128</span> || num == <span class="number">33550336</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p> 方法二 枚举</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">checkPerfectNumber</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(num==<span class="number">1</span>) <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>;i <= num/<span class="number">2</span>;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(num%i==<span class="number">0</span>)</span><br><span class="line"> sum+=i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> sum==num;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>这个稍微时间稍微长了一点</p><p> </p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">checkPerfectNumber</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (num == <span class="number">1</span>) <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i <= num / i; i++) {</span><br><span class="line"> <span class="keyword">if</span> (num % i == <span class="number">0</span>) {</span><br><span class="line"> ans += i;</span><br><span class="line"> <span class="keyword">if</span> (i * i != num) ans += num / i;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans == num;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>这个就很快了 </p><p>同样是枚举 别人写的和我写的 感觉自己在时间复杂度方法还得加强</p><p><img src="https://s2.loli.net/2021/12/31/fKldBcVxSY9JAo1.png"></p><h1 id="20-有效的括号"><a href="#20-有效的括号" class="headerlink" title="20. 有效的括号"></a><a href="https://leetcode-cn.com/problems/valid-parentheses/">20. 有效的括号</a></h1><p>给定一个只包括 ‘(‘,’)’,’{‘,’}’,’[‘,’]’ 的字符串 s ,判断字符串是否有效。</p><p>有效字符串需满足:</p><p>左括号必须用相同类型的右括号闭合。<br>左括号必须以正确的顺序闭合。</p><p>示例 1:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:s = "()"</span><br><span class="line">输出:true</span><br></pre></td></tr></table></figure><p>示例 2:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:s = "()[]{}"</span><br><span class="line">输出:true</span><br></pre></td></tr></table></figure><p>示例 3:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:s = "(]"</span><br><span class="line">输出:false</span><br></pre></td></tr></table></figure><p>示例 4:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:s = "([)]"</span><br><span class="line">输出:false</span><br></pre></td></tr></table></figure><p>示例 5:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:s = "{[]}"</span><br><span class="line">输出:true</span><br></pre></td></tr></table></figure><p>提示:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1 <= s.length <= 104</span><br><span class="line">s 仅由括号 '()[]{}' 组成</span><br></pre></td></tr></table></figure><p>题解</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isValid</span><span class="params">(String s)</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> n = s.length();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (n % <span class="number">2</span> == <span class="number">1</span>) {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> Map<Character, Character> pairs = <span class="keyword">new</span> HashMap<Character, Character>() {{</span><br><span class="line"></span><br><span class="line"> put(<span class="string">')'</span>, <span class="string">'('</span>);</span><br><span class="line"></span><br><span class="line"> put(<span class="string">']'</span>, <span class="string">'['</span>);</span><br><span class="line"></span><br><span class="line"> put(<span class="string">'}'</span>, <span class="string">'{'</span>);</span><br><span class="line"></span><br><span class="line"> }};</span><br><span class="line"></span><br><span class="line"> Deque<Character> stack = <span class="keyword">new</span> LinkedList<Character>();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">char</span> ch = s.charAt(i);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (pairs.containsKey(ch)) {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (stack.isEmpty() || stack.peek() != pairs.get(ch)) {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> stack.pop();</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"></span><br><span class="line"> stack.push(ch);</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> stack.isEmpty();</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>先就s字符串的长度奇数还是偶数进行判断</p><p>如果长度是<strong>奇数</strong> 直接返回false 因为不满足闭合条件</p><h1 id="367-有效的完全平方数"><a href="#367-有效的完全平方数" class="headerlink" title="367. 有效的完全平方数"></a><a href="https://leetcode-cn.com/problems/valid-perfect-square/">367. 有效的完全平方数</a></h1><p>第一次直接想的就是暴力遍历</p><p> </p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isPerfectSquare</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span> ;i <= num/<span class="number">2</span> ; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(i*i == num)</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">if</span>(num == <span class="number">1</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>样例是全过了 但是超时了</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isPerfectSquare</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span> ;i <= num/<span class="number">2</span> ; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(num / i == i && num % i == <span class="number">0</span>) </span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> <span class="keyword">if</span> (num / i < i) <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">if</span>(num == <span class="number">1</span> ||num==<span class="number">4</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>后面参考评论区</p><p>速度是以除代乘 速度大大提升。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isPerfectSquare</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = num / <span class="number">2</span> + <span class="number">1</span>;i >= <span class="number">1</span>; i--){</span><br><span class="line"> <span class="keyword">if</span>(num == i * i) <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>这是第一版的改进 从后面开始遍历 提升下速率 数越大越明显</p><p>二分法</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isPerfectSquare</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> low = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> high = num;</span><br><span class="line"> <span class="keyword">while</span>(low <= high){</span><br><span class="line"> <span class="keyword">int</span> mid = low + (high - low) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">int</span> t = num / mid;</span><br><span class="line"> <span class="keyword">if</span>(t == mid){</span><br><span class="line"> <span class="keyword">if</span>(num % mid==<span class="number">0</span>) <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> low = mid+<span class="number">1</span>;}</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(t<mid)</span><br><span class="line"> {</span><br><span class="line"> high = mid-<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span>{</span><br><span class="line"> low = mid+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>数学方法</p><p>所有平方数都是可以由奇数和构成的</p><p>1=1 4=1+3 9=1+3+5</p><p>则只需要每次从1开始减 每次加2 以num>0为判断条件</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isPerfectSquare</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> ans=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(num > <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> num -= ans;</span><br><span class="line"> ans += <span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> num==<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>在一个 平衡字符串 中,’L’ 和 ‘R’ 字符的数量是相同的。</p><p>给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。</p><p>注意:分割得到的每个字符串都必须是平衡字符串,且分割得到的平衡字符串是原平衡字符串的连续子串。</p><p>返回可以通过分割得到的平衡字符串的 最大数量 。</p><p>示例 1:</p><p>输入:s = “RLRRLLRLRL”<br>输出:4<br>解释:s 可以分割为 “RL”、”RRLL”、”RL”、”RL” ,每个子字符串中都包含相同数量的 ‘L’ 和 ‘R’ 。<br>示例 2:</p><p>输入:s = “RLLLLRRRLR”<br>输出:3<br>解释:s 可以分割为 “RL”、”LLLRRR”、”LR” ,每个子字符串中都包含相同数量的 ‘L’ 和 ‘R’ 。<br>示例 3:</p><p>输入:s = “LLLLRRRR”<br>输出:1<br>解释:s 只能保持原样 “LLLLRRRR”.<br>示例 4:</p><p>输入:s = “RLRRRLLRLL”<br>输出:2<br>解释:s 可以分割为 “RL”、”RRRLLRLL” ,每个子字符串中都包含相同数量的 ‘L’ 和 ‘R’ 。</p><p>提示:</p><p>1 <= s.length <= 1000<br>s[i] = ‘L’ 或 ‘R’<br>s 是一个 平衡 字符串</p><h1 id="1221-分割平衡字符串"><a href="#1221-分割平衡字符串" class="headerlink" title="1221. 分割平衡字符串"></a><a href="https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/">1221. 分割平衡字符串</a></h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">在一个 平衡字符串 中,'L' 和 'R' 字符的数量是相同的。</span><br><span class="line"></span><br><span class="line">给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。</span><br><span class="line"></span><br><span class="line">注意:分割得到的每个字符串都必须是平衡字符串,且分割得到的平衡字符串是原平衡字符串的连续子串。</span><br><span class="line"></span><br><span class="line">返回可以通过分割得到的平衡字符串的 最大数量 。</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">示例 1:</span><br><span class="line"></span><br><span class="line">输入:s = "RLRRLLRLRL"</span><br><span class="line">输出:4</span><br><span class="line">解释:s 可以分割为 "RL"、"RRLL"、"RL"、"RL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。</span><br><span class="line">示例 2:</span><br><span class="line"></span><br><span class="line">输入:s = "RLLLLRRRLR"</span><br><span class="line">输出:3</span><br><span class="line">解释:s 可以分割为 "RL"、"LLLRRR"、"LR" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。</span><br><span class="line">示例 3:</span><br><span class="line"></span><br><span class="line">输入:s = "LLLLRRRR"</span><br><span class="line">输出:1</span><br><span class="line">解释:s 只能保持原样 "LLLLRRRR".</span><br><span class="line">示例 4:</span><br><span class="line"></span><br><span class="line">输入:s = "RLRRRLLRLL"</span><br><span class="line">输出:2</span><br><span class="line">解释:s 可以分割为 "RL"、"RRRLLRLL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">提示:</span><br><span class="line"></span><br><span class="line">1 <= s.length <= 1000</span><br><span class="line">s[i] = 'L' 或 'R'</span><br><span class="line">s 是一个 平衡 字符串</span><br></pre></td></tr></table></figure><p>题解</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">balancedStringSplit</span><span class="params">(String s)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = s.length();</span><br><span class="line"> <span class="keyword">int</span> j=<span class="number">0</span>,ans=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span> ; i < n ; i++ )</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span> (s.charAt(i)==<span class="string">'R'</span>)</span><br><span class="line"> {</span><br><span class="line"> j++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (s.charAt(i)==<span class="string">'L'</span>)</span><br><span class="line"> {</span><br><span class="line"> j--;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(j==<span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> ans++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="453-最小操作次数使数组元素相等"><a href="#453-最小操作次数使数组元素相等" class="headerlink" title="453. 最小操作次数使数组元素相等"></a><a href="https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/">453. 最小操作次数使数组元素相等</a></h1><p>给你一个长度为 <code>n</code> 的整数数组,每次操作将会使 <code>n - 1</code> 个元素增加 <code>1</code> 。返回让数组所有元素相等的最小操作次数。</p><p><strong>示例 1:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">输入:nums = [1,2,3]</span><br><span class="line">输出:3</span><br><span class="line">解释:</span><br><span class="line">只需要3次操作(注意每次操作会增加两个元素的值):</span><br><span class="line">[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]</span><br><span class="line"></span><br></pre></td></tr></table></figure><p><strong>示例 2:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入:nums = [1,1,1]</span><br><span class="line">输出:0</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure><p>解析</p><p>每一步除了最大的那个数不加1 相等于最大的数减1 参考相对运动理解</p><p>每一次操作总和都会-1</p><p>那么最后的结果肯定是n个最小数</p><p>那么</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">return sum - min * n;</span><br></pre></td></tr></table></figure><p>即可得到结果</p><p>利用for each语句遍历数组</p><p>找出数组里面最小的数和总和sum</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i : nums) {</span><br><span class="line"> min = Math.min(min, i);</span><br><span class="line"> sum += i;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>题解</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">minMoves</span><span class="params">(<span class="keyword">int</span>[] nums)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = nums.length;</span><br><span class="line"> <span class="keyword">int</span> min = nums[<span class="number">0</span>], sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i : nums) {</span><br><span class="line"> min = Math.min(min, i);</span><br><span class="line"> sum += i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> sum - min * n;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><h1 id="二分算法"><a href="#二分算法" class="headerlink" title="二分算法"></a>二分算法</h1><p>二分算法基本框架</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">searchInsert</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>&#123;</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>,right=nums.length-<span class="number">1</span>,ans=right+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(left&lt;=right)&#123;</span><br><span class="line"> <span class="keyword">int</span> mid = (left + right)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(nums[mid]&gt;=target)&#123;</span><br><span class="line"> ans=mid;</span><br><span class="line"> right=mid-<span class="number">1</span>;&#125;</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> left=mid+<span class="number">1</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>分析二分查找的一个技巧是:不要出现 else,而是把所有情况用 else if 写清楚,这样可以清楚地展现所有细节。</strong>本文都会使用 else if,旨在讲清楚,读者理解后可自行简化。</p></summary>
<category term="算法" scheme="http://example.com/tags/%E7%AE%97%E6%B3%95/"/>
</entry>
<entry>
<title>爬取强智科技教务系统—以湖南工业大学为例</title>
<link href="http://example.com/2023/01/11/%E7%88%AC%E5%8F%96%E5%BC%BA%E5%88%B6%E7%A7%91%E6%8A%80%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F%E2%80%94%E4%BB%A5%E6%B9%96%E5%8D%97%E5%B7%A5%E4%B8%9A%E5%A4%A7%E5%AD%A6%E4%B8%BA%E4%BE%8B/"/>
<id>http://example.com/2023/01/11/%E7%88%AC%E5%8F%96%E5%BC%BA%E5%88%B6%E7%A7%91%E6%8A%80%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F%E2%80%94%E4%BB%A5%E6%B9%96%E5%8D%97%E5%B7%A5%E4%B8%9A%E5%A4%A7%E5%AD%A6%E4%B8%BA%E4%BE%8B/</id>
<published>2023-01-11T06:57:26.000Z</published>
<updated>2023-09-26T13:07:49.158Z</updated>
<content type="html"><![CDATA[<p> 写这个已经过了很久的的时间了 当时写好没写博客是因为怕这个有额外的问题,现在经过实验 表现的都还没不错 在10min内成绩提醒就会发送到你自己指定的邮箱 我会把代码开源放入Github,当然我也会隐去我自己的信息。</p><p>这篇博客也会教大家怎么使用他</p><span id="more"></span><h3 id="准备"><a href="#准备" class="headerlink" title="准备"></a>准备</h3><p>IDEA+springboot框架+Maven+jsoup+httpclient</p><p>首先去教务系统网页 新的教务系统有验证码验证 ,但是还好我以前收藏了一个不用验证码的网站</p><p>湖南工业大学教务系统网站 :<a href="http://218.75.197.123:83/jsxsd/![](https://s2.loli.net/2022/01/11/jQWEIPiNM3oJd1h.png)">http://218.75.197.123:83/jsxsd/![](https://s2.loli.net/2022/01/11/jQWEIPiNM3oJd1h.png)</a></p><p>按F12 然后找到network(网络这一项) 登陆系统</p><p><img src="https://s2.loli.net/2022/01/11/srBjHaApIo8iDyV.png"></p><p>找到这一项 点开 <img src="https://s2.loli.net/2022/01/11/IBreibC61mT3AWM.png"></p><p>记录下这个的信息 userAccount即是学号 encodedd是学号+密码的base64加密</p><p>maven依赖</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">modelVersion</span>></span>4.0.0<span class="tag"></<span class="name">modelVersion</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">parent</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.springframework.boot<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-boot-starter-parent<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.6.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">relativePath</span>/></span> <span class="comment"><!-- lookup parent from repository --></span></span><br><span class="line"> <span class="tag"></<span class="name">parent</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.example<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>JWXT<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>0.0.1-SNAPSHOT<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">name</span>></span>JWXT<span class="tag"></<span class="name">name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">description</span>></span>JWXT<span class="tag"></<span class="name">description</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">properties</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">java.version</span>></span>1.8<span class="tag"></<span class="name">java.version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">properties</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependencies</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.springframework.boot<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-boot-starter<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.jsoup<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jsoup<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.10.3<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="comment"><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail --></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.springframework.boot<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-boot-starter-mail<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.5.4<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="comment"><!--Lang3工具类--></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.apache.commons<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>commons-lang3<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>3.7<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="comment"><!-- 发送邮件 --></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>javax.mail<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>mail<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.4.7<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--HttpClient--></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.apache.httpcomponents<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>httpclient<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>4.5.6<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.apache.httpcomponents<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>httpclient<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>4.3.4<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="comment"><!--阿里巴巴 json--></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.alibaba<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>fastjson<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.2.51<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>cn.hutool<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>hutool-all<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.1.0<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.springframework.boot<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-boot-starter-test<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">scope</span>></span>test<span class="tag"></<span class="name">scope</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependencies</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">build</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">plugins</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">plugin</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.springframework.boot<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-boot-maven-plugin<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">plugin</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">plugins</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">build</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">project</span>></span></span><br><span class="line"></span><br></pre></td></tr></table></figure><p>在IDEA新建Springboot框架 然后在新建类LoginPZ</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.jwxt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.http.NameValuePair;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.client.entity.UrlEncodedFormEntity;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.client.methods.CloseableHttpResponse;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.client.methods.HttpGet;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.client.methods.HttpPost;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.client.protocol.HttpClientContext;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.cookie.Cookie;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.impl.client.CloseableHttpClient;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.impl.client.HttpClients;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.message.BasicNameValuePair;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.net.URI;</span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 登录</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LoginPz</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">hello</span><span class="params">()</span> </span>{</span><br><span class="line"> Map<String,String> param = <span class="keyword">new</span> HashMap<>();</span><br><span class="line"> String userAccount = <span class="string">"你的学号"</span>;</span><br><span class="line"> String userPassword = <span class="string">"密码"</span>;</span><br><span class="line"> String encoded = <span class="string">"上文提到的加密信息"</span>;<span class="comment">//加密信息</span></span><br><span class="line"> param.put(<span class="string">"userAccount"</span>,userAccount);</span><br><span class="line"> param.put(<span class="string">"userPassword"</span>,userPassword);</span><br><span class="line"> param.put(<span class="string">"encoded"</span>,encoded);</span><br><span class="line"> List<URI> redirectLocations = <span class="keyword">null</span>;</span><br><span class="line"> <span class="comment">// 创建Httpclient对象</span></span><br><span class="line"> CloseableHttpClient httpClient = HttpClients.createDefault();</span><br><span class="line"> CloseableHttpResponse response = <span class="keyword">null</span>;</span><br><span class="line"> String resultString = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// 创建Http Post请求</span></span><br><span class="line"> HttpPost httpPost = <span class="keyword">new</span> HttpPost(<span class="string">"http://218.75.197.123:83/jsxsd/xk/LoginToXk"</span>);</span><br><span class="line"> <span class="comment">//请求头</span></span><br><span class="line"> httpPost.addHeader(<span class="string">"Content-Type"</span>,<span class="string">"application/x-www-form-urlencoded"</span>);</span><br><span class="line"> httpPost.addHeader(<span class="string">"Cookie"</span>,<span class="string">"JSESSIONID=22B4C4CE6240C6C53FF6BC3C197E3B83; SERVERID=121; JSESSIONID=8FFFAEA49DC840CE5A3135330C06CED3"</span>);</span><br><span class="line"> <span class="comment">// 创建参数列表</span></span><br><span class="line"> <span class="keyword">if</span> (param != <span class="keyword">null</span>) {</span><br><span class="line"> List<NameValuePair> paramList = <span class="keyword">new</span> ArrayList<>();</span><br><span class="line"> <span class="keyword">for</span> (String key : param.keySet()) {</span><br><span class="line"> paramList.add(<span class="keyword">new</span> BasicNameValuePair(key, param.get(key)));</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 模拟表单登录</span></span><br><span class="line"> UrlEncodedFormEntity entity = <span class="keyword">new</span> UrlEncodedFormEntity(paramList);</span><br><span class="line"> httpPost.setEntity(entity);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 执行http请求</span></span><br><span class="line"> HttpClientContext context = HttpClientContext.create();</span><br><span class="line"> response = httpClient.execute(httpPost,context);</span><br><span class="line"> <span class="comment">//获取Cookie信息,得到两个参数 JSESSIONID 、 Serverid</span></span><br><span class="line"> List<Cookie> cookies = context.getCookieStore().getCookies();</span><br><span class="line"> <span class="keyword">for</span> (Cookie cookie : cookies) {</span><br><span class="line"> String name = cookie.getName();</span><br><span class="line"> String value = cookie.getValue();</span><br><span class="line"><span class="comment">// System.out.println("name:"+name+","+value);</span></span><br><span class="line"> <span class="keyword">return</span> value;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> response.close();</span><br><span class="line"> } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>这是模拟教务系统登陆 然后我们新建类GetUserInfo</p><p>代码如下</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.jwxt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.http.client.methods.CloseableHttpResponse;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.client.methods.HttpGet;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.impl.client.CloseableHttpClient;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.impl.client.HttpClients;</span><br><span class="line"><span class="keyword">import</span> org.apache.http.util.EntityUtils;</span><br><span class="line"><span class="keyword">import</span> org.jsoup.Jsoup;</span><br><span class="line"><span class="keyword">import</span> org.jsoup.nodes.Document;</span><br><span class="line"><span class="keyword">import</span> org.jsoup.nodes.Element;</span><br><span class="line"><span class="keyword">import</span> org.jsoup.select.Elements;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Timer;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Collectors;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 登录后获取用户信息</span></span><br><span class="line"><span class="comment"> * 相关解析HTMl的操作不是固定的</span></span><br><span class="line"><span class="comment"> * 需要结合自己的需求来操作</span></span><br><span class="line"><span class="comment"> * 此处仅作为演示。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GetUserInfo</span> </span>{</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">int</span> idx = <span class="number">0</span>;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">cj</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">1000000000</span>; ) {</span><br><span class="line"> LoginPz loginPz = <span class="keyword">new</span> LoginPz();</span><br><span class="line"> String cookies = loginPz.hello();</span><br><span class="line"> CloseableHttpClient httpClient = HttpClients.createDefault();</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> HttpGet httpGet = <span class="keyword">new</span> HttpGet(<span class="string">"http://218.75.197.123:83/jsxsd/kscj/cjcx_list?kksj=2021-2022-1"</span>);<span class="comment">//2021-2022-1为学期信息 可自行更改</span></span><br><span class="line"> <span class="comment">//增加头信息</span></span><br><span class="line"> <span class="comment">//注意此处需要修改为正确的JSESSIONID 和 SERVERID</span></span><br><span class="line"> httpGet.addHeader(<span class="string">"Cookie"</span>, <span class="string">"JSESSIONID"</span> + <span class="string">"="</span> + cookies + <span class="string">"; SERVERID=121; JSESSIONID=8FFFAEA49DC840CE5A3135330C06CED3"</span>);</span><br><span class="line"> httpGet.addHeader(<span class="string">"User-Agent"</span>, <span class="string">"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"</span>);</span><br><span class="line"> httpGet.addHeader(<span class="string">"Accept"</span>, <span class="string">"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"</span>);</span><br><span class="line"> httpGet.addHeader(<span class="string">"Connection"</span>, <span class="string">"keep-alive"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//执行</span></span><br><span class="line"> CloseableHttpResponse response = httpClient.execute(httpGet);</span><br><span class="line"> String html = EntityUtils.toString(response.getEntity(), <span class="string">"utf8"</span>);</span><br><span class="line"> Document parse = Jsoup.parse(html);</span><br><span class="line"> parseHtml(parse);</span><br><span class="line"> Thread thread = <span class="keyword">new</span> Thread();</span><br><span class="line"> thread.sleep(<span class="number">600000</span>);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">parseHtml</span><span class="params">(Document parse)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"><span class="comment">// int idx = 2;//目前已出成绩科目</span></span><br><span class="line"> <span class="comment">//选择table</span></span><br><span class="line"> Element table = parse.getElementById(<span class="string">"dataList"</span>);</span><br><span class="line"> <span class="comment">//选择tr</span></span><br><span class="line"> Elements cells = table.select(<span class="string">"tr"</span>);</span><br><span class="line"></span><br><span class="line"> StringBuilder stringBuilder = <span class="keyword">new</span> StringBuilder(); <span class="comment">//用来存储成绩信息</span></span><br><span class="line"> <span class="comment">//自己存储的每一行数据</span></span><br><span class="line"> List<List<String>> tables = <span class="keyword">new</span> ArrayList<>();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> index = <span class="number">1</span>; index < cells.size(); index++) {</span><br><span class="line"> <span class="comment">//第一行是表头 index = 0 跳过</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//第二行开始table数据</span></span><br><span class="line"></span><br><span class="line"> Element row = cells.get(index);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//搜索tr下的所有的td</span></span><br><span class="line"> Elements rows = row.select(<span class="string">"td"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//每一行的数据</span></span><br><span class="line"> List<String> dataList = <span class="keyword">new</span> ArrayList<>();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> (Element element : rows) {</span><br><span class="line"> dataList.add(element.text());</span><br><span class="line"> }</span><br><span class="line"> tables.add(dataList);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//获取表头</span></span><br><span class="line"> Elements headers = cells.get(<span class="number">0</span>).select(<span class="string">"th"</span>);</span><br><span class="line"> List<String> tableHeader = headers.stream()</span><br><span class="line"> .map(Element::text)</span><br><span class="line"> .collect(Collectors.toList());</span><br><span class="line"></span><br><span class="line"> <span class="comment">//打印数据</span></span><br><span class="line"> <span class="keyword">for</span> (String str : tableHeader) {</span><br><span class="line"> System.out.printf(str + <span class="string">" "</span>);</span><br><span class="line"> stringBuilder.append(str + <span class="string">" "</span>);</span><br><span class="line"> }</span><br><span class="line"> stringBuilder.append(<span class="string">"\r\n"</span>);</span><br><span class="line"> System.out.println(<span class="string">""</span>);</span><br><span class="line"> <span class="keyword">for</span> (List<String> strs : tables) {</span><br><span class="line"> <span class="keyword">for</span> (String str : strs) {</span><br><span class="line"> System.out.printf(str + <span class="string">" "</span>);</span><br><span class="line"> stringBuilder.append(str + <span class="string">" "</span>);</span><br><span class="line"> }</span><br><span class="line"> stringBuilder.append(<span class="string">"\r\n"</span>);</span><br><span class="line"> System.out.println(<span class="string">""</span>);</span><br><span class="line"> }</span><br><span class="line"> System.out.println();</span><br><span class="line"><span class="comment">// System.out.println(tables.size());</span></span><br><span class="line"> <span class="keyword">if</span> (tables.size() > idx)<span class="comment">//idx 为原先的成绩数量123</span></span><br><span class="line"> {</span><br><span class="line"> <span class="comment">//发送邮件给自己</span></span><br><span class="line"> System.out.println(<span class="string">"这是一个标志"</span>);</span><br><span class="line"> SendMailUtil.sendEmail(<span class="string">"你的邮箱 [email protected]"</span>, <span class="string">"成绩更新"</span>, stringBuilder.toString());</span><br><span class="line"> idx = tables.size();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>新建发邮件的类SendMailUtil<br>邮箱建议使用qq邮箱 需要拿到授权码</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.jwxt;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.OutputStream;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.mail.Address;</span><br><span class="line"><span class="keyword">import</span> javax.mail.Message;</span><br><span class="line"><span class="keyword">import</span> javax.mail.Session;</span><br><span class="line"><span class="keyword">import</span> javax.mail.Transport;</span><br><span class="line"><span class="keyword">import</span> javax.mail.internet.InternetAddress;</span><br><span class="line"><span class="keyword">import</span> javax.mail.internet.MimeMessage;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sun.mail.util.MailSSLSocketFactory;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SendMailUtil</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">//邮件服务器主机名</span></span><br><span class="line"> <span class="comment">// QQ邮箱的 SMTP 服务器地址为: smtp.qq.com</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> String myEmailSMTPHost = <span class="string">"smtp.qq.com"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//发件人邮箱</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> String myEmailAccount = <span class="string">"[email protected]"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//发件人邮箱密码(授权码)</span></span><br><span class="line"> <span class="comment">//在开启SMTP服务时会获取到一个授权码,把授权码填在这里</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> String myEmailPassword = <span class="string">"授权码 需要自己去qq邮箱拿"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 邮件单发(自由编辑短信,并发送,适用于私信)</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> toEmailAddress 收件箱地址</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> emailTitle 邮件主题</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> emailContent 邮件内容</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sendEmail</span><span class="params">(String toEmailAddress, String emailTitle, String emailContent)</span> <span class="keyword">throws</span> Exception</span>{</span><br><span class="line"></span><br><span class="line"> Properties props = <span class="keyword">new</span> Properties();</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 开启debug调试</span></span><br><span class="line"> props.setProperty(<span class="string">"mail.debug"</span>, <span class="string">"true"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 发送服务器需要身份验证</span></span><br><span class="line"> props.setProperty(<span class="string">"mail.smtp.auth"</span>, <span class="string">"true"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 端口号</span></span><br><span class="line"> props.put(<span class="string">"mail.smtp.port"</span>, <span class="number">465</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 设置邮件服务器主机名</span></span><br><span class="line"> props.setProperty(<span class="string">"mail.smtp.host"</span>, myEmailSMTPHost);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 发送邮件协议名称</span></span><br><span class="line"> props.setProperty(<span class="string">"mail.transport.protocol"</span>, <span class="string">"smtp"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**SSL认证,注意腾讯邮箱是基于SSL加密的,所以需要开启才可以使用**/</span></span><br><span class="line"> MailSSLSocketFactory sf = <span class="keyword">new</span> MailSSLSocketFactory();</span><br><span class="line"> sf.setTrustAllHosts(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//设置是否使用ssl安全连接(一般都使用)</span></span><br><span class="line"> props.put(<span class="string">"mail.smtp.ssl.enable"</span>, <span class="string">"true"</span>);</span><br><span class="line"> props.put(<span class="string">"mail.smtp.ssl.socketFactory"</span>, sf);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//创建会话</span></span><br><span class="line"> Session session = Session.getInstance(props);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//获取邮件对象</span></span><br><span class="line"> <span class="comment">//发送的消息,基于观察者模式进行设计的</span></span><br><span class="line"> Message msg = <span class="keyword">new</span> MimeMessage(session);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//设置邮件标题</span></span><br><span class="line"> msg.setSubject(emailTitle);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//设置邮件内容</span></span><br><span class="line"> <span class="comment">//使用StringBuilder,因为StringBuilder加载速度会比String快,而且线程安全性也不错</span></span><br><span class="line"> StringBuilder builder = <span class="keyword">new</span> StringBuilder();</span><br><span class="line"></span><br><span class="line"> <span class="comment">//写入内容</span></span><br><span class="line"> builder.append(<span class="string">"\n"</span> + emailContent);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//设置显示的发件时间</span></span><br><span class="line"> msg.setSentDate(<span class="keyword">new</span> Date());</span><br><span class="line"></span><br><span class="line"> <span class="comment">//设置邮件内容</span></span><br><span class="line"> msg.setText(builder.toString());</span><br><span class="line"></span><br><span class="line"> <span class="comment">//设置发件人邮箱</span></span><br><span class="line"> <span class="comment">// InternetAddress 的三个参数分别为: 发件人邮箱, 显示的昵称(只用于显示, 没有特别的要求), 昵称的字符集编码</span></span><br><span class="line"> msg.setFrom(<span class="keyword">new</span> InternetAddress(myEmailAccount,<span class="string">"我的工作站"</span>, <span class="string">"UTF-8"</span>));</span><br><span class="line"></span><br><span class="line"> <span class="comment">//得到邮差对象</span></span><br><span class="line"> Transport transport = session.getTransport();</span><br><span class="line"></span><br><span class="line"> <span class="comment">//连接自己的邮箱账户</span></span><br><span class="line"> <span class="comment">//密码不是自己QQ邮箱的密码,而是在开启SMTP服务时所获取到的授权码</span></span><br><span class="line"> <span class="comment">//connect(host, user, password)</span></span><br><span class="line"> transport.connect( myEmailSMTPHost, myEmailAccount, myEmailPassword);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//发送邮件</span></span><br><span class="line"> transport.sendMessage(msg, <span class="keyword">new</span> Address[] { <span class="keyword">new</span> InternetAddress(toEmailAddress) });</span><br><span class="line"></span><br><span class="line"> <span class="comment">//将该邮件保存到本地</span></span><br><span class="line"> OutputStream out = <span class="keyword">new</span> FileOutputStream(<span class="string">"MyEmail.eml"</span>);</span><br><span class="line"> msg.writeTo(out);</span><br><span class="line"> out.flush();</span><br><span class="line"> out.close();</span><br><span class="line"></span><br><span class="line"> transport.close();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>拿授权码过程</p><p><img src="https://s2.loli.net/2022/01/11/dcGYlLwV7R2QSO6.png"></p><p><img src="https://s2.loli.net/2022/01/11/FeGv29CXQPByhwr.png"></p><p>拿到授权码之后复制到上面去</p><p>再到Application添加启动类</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.jwxt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.SpringApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JwxtApplication</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> SpringApplication.run(JwxtApplication.class, args);</span><br><span class="line"> GetUserInfo getUserInfo = <span class="keyword">new</span> GetUserInfo();</span><br><span class="line"> getUserInfo.cj();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>如果自己有服务器就把这个项目打包成jar包上跑起来</p><p>没有的话只能把项目一直跑着不关</p><p>打jar包</p><p><img src="https://s2.loli.net/2022/01/11/Np9EQ26qcyemUTx.png"></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220516201452.png"></p><p>用xshell+xftp把文件传入服务器上</p><p><img src="https://s2.loli.net/2022/01/11/N8nC7XRiZzOkfgt.png"></p><p>然后用xshell 一直cd到jar包所在的路径</p><p><img src="https://s2.loli.net/2022/01/11/NjL9gcGkawufxbv.png"></p><p>使用命令 </p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nohup java -jar JWXT-0.0.1-SNAPSHOT.jar 2>&1 &</span><br></pre></td></tr></table></figure><p>然后就大功告成了 一般你跑起来之后就会收到一封邮件提醒</p><p>github的项目地址 <a href="https://github.com/fengxiaop/JWXT">https://github.com/fengxiaop/JWXT</a></p><p> 如果有用的话请你动动你的小手点点star 因为这无疑是对我巨大的鼓励。</p>]]></content>
<summary type="html"><p> 写这个已经过了很久的的时间了 当时写好没写博客是因为怕这个有额外的问题,现在经过实验 表现的都还没不错 在10min内成绩提醒就会发送到你自己指定的邮箱 我会把代码开源放入Github,当然我也会隐去我自己的信息。</p>
<p>这篇博客也会教大家怎么使用他</p></summary>
<category term="Java" scheme="http://example.com/tags/Java/"/>
</entry>
<entry>
<title>兑吧实习一面+二面+hr面 已OC</title>
<link href="http://example.com/2022/09/23/%E5%85%91%E5%90%A7%E6%8A%80%E6%9C%AF%E4%B8%80%E9%9D%A2%E3%80%81%E4%BA%8C%E9%9D%A2%E3%80%81HR%E9%9D%A2/"/>
<id>http://example.com/2022/09/23/%E5%85%91%E5%90%A7%E6%8A%80%E6%9C%AF%E4%B8%80%E9%9D%A2%E3%80%81%E4%BA%8C%E9%9D%A2%E3%80%81HR%E9%9D%A2/</id>
<published>2022-09-23T08:33:56.000Z</published>
<updated>2023-09-26T13:14:18.309Z</updated>
<content type="html"><![CDATA[<h1 id="兑吧一面-9-22下午"><a href="#兑吧一面-9-22下午" class="headerlink" title="兑吧一面 9.22下午"></a>兑吧一面 9.22下午</h1><p>不得不说流程是真的快 9.21 实习同事推简历过去 9.22早上约一面的电话 半小时不到约第二天早上二面 半小时不到约hr面 hr面不到一小时OC</p><span id="more"></span><p>自我介绍</p><ul><li>Java集合认识的多吗</li><li>ArrayList和LinkedList的区别</li><li>HashMap有了解吗</li><li>你刚刚 说了HashMap的put方法 详细讲讲</li><li>得到下标是异或和位运算 讲讲为什么这样设计</li><li>怎么保证得到的数组下标在范围里面</li><li>随着你不断的放元素 他会有什么变化吗</li><li>为什么是双倍的扩张</li><li>它是不安全的类 那我们考虑线程安全的时候有什么替代方案吗</li><li>currentHashMap怎么做到线程安全的 数组 + segment + 分段锁 </li><li>哪些方式实现线程安全</li><li>sychronize你认识多少呢</li><li>有了解过他底层的实现吗</li><li>在你去使用sychronize的时候会注意什么 </li><li>Java除了sychronize还有其他的方式实现线程安全</li><li>除了volatile+juc.automic还有其他的吗?</li><li>有听说过Lock吗 Java中有哪些</li><li>AQS是什么?怎么用?</li><li>有听说过ReentrantLock吗</li><li>你对JVM的认识有多少</li><li>JVM五个区域哪几个区域会发生内存泄露呢</li><li>静态成员变量 在哪个区域 方法区</li><li>类的成员变量在哪个区域</li><li>垃圾回收算法</li><li>标记清除和标记整理来说他的好处在哪里</li><li>线程池有了解吗 什么是线程池有了解</li><li>做项目的时候有用到过线程池吗</li><li>有没有如何去了解过去创建一个线程池</li><li>mysql用的是什么搜索引擎</li><li>innodb和myisam有什么区别</li><li>事务有什么特性</li><li>innodb是怎么做的事务的 四个隔离级别</li><li>默认的隔离级别</li><li>可重复读解决了哪些问题</li><li>什么是幻读</li><li>mysql是索引怎么分</li><li>Hash索引和b+树索引有什么区别</li><li>单向链表的作用呢</li><li>where id < 5是怎么查询的</li><li>刚刚讲到索盖覆盖 索引覆盖是什么场景</li><li>在使用覆盖索引需要注意什么 最左浅醉原则 </li><li>以前项目讲到接口速度优化?</li><li>有接触过分布锁吗?怎么实现?</li><li>数据库是怎么实现分布式锁的?</li><li>redis实现分布式锁</li><li>使用redis分布式锁需要注意的地方</li><li>会设计分布式锁的过期时间吗</li><li>如何保证 redis 可重入锁</li><li>或者可以简单说一下什么是可重入吗 没有听过</li><li>如果让你去猜 你觉得他应该是什么样的性质</li><li>git的命令有哪些</li><li>聊下Spring, 你对Spring的认知</li><li>日志功能怎么实现呢</li><li>redis在项目里面主要做什么</li><li>常用的数据结构</li><li>zest是什么结构</li><li>平时的学习途径和频率</li><li>反问</li><li>技术栈 springcloud + redis</li><li>底层是否需要钻研? 需要有一个认知</li><li>工程经验和基础扎实哪个更重要 更侧重哪方面 基础扎实</li></ul><h1 id="兑吧二面-9-22上午"><a href="#兑吧二面-9-22上午" class="headerlink" title="兑吧二面 9.22上午"></a>兑吧二面 9.22上午</h1><p>自我介绍</p><p>讲讲项目</p><ul><li>多并发怎么实现的?</li><li>redis用来干嘛的</li><li>MQ用来干嘛的</li><li>robocom一等奖讲讲</li><li>实习的接口调优</li><li>为什么要从上家公司离职</li><li>是主要看杭州还是上海</li><li>有了解过我们公司的业务是做什么的吗</li></ul><p>后面就是介绍公司的业务,新人培养机制 项目制导向 有时候会忙</p><p>调休机制 等等 差不多介绍了10分钟</p><p>二面面完没多久就约hr面了 约在了下午两点</p><h1 id="兑吧HR面"><a href="#兑吧HR面" class="headerlink" title="兑吧HR面"></a>兑吧HR面</h1><ul><li>最近有一段实习的经历 怎么没有继续做下去</li><li>老家是哪的</li><li>这两个项目是在学校做的还是在实习的时候做的</li><li>讲一个项目印象深刻的案例</li><li>那你觉得具体项目做完之后最大的提升之后是什么?</li><li>个人觉得从事Java行业有什么优势</li><li>除了技能这个方面 个人还要在职场学习什么方面</li><li>什么时候自己会比较沮丧</li><li>个人比较倾向什么样的leader</li><li>你觉得是自己是主动性比较强的人吗</li><li>工作的话是兴趣导向为主吗</li><li>未来3-5年的个人规划</li><li>你觉得你周围有做的比较好的同学和同事吗</li><li>高强度学习?什么算高强度学习</li><li>像你们上一段实习工作强度大吗</li><li>那你为什么要考虑来杭州发展</li><li>找实习都在杭州吗?</li><li>其他公司面的怎么样</li></ul><p>这家真的速度非常快 面完半小时内就沟通下一面的时间</p><p>hr面完一小时就OC了 </p>]]></content>
<summary type="html"><h1 id="兑吧一面-9-22下午"><a href="#兑吧一面-9-22下午" class="headerlink" title="兑吧一面 9.22下午"></a>兑吧一面 9.22下午</h1><p>不得不说流程是真的快 9.21 实习同事推简历过去 9.22早上约一面的电话 半小时不到约第二天早上二面 半小时不到约hr面 hr面不到一小时OC</p></summary>
<category term="面试" scheme="http://example.com/tags/%E9%9D%A2%E8%AF%95/"/>
</entry>
<entry>
<title>面试-哈啰一面</title>
<link href="http://example.com/2022/09/18/%E5%93%88%E5%95%B0%E5%87%BA%E8%A1%8C/"/>
<id>http://example.com/2022/09/18/%E5%93%88%E5%95%B0%E5%87%BA%E8%A1%8C/</id>
<published>2022-09-18T07:46:12.137Z</published>
<updated>2023-09-26T13:03:28.855Z</updated>
<content type="html"><![CDATA[<h1 id="简单的自我介绍"><a href="#简单的自我介绍" class="headerlink" title="简单的自我介绍"></a>简单的自我介绍</h1><span id="more"></span><h2 id="项目"><a href="#项目" class="headerlink" title="项目"></a>项目</h2><p>模糊查询是怎么样设计</p><p>%模糊查询性能比较差 有没有更好的方法 es</p><p>Excel 导入与导出</p><p>10w行的excel导入会出现什么问题 </p><p>如果中间解析失败 导致整个异常怎么办</p><p>提高接口响应速度 </p><h2 id="基础问题"><a href="#基础问题" class="headerlink" title="基础问题"></a>基础问题</h2><p>怎么理解操作系统当中的进程和线程 在你的实习当中有用到哪一个吗</p><p>spring是怎么工作的</p><p>有看过spring相关的书吗</p><p>书看了多少呢 里面讲了多少知识呢</p><p>你学的计算机网络大概分几层 每层的作用大概是做什么</p><p>堆和栈有什么区别吗</p><p>你能简述一个你所熟悉的排序算法 + 原理</p><h2 id="写SQL"><a href="#写SQL" class="headerlink" title="写SQL"></a>写SQL</h2><table><thead><tr><th align="left">学生表</th><th>考试得分表</th><th>班级表</th></tr></thead><tbody><tr><td align="left">学生id</td><td>学生id</td><td>班级id</td></tr><tr><td align="left">学生姓名</td><td>课程名</td><td>班级名</td></tr><tr><td align="left">班级id</td><td>得分</td><td></td></tr></tbody></table><p>求语文平均分最高的班级名</p><p>代码管理工具 怎么提交代码 以及 解决代码冲突</p><p>怎样保证你自己开发的质量</p><p>有了解过程序员这个职业吗 后面有什么自己的职业规划</p><p>怎么样实现你所说的这个目标呢</p><p>上一份工作为什么辞职了呢</p><p>主要是看上海还是杭州啊</p><h2 id="反问"><a href="#反问" class="headerlink" title="反问"></a>反问</h2><ul><li>主要业务是哪一块</li><li>校招生需要加强哪一块</li><li>觉得表现怎么样</li></ul>]]></content>
<summary type="html"><h1 id="简单的自我介绍"><a href="#简单的自我介绍" class="headerlink" title="简单的自我介绍"></a>简单的自我介绍</h1></summary>
<category term="实习" scheme="http://example.com/tags/%E5%AE%9E%E4%B9%A0/"/>
</entry>
<entry>
<title>leetcode五百题以及实习接近了尾声</title>
<link href="http://example.com/2022/08/10/leetcode%E4%BA%94%E7%99%BE%E9%A2%98%E4%BB%A5%E5%8F%8A%E5%AE%9E%E4%B9%A0%E6%8E%A5%E8%BF%91%E4%BA%86%E5%B0%BE%E5%A3%B0/"/>
<id>http://example.com/2022/08/10/leetcode%E4%BA%94%E7%99%BE%E9%A2%98%E4%BB%A5%E5%8F%8A%E5%AE%9E%E4%B9%A0%E6%8E%A5%E8%BF%91%E4%BA%86%E5%B0%BE%E5%A3%B0/</id>
<published>2022-08-10T11:46:39.417Z</published>
<updated>2023-09-26T13:08:57.308Z</updated>
<content type="html"><![CDATA[<h1 id="leetcode五百题以及实习接近了尾声"><a href="#leetcode五百题以及实习接近了尾声" class="headerlink" title="leetcode五百题以及实习接近了尾声"></a>leetcode五百题以及实习接近了尾声</h1><span id="more"></span><p>六月中旬 自己坐高铁独自北上,人生中第一次离开了自己生活了20年没离开过的湖南省。</p><p>遥想三年前,自己和当时一起考入一个大学的同桌坐高铁一起从那个湘西南的小县城去往湖南株洲。</p><p>当时两个从来没有离开过那个小县城的我们用着惊叹的眼神看着高铁站外面的高楼大厦,人来人往以及车让我们迷失了方向。</p><p>好在学校在高铁站有接待点,上了学校包的公交车,和不同地方五湖四海不同学院,和他们一起前往了那个我大学生涯待在那里的学校</p><p>如果八月中旬,我决定结束我的实习之旅 本周五完成工作交接并且正式离职。今天自己刷leetocde也达到了500道题 也想发点什么给自己记录一下,也想大学毕业的时候还能够继续看一下这些记录</p><!-- more --><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220810194831.png"></p><p>自己刷力扣是从9月份那时候大三刚开学 感觉自己要为工作准备一些 以前一直知道leetcode这个网站。</p><p>但是一直没有勇气坚持下去,我记得我大二的时候尝试过,但是被我想像中的两数之和以及核心代码不适应劝退了。</p><p> 大二一年忙着家教挣生活费,当时没时间去弄这样。记得刷了力扣两个月 大概11月份的样子 我去参加了学院组织的程序竞赛 获得了非新生组的二等奖 。</p><p> 离一等奖仅有一名只差 当然这要感谢我们学校的ACM大神们不屑于来欺负我们。但是的那个二等奖给了我很大的信心,后面断断续续的刷了一些题目。</p><p>后面加了<strong>宫水三叶姐</strong>的刷题群后,发现氛围真的是一个极其重要的东西。那里们有很多人细心的指导着新手对一些很常见问题的提问。</p><p>再后面跟着做每日一题 但是题太难了就会直接copy 老是想着有时间再去复习 实际上并没有坐到。</p><p>自己的水平再慢慢的提升,但是进步的空间依旧很大。</p><p>需要自己继续努力 秋招已经开始了。今年好像是过去十年最难的一届 好像也是将来十年最好的一年,听的人很焦虑,除了工作压力大之外 我承受受了这个的影响 想早一点的准备秋招。</p><p>在实习遇见了很多nice的同事 当然也会遇见很多不是很高兴的事情 比如比较push的组长 自己一直是一个想做到工作生活分离的人 不想在下班的时候被打扰 结果生活往往不是那么的如意。</p><p>也给我要步入社会的我上了一课。</p><p>在这里希望秋招顺利!工作顺心 万事如意。</p><p>也祝各位看到这的人<strong>天天开心</strong>!</p>]]></content>
<summary type="html"><h1 id="leetcode五百题以及实习接近了尾声"><a href="#leetcode五百题以及实习接近了尾声" class="headerlink" title="leetcode五百题以及实习接近了尾声"></a>leetcode五百题以及实习接近了尾声</h1></summary>
<category term="所想" scheme="http://example.com/tags/%E6%89%80%E6%83%B3/"/>
</entry>
<entry>
<title>JAVA基础复习</title>
<link href="http://example.com/2022/06/29/Java%E5%9F%BA%E7%A1%80%E5%A4%8D%E4%B9%A0/"/>
<id>http://example.com/2022/06/29/Java%E5%9F%BA%E7%A1%80%E5%A4%8D%E4%B9%A0/</id>
<published>2022-06-29T07:27:15.543Z</published>
<updated>2023-09-26T12:51:56.958Z</updated>
<content type="html"><![CDATA[<h1 id="Java基础复习"><a href="#Java基础复习" class="headerlink" title="Java基础复习"></a>Java基础复习</h1><h2 id=""><a href="#" class="headerlink" title=""></a><span id="more"></span></h2><h2 id="1、-与equals方法"><a href="#1、-与equals方法" class="headerlink" title="1、== 与equals方法"></a>1、== 与equals方法</h2><p>对于八种基本数据类型来说(byte short int long float double boolean char) == 是比较的值 而八种基本数据类型 是没有equals方法的</p><p>对于引用数据类型来说 == 比较的是对象的内存地址 而equals比较的字面值(例:String类型)</p><h2 id="2、Synchronized关键字"><a href="#2、Synchronized关键字" class="headerlink" title="2、Synchronized关键字"></a>2、Synchronized关键字</h2><h3 id="作用"><a href="#作用" class="headerlink" title="作用"></a>作用</h3><ul><li><strong>原子性</strong>:<strong>所谓原子性就是指一个操作或者多个操作,要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行。</strong>被<code>synchronized</code>修饰的类或对象的所有操作都是原子的,因为在执行操作之前必须先获得类或对象的锁,直到执行完才能释放。</li><li><strong>可见性</strong>:**可见性是指多个线程访问一个资源时,该资源的状态、值信息等对于其他线程都是可见的。 **synchronized和volatile都具有可见性,其中synchronized对一个类或对象加锁时,一个线程如果要访问该类或对象必须先获得它的锁,而这个锁的状态对于其他任何线程都是可见的,并且在释放锁之前会将对变量的修改刷新到共享内存当中,保证资源变量的可见性。</li><li><strong>有序性</strong>:<strong>有序性值程序执行的顺序按照代码先后执行。</strong> synchronized和volatile都具有有序性,Java允许编译器和处理器对指令进行重排,但是指令重排并不会影响单线程的顺序,它影响的是多线程并发执行的顺序性。synchronized保证了每个时刻都只有一个线程访问同步代码块,也就确定了线程执行同步代码块是分先后顺序的,保证了有序性。</li></ul><p><strong>Synchronized主要有三种用法</strong>:</p><ul><li>修饰实例方法: 作用于当前对象实例加锁,进入同步代码前要获得 当前对象实例的锁</li></ul><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">synchronized</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">//业务代码</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li><strong>修饰静态方法:</strong> 也就是给当前类加锁,会作用于类的所有对象实例 ,进入同步代码前要获得 <strong>当前 class 的锁</strong>。因为静态成员不属于任何一个实例对象,是类成员( <em>static 表明这是该类的一个静态资源,不管 new 了多少个对象,只有一份</em>)。所以,如果一个线程 A 调用一个实例对象的非静态 <code>synchronized</code> 方法,而线程 B 需要调用这个实例对象所属类的静态 <code>synchronized</code> 方法,是允许的,不会发生互斥现象,<strong>因为访问静态 <code>synchronized</code> 方法占用的锁是当前类的锁,而访问非静态 <code>synchronized</code> 方法占用的锁是当前实例对象锁</strong>。</li></ul><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">synchronized</span> <span class="keyword">void</span> staic <span class="title">method</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">//业务代码</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li><strong>修饰代码块</strong> :指定加锁对象,对给定对象/类加锁。<code>synchronized(this|object)</code> 表示进入同步代码库前要获得<strong>给定对象的锁</strong>。<code>synchronized(类.class)</code> 表示进入同步代码前要获得 <strong>当前 class 的锁</strong></li></ul><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">synchronized</span>(<span class="keyword">this</span>) {</span><br><span class="line"> <span class="comment">//业务代码</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="3-Java容器"><a href="#3-Java容器" class="headerlink" title="3.Java容器"></a>3.Java容器</h2><h3 id="一、Collection"><a href="#一、Collection" class="headerlink" title="一、Collection"></a>一、Collection</h3><p>简单来说就是单个集合的元素</p><p>分为List、Set、Queue三大类</p><p>List有ArrayList(底层由数组组成 查找快 支持随机访问) LinkedList(底层由双向链表组成 修改快 还可以做栈 队列 以及双向队列) 以及Vector(线程安全 但是效率低 很少用)</p><h4 id="List"><a href="#List" class="headerlink" title="List"></a>List</h4><h5 id="ArrayList"><a href="#ArrayList" class="headerlink" title="ArrayList"></a>ArrayList</h5><p>JDK 7 以无参数构造方法创建 ArrayList 时,直接创建了长度是10的Object[]数组elementData 。</p><p>JDK 8 以无参数构造方法创建 ArrayList 时,实际上初始化赋值的是一个空数组。当真正对数组进行添加元素操作时,才真正分配容量。即向数组中添加第一个元素时,数组容量扩为 10。</p><p>底层为一个Object类型的数组 初始长度为0;若采用了泛型 ArrayList<String> list = new ArrayList<>();</p><p>则生成的是String[]类型的数组 初始长度为0</p><h6 id="扩容"><a href="#扩容" class="headerlink" title="扩容"></a>扩容</h6><p>当初始长度为10已经加入了十个元素之后,我们需要再加一个元素的时候,我们就需要扩容</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220629135732.png"></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e, Object[] elementData, <span class="keyword">int</span> s)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (s == elementData.length)</span><br><span class="line"> elementData = grow();</span><br><span class="line"> elementData[s] = e;</span><br><span class="line"> size = s + <span class="number">1</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>这段源代码就是ArrayList的add()方法 如果添加的元素已经满了 则调用grow()函数 很明显 这是一个扩容函数</p><p>grow函数有两个 一个有参函数 一个无参函数</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220629141034.png"></p><p>无参参数会调用有参参数 进行1.5倍的扩容</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ArrayList list1 = <span class="keyword">new</span> ArrayList(<span class="number">23</span>);</span><br></pre></td></tr></table></figure><p>这表示着生成了一个初始长度为23的ArrayList数组</p><h5 id="Vector"><a href="#Vector" class="headerlink" title="Vector"></a>Vector</h5><p>和ArrayList数组类似 线程安全 效率低 用的很少 </p><p>但是我们用的Stack(栈)则是基于Vector设计的</p><p>实现栈</p><p>Stack继承Vector 是Vector的子类</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220629142617.png"></p><h5 id="LinkedList"><a href="#LinkedList" class="headerlink" title="LinkedList"></a>LinkedList</h5><p>基于双向链表实现 增删元素效率高 查询效率低</p><p>LinkedList可以用作栈 队列 以及双向队列</p><h4 id="Set"><a href="#Set" class="headerlink" title="Set"></a>Set</h4><p>集合 无序可去重的集合</p><h5 id="TreeSet"><a href="#TreeSet" class="headerlink" title="TreeSet"></a>TreeSet</h5><p> 无序 不可重复 自动排序 相当于存放在TreeMap的Key部分</p><h5 id="HashSet"><a href="#HashSet" class="headerlink" title="HashSet"></a>HashSet</h5><p> 无序 不可重复 支持快速查找 存放在HashMap中相当于key部分</p><h5 id="LinkedHashSet"><a href="#LinkedHashSet" class="headerlink" title="LinkedHashSet"></a>LinkedHashSet</h5><p> 基于双向链表实现,具有HashSet的查找效率</p><h4 id="Queue"><a href="#Queue" class="headerlink" title="Queue"></a>Queue</h4><h5 id="LinkedList-1"><a href="#LinkedList-1" class="headerlink" title="LinkedList"></a>LinkedList</h5><p>可以用他来实现双向队列</p><h5 id="PriorityQueue"><a href="#PriorityQueue" class="headerlink" title="PriorityQueue"></a>PriorityQueue</h5><p>用于堆实现 可以用它实现优先队列</p><h3 id="二、Map"><a href="#二、Map" class="headerlink" title="二、Map"></a>二、Map</h3><p>映射类型 Key - Value类型结构、</p><h4 id="HashMap"><a href="#HashMap" class="headerlink" title="HashMap"></a>HashMap</h4><p>比如最为常见的HashMap</p><p>JDK 1.7 底层是数组+链表</p><p>JDK 1.8 底层是数组+链表+红黑树 加入红黑树的目的是增加HashMap的插入和查询速率</p><p>HaashMap通过key进行hashcode与 与运算 得到下标。</p><p>HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。</p><p>HashMap 实现了 Map 接口,根据键的 HashCode 值存储数据,具有很快的访问速度,最多允许一条记录的键为 null,不支持线程同步。</p><p>HashMap 是无序的,即不会记录插入的顺序。</p><p>HashMap 继承于AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220629151158.png"></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, Exception </span>{</span><br><span class="line"> HashMap<String,String> map = <span class="keyword">new</span> HashMap<>();</span><br><span class="line"> map.put(<span class="string">"1"</span>,<span class="string">"one"</span>);</span><br><span class="line"> map.put(<span class="string">"2"</span>,<span class="string">"two"</span>);</span><br><span class="line"> map.put(<span class="string">"3"</span>,<span class="string">"three"</span>);</span><br><span class="line"> map.put(<span class="string">"4"</span>,<span class="string">"four"</span>);</span><br><span class="line"> System.out.println(map);</span><br><span class="line"></span><br><span class="line"> HashMap<String, String> Sites = <span class="keyword">new</span> HashMap<String, String>();</span><br><span class="line"> <span class="comment">// 添加键值对</span></span><br><span class="line"> Sites.put(<span class="string">"one"</span>, <span class="string">"Google"</span>);</span><br><span class="line"> Sites.put(<span class="string">"two"</span>, <span class="string">"Runoob"</span>);</span><br><span class="line"> Sites.put(<span class="string">"three"</span>, <span class="string">"Taobao"</span>);</span><br><span class="line"> Sites.put(<span class="string">"four"</span>, <span class="string">"Zhihu"</span>);</span><br><span class="line"> Sites.put(<span class="string">"apple"</span>,<span class="string">"lll"</span>);</span><br><span class="line"> System.out.println(Sites);</span><br><span class="line"> <span class="comment">// key=商品名称,value=价格,这里以这个例子实现按名称排序和按价格排序.</span></span><br><span class="line"> Map store = <span class="keyword">new</span> HashMap();</span><br><span class="line"></span><br><span class="line"> store.put(<span class="string">"iphone12"</span>, <span class="number">6799</span>);</span><br><span class="line"> store.put(<span class="string">"iphone12pro"</span>, <span class="number">8499</span>);</span><br><span class="line"> store.put(<span class="string">"macbookPro"</span>, <span class="number">19499</span>);</span><br><span class="line"> store.put(<span class="string">"ipadAir"</span>, <span class="number">6999</span>);</span><br><span class="line"> store.put(<span class="string">"watch6"</span>, <span class="number">3199</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 直接输出HashMap得到的是一个无序Map(不是Arraylist那种顺序型储存)</span></span><br><span class="line"> System.out.println(store);</span><br><span class="line"></span><br><span class="line"><span class="comment">// {ipadAir=6999, iphone12pro=8499, macbookPro=19499, watch6=3199, iphone12=6799}</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>起初我验证Map的无序的时候 输出的总是有序的 增加了样本之后才变得无序</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220629152545.png"></p><p>但是输入 它内部就有机构形成 无论你是输出十遍还是一百遍 他都是输出一样的顺序 </p><p>这就是HashMap的无序性和有序性</p><p>采用拉链法解决哈希冲突</p><p>JDK1.7采用头插法,有可能形成回路 </p><p>JDK1.8以后采用尾插法</p><p>HashMap的默认初始容量为16</p><ul><li><p>必须是 2 的次幂,这也是 jdk 官⽅推荐的 </p></li><li><p>这是因为达到散列均匀,为了提⾼ HashMap 集合的存取效率,所必须的 </p></li></ul><p> HashMap 默认加载因⼦:0.75 </p><p>数组容器达到 3/4 时,开始扩容 </p><p> JDK 8 之后,对 HashMap 底层数据结构(单链表)进⾏了改进 </p><ul><li><p>如果单链表元素超过8个,则将单链表转变为红⿊树; </p></li><li><p>如果红⿊树节点数量⼩于6时,会将红⿊树重新变为单链表。</p></li></ul><p>hashcode() :通过调用Hashcode()方法得到key的哈希值</p><p>通过哈希函数/哈希算法 转换成数组的下表</p><p>重写Hashcode()和equals()的原因是</p><p>需要达到散列分布均匀</p><h2 id="4-JVM-JRE-JDK的区别"><a href="#4-JVM-JRE-JDK的区别" class="headerlink" title="4.JVM,JRE,JDK的区别"></a>4.JVM,JRE,JDK的区别</h2><p>总的来说 JVM包括JRE JRE包括JVM</p><p>Java 虚拟机(JVM)是运行 Java 字节码的虚拟机。JVM 有针对不同系统的特定实现(Windows,Linux,macOS),目的是使用相同的字节码,它们都会给出相同的结果。字节码和不同系统的 JVM 实现是 Java 语言“一次编译,随处可以运行”的关键所在。</p>]]></content>
<summary type="html"><h1 id="Java基础复习"><a href="#Java基础复习" class="headerlink" title="Java基础复习"></a>Java基础复习</h1><h2 id=""><a href="#" class="headerlink" title=""></a></summary>
<category term="JAVA" scheme="http://example.com/tags/JAVA/"/>
</entry>
<entry>
<title>正量数字面试复盘</title>
<link href="http://example.com/2022/04/24/%E6%AD%A3%E9%87%8F%E6%95%B0%E5%AD%97%E5%A4%8D%E7%9B%98/"/>
<id>http://example.com/2022/04/24/%E6%AD%A3%E9%87%8F%E6%95%B0%E5%AD%97%E5%A4%8D%E7%9B%98/</id>
<published>2022-04-24T11:46:58.797Z</published>
<updated>2023-09-26T13:02:16.193Z</updated>
<content type="html"><![CDATA[<h1 id="正量数字复盘"><a href="#正量数字复盘" class="headerlink" title="正量数字复盘"></a>正量数字复盘</h1><p>自我介绍(2 min) </p><h2 id="Java基础"><a href="#Java基础" class="headerlink" title="Java基础"></a>Java基础</h2><h3 id=""><a href="#" class="headerlink" title=""></a><span id="more"></span></h3><h3 id="String类的方法"><a href="#String类的方法" class="headerlink" title="String类的方法"></a>String类的方法</h3><p>刚开始只想起<code>equals</code>方法</p><p>后面问截取 我答成<code>split</code>方法了 实际上应该是<code>substring</code></p><p>又问了个切割 应该是上面的<code>split</code> 这里我把<code>substring</code>方面说了 </p><p>其实还有很多 面试完之后IDEA里面敲了一下 发现还有很多常用的方法</p><p>一个是获取字符串长度 <code>length()</code> </p><p>一个查看字符串索引的字母 <code>charAt()</code></p><p>contains() toLowercase()toUppercase()这就不一一说了</p><h3 id="数组与List互转"><a href="#数组与List互转" class="headerlink" title="数组与List互转"></a>数组与List互转</h3><p>List转数组 通过toArray()方法</p><p>数组转List 我答得是for循环遍历 然后list通过add()方法加入 </p><p>后面发现可以用asList()方法 这个 很少用到</p><h3 id="HashMap和LinkedList的区别"><a href="#HashMap和LinkedList的区别" class="headerlink" title="HashMap和LinkedList的区别"></a>HashMap和LinkedList的区别</h3><p>HashMap 键值对结构 </p><p>LinkedList 底层数据结构 有序 可重复</p><h3 id="Set与HashMap的最大区别"><a href="#Set与HashMap的最大区别" class="headerlink" title="Set与HashMap的最大区别"></a>Set与HashMap的最大区别</h3><p>Set是无序的 去重的</p><h2 id="Mysql部分"><a href="#Mysql部分" class="headerlink" title="Mysql部分"></a>Mysql部分</h2><h3 id="查询五条数据"><a href="#查询五条数据" class="headerlink" title="查询五条数据"></a>查询五条数据</h3><p>limit 5</p><h3 id="修改一个指定code的name怎么做"><a href="#修改一个指定code的name怎么做" class="headerlink" title="修改一个指定code的name怎么做"></a>修改一个指定code的name怎么做</h3><p>答 update … where…</p><h3 id="一条sql查询code重复的次数"><a href="#一条sql查询code重复的次数" class="headerlink" title="一条sql查询code重复的次数"></a>一条sql查询code重复的次数</h3><p>select count(code)</p><p>group by code</p><h3 id="联合查询用哪个"><a href="#联合查询用哪个" class="headerlink" title="联合查询用哪个"></a>联合查询用哪个</h3><p>答join 被告知是连接 left join 与 right join</p><p>后面答 union</p><h3 id="Mysql的优化"><a href="#Mysql的优化" class="headerlink" title="Mysql的优化"></a>Mysql的优化</h3><p>等值查询可以hash索引</p><p>这方面不太了解 后面复盘找到了这个连接</p><p><a href="https://www.cnblogs.com/JaxYoun/p/12604009.html">https://www.cnblogs.com/JaxYoun/p/12604009.html</a></p><h3 id="用过的Mysql引擎"><a href="#用过的Mysql引擎" class="headerlink" title="用过的Mysql引擎"></a>用过的Mysql引擎</h3><p>只答出了 Innodb</p><h3 id="写代码的过程有了解过事物"><a href="#写代码的过程有了解过事物" class="headerlink" title="写代码的过程有了解过事物"></a>写代码的过程有了解过事物</h3><h3 id="事物的四个特点"><a href="#事物的四个特点" class="headerlink" title="事物的四个特点"></a>事物的四个特点</h3><h4 id="ACID"><a href="#ACID" class="headerlink" title="ACID"></a>ACID</h4><p>A 原子性 要么一起成功</p><p>C 一致性</p><p>I 独立性或隔离性</p><p>D持久性</p><h3 id="有没有听过分布式这个概念吗"><a href="#有没有听过分布式这个概念吗" class="headerlink" title="有没有听过分布式这个概念吗"></a>有没有听过分布式这个概念吗</h3><p>有 但是还没有接触过</p><h3 id="Spring重要组成部分"><a href="#Spring重要组成部分" class="headerlink" title="Spring重要组成部分"></a>Spring重要组成部分</h3><p>IOC AOP 是springcore的主要组成部分 </p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220424201245.png"></p><h3 id="Springboot-pom-xml-有了解过底层吗"><a href="#Springboot-pom-xml-有了解过底层吗" class="headerlink" title="Springboot pom.xml 有了解过底层吗"></a>Springboot pom.xml 有了解过底层吗</h3><p>没用太多了解过</p><h3 id="Spring-springMvc-springboot常用的注解"><a href="#Spring-springMvc-springboot常用的注解" class="headerlink" title="Spring springMvc springboot常用的注解"></a>Spring springMvc springboot常用的注解</h3><p>@requestmapper</p><p>@scheduled</p><p>@Controller</p><p>@around</p><p>@Before</p><h3 id="Controller与RestController有什么区别"><a href="#Controller与RestController有什么区别" class="headerlink" title="Controller与RestController有什么区别"></a>Controller与RestController有什么区别</h3><p>答 不太清楚</p><p>@RestController注解等价于@ResponseBody + @Controller。@RestController和@Controller的共同点是都用来表示Spring某个类是否可以接收HTTP请求,<strong>二者区别: @RestController无法返回指定页面,而@Controller可以</strong>;前者可以直接返回数据,后者需要@ResponseBody辅助。下面详细分析<em>。</em></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220424201741.png"></p><p>返回这个一个json数据 用什么注解</p><p>答 不知道</p><p>一查好像是 @Restcontroller</p><h2 id="前端"><a href="#前端" class="headerlink" title="前端"></a>前端</h2><h3 id="前端的东西有接触过吗"><a href="#前端的东西有接触过吗" class="headerlink" title="前端的东西有接触过吗"></a>前端的东西有接触过吗</h3><p>html+css+js 会写一些</p><p>框架 Layui bootstrap Vue</p><h3 id="adjx的基本参数"><a href="#adjx的基本参数" class="headerlink" title="adjx的基本参数"></a>adjx的基本参数</h3><p>…我竟然回答能不能看以前的项目 </p><p>面试官被我逗笑了 说没事 想不起来就算了</p><p>type 是get请求还是post请求</p><p>url </p><p><a href="https://so.csdn.net/so/search?q=async&spm=1001.2101.3001.7020">async</a>:请求是否为异步</p><p>header</p><p>data</p><p>success</p><p>error</p><p>Layui下拉选择</p><p>不会qaq 我真的对前端不熟悉</p><p>问项目前端是不是自己写的 严重怀疑我的前端水平了。。。</p><h3 id="教务系统的token放哪里"><a href="#教务系统的token放哪里" class="headerlink" title="教务系统的token放哪里"></a>教务系统的token放哪里</h3><p>存在redis里面</p><h3 id="Redis了解吗"><a href="#Redis了解吗" class="headerlink" title="Redis了解吗"></a>Redis了解吗</h3><p>在学习</p><h2 id="Linux命令"><a href="#Linux命令" class="headerlink" title="Linux命令"></a>Linux命令</h2><h3 id="查看某个项目的进程-ps-ef-grep-java-nginx"><a href="#查看某个项目的进程-ps-ef-grep-java-nginx" class="headerlink" title="查看某个项目的进程 ps -ef|grep java/nginx"></a>查看某个项目的进程 ps -ef|grep java/nginx</h3><h3 id="查看当前所处的位置-pwd"><a href="#查看当前所处的位置-pwd" class="headerlink" title="查看当前所处的位置 pwd"></a>查看当前所处的位置 pwd</h3><h3 id="查看一个项目的实时日志-只记得t开头的命令打卡-面试官补充tail"><a href="#查看一个项目的实时日志-只记得t开头的命令打卡-面试官补充tail" class="headerlink" title="查看一个项目的实时日志 只记得t开头的命令打卡 面试官补充tail"></a>查看一个项目的实时日志 只记得t开头的命令打卡 面试官补充tail</h3><h2 id="日常聊天"><a href="#日常聊天" class="headerlink" title="日常聊天"></a>日常聊天</h2><h3 id="问我什么时候能够最快入职"><a href="#问我什么时候能够最快入职" class="headerlink" title="问我什么时候能够最快入职"></a>问我什么时候能够最快入职</h3><p>五月中下旬</p><h3 id="期望薪资"><a href="#期望薪资" class="headerlink" title="期望薪资"></a>期望薪资</h3><p>你那边能给多少呢</p><h3 id="老家哪的"><a href="#老家哪的" class="headerlink" title="老家哪的"></a>老家哪的</h3><p>也是湖南的</p><h3 id="为什么要来北方呢"><a href="#为什么要来北方呢" class="headerlink" title="为什么要来北方呢"></a>为什么要来北方呢</h3><p>没去过想去 不恋家 </p><p>投简历选定了天津吗</p><p>都有</p><h2 id="反问环节"><a href="#反问环节" class="headerlink" title="反问环节"></a>反问环节</h2><h3 id="后面面试流程"><a href="#后面面试流程" class="headerlink" title="后面面试流程"></a>后面面试流程</h3><p>没有了 就这一次</p><h3 id="面试结果什么时候出来"><a href="#面试结果什么时候出来" class="headerlink" title="面试结果什么时候出来"></a>面试结果什么时候出来</h3><p>本周前联系了就是有结果 没有就是没结果</p><h3 id="开发方向与培养机制"><a href="#开发方向与培养机制" class="headerlink" title="开发方向与培养机制"></a>开发方向与培养机制</h3><p>TO B 电商 系统</p><p>技术栈 springboot+springcloud微服务这一套</p><p>好像没有回答培养机制</p><p>后面讲等横向对比 有机会的话联系</p><p>总结难度还行 偏基础和开发方向 没有做题 让我感觉我刷力扣三百多道题白刷了…</p><p>自己表情管理有待加强 总喜欢想的时候看上面 像翻白眼一样 要是没复盘还真不知道自己是这样的。。。</p><p>这一次视频面试 后面希望改正过来和顺顺利利</p>]]></content>
<summary type="html"><h1 id="正量数字复盘"><a href="#正量数字复盘" class="headerlink" title="正量数字复盘"></a>正量数字复盘</h1><p>自我介绍(2 min) </p>
<h2 id="Java基础"><a href="#Java基础" class="headerlink" title="Java基础"></a>Java基础</h2><h3 id=""><a href="#" class="headerlink" title=""></a></summary>
<category term="面试" scheme="http://example.com/tags/%E9%9D%A2%E8%AF%95/"/>
</entry>
<entry>
<title>记第一次在Github上pull request以及成为一个7.5k+项目的contributors</title>
<link href="http://example.com/2022/04/14/%E8%AE%B0%E7%AC%AC%E4%B8%80%E6%AC%A1%E5%9C%A8Github%E4%B8%8Apull%20request%E4%BB%A5%E5%8F%8A%E6%88%90%E4%B8%BA%E4%B8%80%E4%B8%AA7.5k+%E9%A1%B9%E7%9B%AE%E7%9A%84contributors/"/>
<id>http://example.com/2022/04/14/%E8%AE%B0%E7%AC%AC%E4%B8%80%E6%AC%A1%E5%9C%A8Github%E4%B8%8Apull%20request%E4%BB%A5%E5%8F%8A%E6%88%90%E4%B8%BA%E4%B8%80%E4%B8%AA7.5k+%E9%A1%B9%E7%9B%AE%E7%9A%84contributors/</id>
<published>2022-04-14T02:13:36.089Z</published>
<updated>2023-09-26T13:07:32.662Z</updated>
<content type="html"><![CDATA[<h1 id="记第一次在Github上pull-request以及成为一个7-5k-项目的contributors"><a href="#记第一次在Github上pull-request以及成为一个7-5k-项目的contributors" class="headerlink" title="记第一次在Github上pull request以及成为一个7.5k+项目的contributors"></a>记第一次在Github上pull request以及成为一个7.5k+项目的contributors</h1><p>项目地址:<a href="https://github.com/wolverinn/Waking-Up">https://github.com/wolverinn/Waking-Up</a> 这个项目是同学推荐给我的 用来复习操作系统,计算机网络,以及数据库</p><p>在复习的过程中,发现了作者有些问题有笔误和有些问题闲置下来一直没有补充,所以就在当天提了几个Issue </p><span id="more"></span><p><img src="https://s2.loli.net/2022/04/14/9gskCLieK63rXOf.png"></p><p>提了之后,每个Issue作者在晚上都回答了 并且语气非常的好</p><p>后面我就试探性的提了个pull request(应该就是所说的PR吧) 后面第二天早上,也就是今天早上作者回复了并且非常礼貌和谦虚的对我表示感谢。</p><p><img src="https://s2.loli.net/2022/04/14/JvgVcIMQRDEKb8N.png"></p><p>其实一直对contributors有着敬佩之情,曾经感到很遥远,但是在前几天,一个群友成为了大名鼎鼎的Redis的contributors,让我羡慕的同时,认为自己难道就不可以吗。所以后面我开始看项目的同时,并且提Issue来和作者交流。</p><p><img src="https://s2.loli.net/2022/04/14/XWrbuqo1NSYk2pl.png"></p><p>在我看来,与作者交流是一个非常使人受益的事,我遇见的作者态度都非常好,他们会和你交流并且指正你的不足。</p><p>后面就是碎碎念</p><p>找实习到这里,因为项目方面准备的很薄弱,以及学历没有优势,很多公司是笔试完石沉大海或者投简历就是石沉大海。</p><p>进行了两次面试,每次体验都挺好的,两个面试官人都很好,都会鼓励你。并且告诉你的不足之处。可惜第二家公司止步于HR面。</p><p>也由衷的感谢自己身边同学以及朋友在我低落的时候给予我极大的帮助。</p>]]></content>
<summary type="html"><h1 id="记第一次在Github上pull-request以及成为一个7-5k-项目的contributors"><a href="#记第一次在Github上pull-request以及成为一个7-5k-项目的contributors" class="headerlink" title="记第一次在Github上pull request以及成为一个7.5k+项目的contributors"></a>记第一次在Github上pull request以及成为一个7.5k+项目的contributors</h1><p>项目地址:<a href="https://github.com/wolverinn/Waking-Up">https://github.com/wolverinn/Waking-Up</a> 这个项目是同学推荐给我的 用来复习操作系统,计算机网络,以及数据库</p>
<p>在复习的过程中,发现了作者有些问题有笔误和有些问题闲置下来一直没有补充,所以就在当天提了几个Issue </p></summary>
<category term="Github" scheme="http://example.com/tags/Github/"/>
</entry>
<entry>
<title>Redis-Redis学习</title>
<link href="http://example.com/2022/04/13/Redis/"/>
<id>http://example.com/2022/04/13/Redis/</id>
<published>2022-04-13T06:11:39.037Z</published>
<updated>2023-09-26T13:08:41.080Z</updated>
<content type="html"><![CDATA[<h1 id="Redis"><a href="#Redis" class="headerlink" title="Redis"></a>Redis</h1><h2 id="1-NoSQL数据库-Not-Only-SQL"><a href="#1-NoSQL数据库-Not-Only-SQL" class="headerlink" title="1.NoSQL数据库(Not Only SQL)"></a>1.NoSQL数据库(Not Only SQL)</h2><h3 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h3><p>是一种非关系型数据库 </p><p>我们以前学的Mysql Oracle都属于关系式数据库</p><span id="more"></span><h2 id="特点:"><a href="#特点:" class="headerlink" title="特点:"></a>特点:</h2><ul><li>Redis不支持ACID</li><li>不遵循SQL标准</li><li>远超于SQL的性能</li></ul><h2 id="适用场景"><a href="#适用场景" class="headerlink" title="适用场景"></a>适用场景</h2><p>对数据高并发的读写</p><p>海量数据的读写</p><p>对数据高可扩展醒的</p><h2 id="不适用场景"><a href="#不适用场景" class="headerlink" title="不适用场景"></a>不适用场景</h2><ul><li>需要事物支持</li><li>基于sql的结构化查询存储,处理复杂的关系。</li></ul><p>扩展: MongoDB 文档型数据库</p><p> Memcached 是以前的NoSQL数据库</p><h2 id="Redis安装"><a href="#Redis安装" class="headerlink" title="Redis安装"></a>Redis安装</h2><p>官网下载地址 <a href="https://redis.io/download/">https://redis.io/download/</a></p><p>推荐使用工具Xshell + XFTP 在Linux服务器上启动</p><p>后台启动命令</p><p>redis-cli</p><p>Redis是一个开源的key-value存储系统</p><p>和Memcached类似,它支持存储value的类型相对更多,包括String(字符串)、list(链表)、set(集合)、zset(sorted set–有序集合)和hash(哈希类型)。</p><p>这些数据类型都支持push/pop、add/remove 及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。</p><p>在此基础上,Redis支持各种不同方式的排序。</p><p>与memcached一样,为了保证效率,数据都是缓存在内存中。</p><p>常用五大数据类型</p><ul><li>String </li><li>List</li><li>Set</li><li>Hash</li><li>sorted set</li></ul><h2 id="Redis键-Key"><a href="#Redis键-Key" class="headerlink" title="Redis键(Key)"></a>Redis键(Key)</h2><p>key *查看当前库所有key</p><p>exist key 判断某个key是否存在</p><p>type key查看你的key是什么类型</p><p>del key删除指定的key数据</p><p>unlink key 根据value选择非阻塞删除</p><p>expire key 10 10秒钟:为给定的Key设置过期时间</p><p>tll key查看还有多少秒过期 -1白送永不过期 -2表示已经过期。</p><p>select命令切换数据库</p><p>dbsize 查看当前数据库的key的数量</p><p>flushdb 清空当前库</p><p>flushall 通杀全部库</p><p>String类型是Redis最基本的数据类型,一个Redis中字符串value最多可以是512M</p><h2 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h2><p>set <key> <value></p><p>添加键值对</p><p>例子</p><p><code>set 1 2</code></p><p><code> get 1</code> 结果为2</p><p><code>set 1 3</code> 当key相同时,后面的value会覆盖前面的</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220414135627.png"></p><p>append 类似于StringBuilder的append()方法</p><p><code>append 1 100</code> 3+100 ->3100 返回的是字符串添加后的长度</p><p>strlen 类似于length()方法</p><p><code>strlen 1 </code> 求key为1的字符串长度</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220414135935.png"></p><p>setnx <key><value>只有在 key 不存在时 设置 key 的值</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220414140042.png"></p><p><code>incr</code> 将key储存的value加1 只能对数字类型进行操作 如果为空,新增值为1</p><p><code>decr</code> 将key储存的value-1 只能对数字值操作,如果为空,新增值为-1</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220414140622.png"></p><p>incrby / decrby <key><步长>将 key 中储存的数字值增减。自定义步长。</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220414140706.png"></p><p>mset<key1><value><key2><value2>…</p><p>同时设置一个或多个key-value对</p><p>mget<key1><key2>…</p><p>同时获取一个或者多个value</p><p>msetnx<key1><value><key2><vallue2>…</p><p>同时设置一个或者多个key-value对,当且仅当所有给定key都不存在。</p><p><strong>原子性 有一个失败则都失败。</strong></p><h2 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h2><p>String的数据结构为简单动态字符串。是可以修改的字符串,内部结构实现上类似于Java的ArrayList,采用预分配冗余的方式来减少内存的频繁分配</p><h1 id="Redis-三-列表-List"><a href="#Redis-三-列表-List" class="headerlink" title="Redis(三) 列表(List)"></a>Redis(三) 列表(List)</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>单键多值</p><p>Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。</p><p>它的底层实际是个<strong>双向链表</strong> 对两端的操作性能都很高,通过索引下标的操作中间的节点性能会较差。</p><h2 id="常用命令-1"><a href="#常用命令-1" class="headerlink" title="常用命令"></a>常用命令</h2><p><code>Ipush/rpush</code><key><value1><value2><values2><value></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419143554.png"></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419144025.png"></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419144044.png"></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419195322.png"></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419195407.png"></p><p>lpop把最左边吞出 rpop把最右边吞出 </p><p>不出意外我们k1列表里应该是 V2 V1 V2 V3 V4 V5的顺序</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419195545.png"></p><p><code>lrange k1 0 -1</code> lrange 表示从左边开始的范围 0表示左边第一个 -1表示右边第一个</p><p>以此类推 1表示左边第二个,-2表示右边第二个</p><p><code>lindex k1 3</code>表示从左边输出k1中序列为3的value值 (下标为0开始 所以这个是左边第四个)</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419195739.png"></p><p>获取k1的长度 <code>llen k1</code></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419195857.png"></p><p><code>linsert k1 before v1 v7</code>表示在v1之前插入v7</p><p>v1不能有重复 否则会返回-1</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419200100.png"></p><p><code>lrem key n value</code>删除左边前n个的value元素</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419200244.png"></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419200432.png"></p><p><code>lrem k1 2 v2 </code> </p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419200532.png"></p><h2 id="数据结构-1"><a href="#数据结构-1" class="headerlink" title="数据结构"></a>数据结构</h2><p>QuickList</p><p>在列数元素较少的情况下 会使用一块连续内存</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419200713.png"></p><h1 id="Redis-四-Set"><a href="#Redis-四-Set" class="headerlink" title="Redis(四) Set"></a>Redis(四) Set</h1><h2 id="简介-1"><a href="#简介-1" class="headerlink" title="简介"></a>简介</h2><p>Redis set对外提供的功能与list类似是一个列表的功能,特殊之处在于set是可以****自动排重****的,当你需要存储一个列表数据,又不希望出现重复数据时,set是一个很好的选择,并且set提供了判断某个成员是否在一个set集合内的重要接口,这个也是list所不能提供的。</p><p>Redis的Set是string类型的无序集合。它底层其实是一个value为null的hash表,所以添加,删除,查找的****复杂度都是O(1)****。</p><p>一个算法,随着数据的增加,执行时间的长短,如果是O(1),数据增加,查找数据的时间不变</p><p>set类似于HashSet 是无序去重的</p><h2 id="常用命令-2"><a href="#常用命令-2" class="headerlink" title="常用命令"></a>常用命令</h2><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419201507.png"></p><p>刚开始报错是 已经存在一个key了 就是我直接用的list的k1还存在 无法存入k1为key的set集合</p><p><code>sadd k8 v1 v1 v2</code></p><p>使用<code>smembers k8</code>展示 发现两个v1只存在于一个 证明Set的去重</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419201805.png"></p><p><code>sismember k8 v1</code>判断k8中是否含有v1这样的值</p><p>如果有返回1 没有返回0</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419201915.png"></p><p> <code>scard k8</code>k8的元素个数</p><p><code>sream k8 v1 v2</code> 删除k8里面的v1与v2元素</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419202124.png"></p><p><code>spop k8</code> 表示随机吐出k8的value并删除</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419202227.png"></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419202332.png"></p><p>srandmember <key><n>随机从该集合中取出n个值。不会从集合中删除 。</p><p><code>srandmember k10 5</code>随机从k10里面吐出5个元素并且不会删除</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419202439.png"></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419202538.png"></p><p>smove <source><destination>value把集合中一个值从一个集合移动到另一个集合</p><p><code>smove k10 k11 v2</code> 把k10里面的v2移入到k11里面去</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419202811.png"></p><p>sinter <key1><key2>返回两个集合的交集元素。</p><p>sunion <key1><key2>返回两个集合的并集元素。</p><p>sdiff <key1><key2>返回两个集合的****差集****元素(key1中的,不包含key2中的)</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419202942.png"></p><h2 id="数据结构-2"><a href="#数据结构-2" class="headerlink" title="数据结构"></a>数据结构</h2><p>Set数据结构是dict字典,字典是用哈希表实现的。</p><p>Java中HashSet的内部实现使用的是HashMap,只不过所有的value都指向同一个对象。Redis的set结构也是一样,它的内部也使用hash结构,所有的value都指向同一个内部值。</p><h1 id="Redis-五-Hash"><a href="#Redis-五-Hash" class="headerlink" title="Redis(五) Hash"></a>Redis(五) Hash</h1><h2 id="简介-2"><a href="#简介-2" class="headerlink" title="简介"></a>简介</h2><p>Redis hash 是一个键值对集合</p><p>Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。</p><p>类似Java里面的Map<String,Object></p><h2 id="常用命令-3"><a href="#常用命令-3" class="headerlink" title="常用命令"></a>常用命令</h2><p>hset <key><field><value>给<key>集合中的 <field>键赋值<value></p><p>hget <key1><field>从<key1>集合<field>取出 value </p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419203151.png"></p><p><code>hmset <key1><field1><value1><field2><value2>... 批量设置hash的值</code></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419203325.png"></p><p><code>hexists<key1><field>查看哈希表 key 中,给定域 field 是否存在。</code></p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419203443.png"></p><p>如果存在返回1 不存在返回0</p><p>hkeys <key>列出该hash集合的所有field</p><p>hvals <key>列出该hash集合的所有value</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419203542.png"></p><p>hincrby <key><field><increment>为哈希表 key 中的域 field 的值加上增量 1 -1</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419203717.png"></p><p>id增长为2了</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419203800.png"></p><p>hsetnx <key><field><value>将哈希表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在 .</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419203852.png"></p><p>设置失败 因为field id存在</p><h2 id="数据结构-3"><a href="#数据结构-3" class="headerlink" title="数据结构"></a>数据结构</h2><p>Hash类型对应的数据结构是两种:ziplist(压缩列表),hashtable(哈希表)。当field-value长度较短且个数较少时,使用ziplist,否则使用hashtable。</p><h1 id="Redis-六-Zest-sorted-set"><a href="#Redis-六-Zest-sorted-set" class="headerlink" title="Redis(六) Zest(sorted set)"></a>Redis(六) Zest(sorted set)</h1><h2 id="简介-3"><a href="#简介-3" class="headerlink" title="简介"></a>简介</h2><p>Redis有序集合zset与普通集合set非常相似,是一个没有重复元素的字符串集合。</p><p>不同之处是有序集合的每个成员都关联了一个<em><strong>*评分(score)*</strong></em>,这个评分(score)被用来按照从最低分到最高分的方式排序集合中的成员。集合的成员是唯一的,但是评分可以 是重复了 。</p><p>因为元素是有序的, 所以你也可以很快的根据评分(score)或者次序(position)来获取一个范围的元素。</p><p>访问有序集合的中间元素也是非常快的,因此你能够使用有序集合作为一个没有重复成员的智能列表。</p><h2 id="常用命令-4"><a href="#常用命令-4" class="headerlink" title="常用命令"></a>常用命令</h2><p>zadd <key><score1><value1><score2><value2>…</p><p>将一个或多个 member 元素及其 score 值加入到有序集 key 当中。</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419204852.png"></p><p><em><strong>*zrange <key><start><stop> [WITHSCORES]*</strong></em> </p><p>返回有序集 key 中,下标在<start><stop>之间的元素</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419204936.png"></p><p>会按照分数顺序返回</p><p>带WITHSCORES,可以让分数一起和值返回到结果集。</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419205032.png"></p><p>zrangebyscore key minmax [withscores] [limit offset count]</p><p>返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。有序集成员按 score 值递增(从小到大)次序排列。 </p><p>zrevrangebyscore key maxmin [withscores] [limit offset count] </p><p>同上,改为从大到小排列。 </p><p>zincrby <key><increment><value> 为元素的score加上增量</p><p>zrem <key><value>删除该集合下,指定值的元素 </p><p>zcount <key><min><max>统计该集合,分数区间内的元素个数 </p><p>zrank <key><value>返回该值在集合中的排名,从0开始。</p><p>案例:如何利用zset实现一个文章访问量的排行榜?</p><p><img src="C:\Users\peng\AppData\Local\Temp\ksohtml\wpsD021.tmp.png" alt="img"></p><h2 id="数据结构-4"><a href="#数据结构-4" class="headerlink" title="数据结构"></a>数据结构</h2><p>SortedSet(zset)是Redis提供的一个非常特别的数据结构,一方面它等价于Java的数据结构Map<String, Double>,可以给每一个元素value赋予一个权重score,另一方面它又类似于TreeSet,内部的元素会按照权重score进行排序,可以得到每个元素的名次,还可以通过score的范围来获取元素的列表。</p><p>zset底层使用了两个数据结构</p><p>(1)hash,hash的作用就是关联元素value和权重score,保障元素value的唯一性,可以通过元素value找到相应的score值。</p><p>(2)跳跃表,跳跃表的目的在于给元素value排序,根据score的范围获取元素列表。</p><p><em><strong>*跳跃表(跳表)*</strong></em></p><p>1、简介</p><p> 有序集合在生活中比较常见,例如根据成绩对学生排名,根据得分对玩家排名等。对于有序集合的底层实现,可以用数组、平衡树、链表等。数组不便元素的插入、删除;平衡树或红黑树虽然效率高但结构复杂;链表查询需要遍历所有效率低。Redis采用的是跳跃表。跳跃表效率堪比红黑树,实现远比红黑树简单。</p><p>2、实例</p><p> 对比有序链表和跳跃表,从链表中查询出51</p><p>(1) 有序链表</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419205322.png"> </p><p>要查找值为51的元素,需要从第一个元素开始依次查找、比较才能找到。共需要6次比较。</p><p>(2) 跳跃表</p><p><img src="http://windxiao-1307340949.cosgz.myqcloud.com/20220419205344.png"> </p><p>从第2层开始,1节点比51节点小,向后比较。</p><p>21节点比51节点小,继续向后比较,后面就是NULL了,所以从21节点向下到第1层</p><p>在第1层,41节点比51节点小,继续向后,61节点比51节点大,所以从41向下</p><p>在第0层,51节点为要查找的节点,节点被找到,共查找4次。</p><p>从此可以看出跳跃表比有序链表效率要高</p>]]></content>
<summary type="html"><h1 id="Redis"><a href="#Redis" class="headerlink" title="Redis"></a>Redis</h1><h2 id="1-NoSQL数据库-Not-Only-SQL"><a href="#1-NoSQL数据库-Not-Only-SQL" class="headerlink" title="1.NoSQL数据库(Not Only SQL)"></a>1.NoSQL数据库(Not Only SQL)</h2><h3 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h3><p>是一种非关系型数据库 </p>
<p>我们以前学的Mysql Oracle都属于关系式数据库</p></summary>
<category term="Redis" scheme="http://example.com/tags/Redis/"/>
</entry>
<entry>
<title>阅文笔试2021笔试题复习</title>
<link href="http://example.com/2022/04/07/%E9%98%85%E6%96%87%E7%AC%94%E8%AF%95%E5%A4%8D%E4%B9%A0/"/>
<id>http://example.com/2022/04/07/%E9%98%85%E6%96%87%E7%AC%94%E8%AF%95%E5%A4%8D%E4%B9%A0/</id>
<published>2022-04-07T00:17:05.425Z</published>
<updated>2023-09-26T13:10:50.673Z</updated>
<content type="html"><![CDATA[<h1 id="阅文笔试复习"><a href="#阅文笔试复习" class="headerlink" title="阅文笔试复习"></a>阅文笔试复习</h1><h2 id="1-详细描述ThreadPoolExecutor的各个参数的含义,介绍一个任务提交到线程池后的执行流程"><a href="#1-详细描述ThreadPoolExecutor的各个参数的含义,介绍一个任务提交到线程池后的执行流程" class="headerlink" title="1.详细描述ThreadPoolExecutor的各个参数的含义,介绍一个任务提交到线程池后的执行流程"></a>1.详细描述ThreadPoolExecutor的各个参数的含义,介绍一个任务提交到线程池后的执行流程</h2><ul><li><p>corePoolSize : 线程池的核心大小,也可以理解为最小的线程池大小。</p></li><li><p>maximinPoolSize : 最大线程池大小</p></li><li><p>keepAliveTime:空余线程存活时间,指的是超过corePoolSize的空余线程达到多长时间才进行销毁。</p></li><li><p>util:销毁时间单位</p></li><li><p>WorkQueue:存储等待执行线程的工作队列。</p></li><li><p>threadFactory:创建线程的工厂,一般用默认即可。</p></li><li><p>handle:拒绝策略,当工厂队列,线程池全满时如何拒绝新任务,默认抛出异常</p><span id="more"></span></li></ul><h2 id="2-请简要说明Servlet中的生命周期"><a href="#2-请简要说明Servlet中的生命周期" class="headerlink" title="2.请简要说明Servlet中的生命周期"></a>2.请简要说明Servlet中的生命周期</h2><p>1.Servlet初始化后调用Init()方法</p><p>2.Servlet调用service()方法来处理客户端的请求。</p><p>3.Servlet销毁前调用destroy()方法终止</p><h2 id="3-开启两个线程A-B,打印1到10-线程A打印奇数-1-3-5-7-9-线程B打印偶数-2-4-6-8-10"><a href="#3-开启两个线程A-B,打印1到10-线程A打印奇数-1-3-5-7-9-线程B打印偶数-2-4-6-8-10" class="headerlink" title="3.开启两个线程A,B,打印1到10 线程A打印奇数(1,3,5,7,9),线程B打印偶数(2,4,6,8,10)."></a>3.开启两个线程A,B,打印1到10 线程A打印奇数(1,3,5,7,9),线程B打印偶数(2,4,6,8,10).</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.yuewen;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.locks.LockSupport;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ABXianC</span> </span>{</span><br><span class="line"> <span class="keyword">static</span> Thread thread1;</span><br><span class="line"> <span class="keyword">static</span> Thread thread2;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> thread1 = <span class="keyword">new</span> Thread(() -> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">9</span>; i += <span class="number">2</span>) {</span><br><span class="line"> System.out.println(i);</span><br><span class="line"> LockSupport.unpark(thread2);</span><br><span class="line"> LockSupport.park();</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> thread2 = <span class="keyword">new</span> Thread(() -> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i <= <span class="number">10</span>; i = i + <span class="number">2</span>) {</span><br><span class="line"> LockSupport.park();</span><br><span class="line"> System.out.println(i);</span><br><span class="line"> LockSupport.unpark(thread1);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> thread1.start();</span><br><span class="line"> thread2.start();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="4-请编写代码实现单例模式-类名为Singletion"><a href="#4-请编写代码实现单例模式-类名为Singletion" class="headerlink" title="4.请编写代码实现单例模式,类名为Singletion"></a>4.请编写代码实现单例模式,类名为Singletion</h2><h3 id="1-饿汉模式"><a href="#1-饿汉模式" class="headerlink" title="1.饿汉模式"></a>1.饿汉模式</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Singleton</span></span>{</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">private</span> Singleton instance = <span class="keyword">new</span> Singleton();<span class="comment">//因为无法实例化,所以必须是静态的</span></span><br><span class="line"> <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> Singleton <span class="title">getInstance</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> instance;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="2-懒汉线程安全"><a href="#2-懒汉线程安全" class="headerlink" title="2.懒汉线程安全"></a>2.懒汉线程安全</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.yuewen;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Singletion</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> Singleton instance;</span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="title">Singletion</span><span class="params">()</span></span>{};</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">synchronized</span> Singleton <span class="title">getInstance</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(instance == <span class="keyword">null</span>) instance = <span class="keyword">new</span> Singleton();</span><br><span class="line"> <span class="keyword">return</span> instance;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="5-写一个Map转换成JavaBean的工具类方法,实现如下mapToObject方法-使用Java反射,不允许使用第三方库"><a href="#5-写一个Map转换成JavaBean的工具类方法,实现如下mapToObject方法-使用Java反射,不允许使用第三方库" class="headerlink" title="5.写一个Map转换成JavaBean的工具类方法,实现如下mapToObject方法(使用Java反射,不允许使用第三方库)"></a>5.写一个Map转换成JavaBean的工具类方法,实现如下mapToObject方法(使用Java反射,不允许使用第三方库)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Object <span class="title">mapToObject</span><span class="params">(Map<String,Object> map,Class<?> beanClass)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(map == <span class="keyword">null</span>) <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line"> Object obj = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">try</span>{</span><br><span class="line"> obj = beanClass.newInstance();</span><br><span class="line"> Field[] fields = obj.getClass().getDeclaredFields();</span><br><span class="line"> <span class="keyword">for</span>(Field field : fields)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> mod = field.getModifiers();</span><br><span class="line"> <span class="keyword">if</span>(Modifier.isStatic(mod) || Modifier.isFinal(mod)){</span><br><span class="line"><span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> field.setAccessible(<span class="keyword">true</span>);</span><br><span class="line"> field.set(obj,map.get(field.getName()));</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">catch</span>(Exception e)</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> obj;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="6-数据库操作是我们经常使用的一个技能,-请你完成一个简单的用户密码验证过程-,给定的条件如下:"><a href="#6-数据库操作是我们经常使用的一个技能,-请你完成一个简单的用户密码验证过程-,给定的条件如下:" class="headerlink" title="6.数据库操作是我们经常使用的一个技能, 请你完成一个简单的用户密码验证过程 ,给定的条件如下:"></a>6.数据库操作是我们经常使用的一个技能, 请你完成一个简单的用户密码验证过程 ,给定的条件如下:</h2><p>数据库中存在个用户表:users ,表结构如下:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE `users` (</span><br><span class="line"> `uid` bigint(20) NOT NULL COMMENT '用户ID',</span><br><span class="line"> `user_name` varchar(32) NOT NULL COMMENT '用户账号',</span><br><span class="line"> `password` varchar(64) NOT NULL COMMENT '用户混淆密码',</span><br><span class="line"> PRIMARY KEY (`uid`),</span><br><span class="line"> UNIQUE KEY `u_user_name` (`user_name`)</span><br><span class="line">) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表'</span><br></pre></td></tr></table></figure><p>完善以下方法</p><p>public boolean verifyPassword(String username,String password) {<br> Connection con = getConnection () ;// getConnection() 方法是个已有的方法可以获取到数据库连接 ,</p><p> // here is your code<br>}</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">verifyPassword</span><span class="params">(String username,String password)</span></span>{</span><br><span class="line"> Connection con=getConnection;</span><br><span class="line"> String sql=<span class="string">"SELECT password FROM users WHERE user_name=?"</span>;</span><br><span class="line"> PreparedStatement pst=<span class="keyword">null</span>;</span><br><span class="line"> ResultSet rs=<span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">boolean</span> flag=<span class="keyword">false</span>;</span><br><span class="line"> <span class="keyword">try</span>{</span><br><span class="line"> pst=con.prepareStatement(sql);</span><br><span class="line"> pst.setObject(<span class="number">1</span>,username);</span><br><span class="line"> rs=pst.executeQuery();</span><br><span class="line"> <span class="keyword">while</span>(rs.next()){</span><br><span class="line"> <span class="keyword">if</span>(rs.getString(<span class="string">"password"</span>).equals(password)){</span><br><span class="line"> flag=<span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }<span class="keyword">catch</span>(ClassNotFoundException e){</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }<span class="keyword">catch</span> (SQLException e){</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }<span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">try</span>{</span><br><span class="line"> <span class="keyword">if</span>(rs!=<span class="keyword">null</span>) rs.close();</span><br><span class="line"> <span class="keyword">if</span>(pst!=<span class="keyword">null</span>) pst.close();</span><br><span class="line"> <span class="keyword">if</span>(con!=<span class="keyword">null</span>) con.close();</span><br><span class="line"> }<span class="keyword">catch</span>(SQLException e){</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> flag;</span><br><span class="line"> }</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="7-介绍HashMap的数据结构、扩容机制,HashMap与Hashtable的区别,是否是线程安全的,并介绍ConcurrentHashMap的实现机制。"><a href="#7-介绍HashMap的数据结构、扩容机制,HashMap与Hashtable的区别,是否是线程安全的,并介绍ConcurrentHashMap的实现机制。" class="headerlink" title="7.介绍HashMap的数据结构、扩容机制,HashMap与Hashtable的区别,是否是线程安全的,并介绍ConcurrentHashMap的实现机制。"></a>7.介绍HashMap的数据结构、扩容机制,HashMap与Hashtable的区别,是否是线程安全的,并介绍ConcurrentHashMap的实现机制。</h2><p>HashMap JDK1.8之前 数组+链表</p><p>JDK1.8之后 数组+链表+红黑树</p><h3 id="1-数组结构"><a href="#1-数组结构" class="headerlink" title="1.数组结构"></a>1.数组结构</h3><p>数组用于存储内容,链表(红黑树)用于解决hash冲突。如果链表长度大于阈值8,但是当前数组长度小于树化阈值64,则进行数组扩容操作;如果数组长度大于树化阈值64,则进行链表树化操作,将单向链表转化为红黑树结构。</p><h3 id="2-扩容机制:"><a href="#2-扩容机制:" class="headerlink" title="2.扩容机制:"></a>2.扩容机制:</h3><p>如果不指定容量,则初始容量默认为16。如果指定容量,则初始容量设置为大于指定容量的最小2的幂数。当当前容量大于容量*负载因子(默认为0.75)时进行扩容操作,扩容为原容量的2倍。</p><h3 id="3-HashMap与HashTable的区别"><a href="#3-HashMap与HashTable的区别" class="headerlink" title="3.HashMap与HashTable的区别"></a>3.HashMap与HashTable的区别</h3><p> 1)数据结构区别:HashMap为数组+链表(红黑树),HashTable为数组+链表,HashTable没有树化操作。</p><p> 2)扩容机制区别:未指定容量情况下,HashMap容量默认16,每次扩容为2n(n:原容量)。HashTable容量默认为11,每次扩容为2n+1(n:原容量)。指定容量情况下,HashMap将保证容量为2的幂数,HashTable将直接使用指定容量。</p><p> 3)数据插入方式的区别:当发生hash冲突时,HashMap使用尾插法插入链表,HashTable使用头插法插入链表。</p><p> 4)线程安全区别:HashMap是非线程安全的,HashTable因为使用synchronized修饰方法,所以HashTable是线程安全的。</p><p>ConcurrentHashMap的实现机制</p><p> 1)ConcurrentHashMap通过synchronized关键字和CAS操作实现线程安全,若插入的槽没有数据,使用CAS操作执行插入操作,若插入的槽有数据,通过synchronized锁住链表的头节点,从而实现效率与线程安全的平衡</p><h2 id="8-介绍数据库连接池的实现方式。如何从连接池中获取连接、将连接放回连接池?使用连接池的优势是什么?列举一下自己用过的连接池。"><a href="#8-介绍数据库连接池的实现方式。如何从连接池中获取连接、将连接放回连接池?使用连接池的优势是什么?列举一下自己用过的连接池。" class="headerlink" title="8.介绍数据库连接池的实现方式。如何从连接池中获取连接、将连接放回连接池?使用连接池的优势是什么?列举一下自己用过的连接池。"></a>8.介绍数据库连接池的实现方式。如何从连接池中获取连接、将连接放回连接池?使用连接池的优势是什么?列举一下自己用过的连接池。</h2><h3 id="连接池实现原理:"><a href="#连接池实现原理:" class="headerlink" title="连接池实现原理:"></a>连接池实现原理:</h3><p>1.用户给servlet发送请求,请求Dao要Connection</p><p>2.Dao从“连接池”中取出Connection资源,与DB的通讯</p><p>3.当用户离开之后,释放该Connection,那么该Connection被释放到连接池中,等待下一个用户来</p><p>Demo目标:</p><p>通过简单的增删改查来做到下面几个关于连接池的方式,让我们更了解几种优化的方式</p><p>1.自定义一个Pool,来实现类似于现在开源连接池为我们做的一些操作</p><p>2.使用Tomcat内置的连接池(apache dbcp)</p><p>3.使用DBCP数据库连接池</p><p>4.使用C3P0数据库连接池(推荐)</p><h3 id="数据库连接池技术带来的优势:"><a href="#数据库连接池技术带来的优势:" class="headerlink" title="数据库连接池技术带来的优势:"></a><strong>数据库连接池技术带来的优势</strong>:</h3><p>1. 资源重用</p><p>由于数据库连接得到重用,避免了频繁创建、释放连接引起的大量性能开销。在减少系统消耗的基础上,另一方面也增进了系统运行环境的平稳性(减少内存碎片以及数据库临时进程/线程的数量)。</p><p>2. 更快的系统响应速度</p><p>数据库连接池在初始化过程中,往往已经创建了若干数据库连接置于池中备用。此时连接的初始化工作均已完成。对于业务请求处理而言,直接利用现有可用连接,避免了数据库连接初始化和释放过程的时间开销,从而缩减了系统整体响应时间。</p><p>3. 新的资源分配手段</p><p>对于多应用共享同一数据库的系统而言,可在应用层通过数据库连接的配置,实现数据库连接池技术,几年钱也许还是个新鲜话题,对于目前的业务系统而言,如果设计中还没有考虑到连接池的应用,那么…….快在设计文档中加上这部分的内容吧。某一应用最大可用数据库连接数的限制,避免某一应用独占所有数据库资源。</p><p>4. 统一的连接管理,避免数据库连接泄漏</p><p>在较为完备的数据库连接池实现中,可根据预先的连接占用超时设定,强制收回被占用连接。从而避免了常规数据库连接操作中可能出现的资源泄漏。一个最小化的数据库连接池实现:</p><h2 id="9-什么是死锁?JAVA程序中什么情况下回出现死锁?如何避免出现死锁?"><a href="#9-什么是死锁?JAVA程序中什么情况下回出现死锁?如何避免出现死锁?" class="headerlink" title="9.什么是死锁?JAVA程序中什么情况下回出现死锁?如何避免出现死锁?"></a>9.什么是死锁?JAVA程序中什么情况下回出现死锁?如何避免出现死锁?</h2><p>死锁是一种特定的程序状态,在实体之间,由于循环依赖导致彼此一直处于等待之中,没有任何个体可以继续前进。死锁不仅仅会发生在线程之间,存在资源独占的进程之间同样也可能出现死锁。通常来说,我们大多是聚焦在多线程场景中的死锁,指两个或多个线程之间,由于互相持有对方需要的锁,而永久处于阻塞的状态。</p><p>基本上死锁的发生是因为:互斥条件,类似Java中Monitor都是独占的,要么是我用,要么是你用。互斥条件是长期持有的,在使用结束之前,自己不会释放,也不能被其它线程抢占。循环依赖关系,两个或者多个个体之间出现了锁的链条环。<strong>免死锁的思路和方法。****1、</strong>如果可能的话,尽量避免使用多个锁,并且只有需要时才持有锁。<strong>2、</strong>如果必须使用多个锁,尽量设计好锁的获取顺序。</p><p><strong>3</strong>、使用带超时的方法,为程序带来更多可控性</p><h2 id="10-分布式锁有几种实现方式,并介绍每种方式的优缺点。"><a href="#10-分布式锁有几种实现方式,并介绍每种方式的优缺点。" class="headerlink" title="10. 分布式锁有几种实现方式,并介绍每种方式的优缺点。"></a>10. 分布式锁有几种实现方式,并介绍每种方式的优缺点。</h2><p>分布式锁一般有三种实现方式:<br>1、 数据库锁<br>2、基于Redis的分布式锁<br>3、基于ZooKeeper的分布式锁</p><h2 id="11-什么是TCP粘包拆包?为什么会出现粘包拆包?如何在应用层面解决此问题?"><a href="#11-什么是TCP粘包拆包?为什么会出现粘包拆包?如何在应用层面解决此问题?" class="headerlink" title="11. 什么是TCP粘包拆包?为什么会出现粘包拆包?如何在应用层面解决此问题?"></a>11. 什么是TCP粘包拆包?为什么会出现粘包拆包?如何在应用层面解决此问题?</h2><p> 如果客户端连续不断的向服务端发送数据包时,服务端接收的数据会出现两个数据包粘在一起的情况,这就是TCP协议中经常会遇到的粘包以及拆包的问题。</p><p>1、TCP是基于字节流的,虽然应用层和传输层之间的数据交互是大小不等的数据块,但是TCP把这些数据块仅仅看成一连串无结构的字节流,没有边界;</p><p>2、在TCP的首部没有表示数据长度的字段,</p><p>基于上面两点,在使用TCP传输数据时,才有粘包或者拆包现象发生的可能。</p><p>解决</p><p>1、发送端给每个数据包添加包首部,首部中应该至少包含数据包的长度,这样接收端在接收到数据后,通过读取包首部的长度字段,便知道每一个数据包的实际长度了。<br>2、发送端将每个数据包封装为固定长度(不够的可以通过补0填充),这样接收端每次从接收缓冲区中读取固定长度的数据就自然而然的把每个数据包拆分开来。<br>3、可以在数据包之间设置边界,如添加特殊符号,这样,接收端通过这个边界就可以将不同的数据包拆分开。</p><h2 id="12-请大致描述一下BIO,AIO和NIO的区别?"><a href="#12-请大致描述一下BIO,AIO和NIO的区别?" class="headerlink" title="12 请大致描述一下BIO,AIO和NIO的区别?"></a>12 请大致描述一下BIO,AIO和NIO的区别?</h2><p>BIO:同步阻塞式IO,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通过线程池机制改善。<br>NIO:同步非阻塞式IO,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才启动一个线程进行处理。<br>AIO:异步非阻塞式IO,服务器实现模式为一个有效请求一个线程,客户端的I/O请求都是由OS先完成了再通知服务器应用去启动线程进行处理。 </p><h2 id="13-在JAVA语法中加载类的的方式有哪些?"><a href="#13-在JAVA语法中加载类的的方式有哪些?" class="headerlink" title="13 在JAVA语法中加载类的的方式有哪些?"></a>13 在JAVA语法中加载类的的方式有哪些?</h2><p>1、创建类的实例(开辟地址空间)</p><p>2、访问某个静态类或接口的静态常量,或者对该静态变量赋值(类初始化)</p><p>3、调用类的静态访问(new,也会占用空间)</p><p>4、反射(类初始化)</p><p>5、初始化一个类的子类(继承)</p><p>6、JAVA虚拟机启动被称标明为启动类的类</p><p>7、调用某个 ClassLoader 实例的 loadClass() 方法(类不会初始化)</p><h2 id="14-建立三个线程A、B、C,A线程打印10次字母A,B线程打印10次字母B-C线程打印10次字母C,但是要求三个线程同时运行,并且实现交替打印,即按照ABCABCABC的顺序打印。"><a href="#14-建立三个线程A、B、C,A线程打印10次字母A,B线程打印10次字母B-C线程打印10次字母C,但是要求三个线程同时运行,并且实现交替打印,即按照ABCABCABC的顺序打印。" class="headerlink" title="14 建立三个线程A、B、C,A线程打印10次字母A,B线程打印10次字母B,C线程打印10次字母C,但是要求三个线程同时运行,并且实现交替打印,即按照ABCABCABC的顺序打印。"></a>14 建立三个线程A、B、C,A线程打印10次字母A,B线程打印10次字母B,C线程打印10次字母C,但是要求三个线程同时运行,并且实现交替打印,即按照ABCABCABC的顺序打印。</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.yuewen;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.locks.LockSupport;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ABC</span> </span>{</span><br><span class="line"> <span class="keyword">static</span> Thread A, B, C;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> A = <span class="keyword">new</span> Thread(() -> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {</span><br><span class="line"> LockSupport.park();</span><br><span class="line"> System.out.print(<span class="string">"A"</span>);</span><br><span class="line"> LockSupport.unpark(B);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> B = <span class="keyword">new</span> Thread(() -> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {</span><br><span class="line"> LockSupport.park();</span><br><span class="line"> System.out.print(<span class="string">"B"</span>);</span><br><span class="line"> LockSupport.unpark(C);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> C = <span class="keyword">new</span> Thread(() -> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {</span><br><span class="line"> LockSupport.unpark(A);</span><br><span class="line"> LockSupport.park();</span><br><span class="line"> System.out.print(<span class="string">"C"</span>);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> A.start();</span><br><span class="line"> B.start();</span><br><span class="line"> C.start();</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="15-请列举5个spring框架中的注解,并说明注解的用法以及使用场景"><a href="#15-请列举5个spring框架中的注解,并说明注解的用法以及使用场景" class="headerlink" title="15 请列举5个spring框架中的注解,并说明注解的用法以及使用场景"></a>15 请列举5个spring框架中的注解,并说明注解的用法以及使用场景</h2><ul><li>@component 标注一个POJO类</li><li>@Reposity 标注一个DAO类</li><li>@Service标注一个业务逻辑类</li><li>@Controller标注一个控制器类</li><li>@Autowired标注在字段或属性的setter 方法</li><li>@Before当前方面看成是前置通知</li><li>@After当前方面看成是始终通知</li><li>@Around当前方法看成环绕通知</li><li>@Aspect把当前类声明成切面类</li></ul><h2 id="16-给定一组自然数,数字的值有可能会大于2-64-,要求计算出所有数字的和"><a href="#16-给定一组自然数,数字的值有可能会大于2-64-,要求计算出所有数字的和" class="headerlink" title="16 给定一组自然数,数字的值有可能会大于2^64 ,要求计算出所有数字的和"></a>16 给定一组自然数,数字的值有可能会大于2^64 ,要求计算出所有数字的和</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 计算一组数字的和</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> numbers string字符串ArrayList 一组自然数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> string字符串</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">sum</span> <span class="params">(ArrayList<String> numbers)</span> </span>{</span><br><span class="line"> <span class="comment">// write code here</span></span><br><span class="line"></span><br><span class="line"> String result=<span class="string">"0"</span>;</span><br><span class="line"> <span class="keyword">for</span> (String number : numbers) {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span>(number==<span class="keyword">null</span>||number.length()==<span class="number">0</span>){</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> resultLen = result.length();</span><br><span class="line"> <span class="keyword">int</span> curNumLen = number.length();</span><br><span class="line"> <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> remain;</span><br><span class="line"> StringBuilder stringBuilder = <span class="keyword">new</span> StringBuilder();</span><br><span class="line"> <span class="keyword">while</span> (resultLen><span class="number">0</span>||curNumLen><span class="number">0</span>){</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> resultNum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(resultLen><span class="number">0</span>){</span><br><span class="line"> resultNum = result.charAt(--resultLen) - <span class="string">'0'</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> curNum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(curNumLen><span class="number">0</span>){</span><br><span class="line"> curNum = number.charAt(--curNumLen) - <span class="string">'0'</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sum=sum+resultNum+curNum;</span><br><span class="line"> remain=sum%<span class="number">10</span>;</span><br><span class="line"> stringBuilder.append(remain);</span><br><span class="line"> sum/=<span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span>(sum!=<span class="number">0</span>){</span><br><span class="line"> stringBuilder.append(sum);</span><br><span class="line"> }</span><br><span class="line"> result=stringBuilder.reverse().toString();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> String num1=<span class="string">"123456"</span>;</span><br><span class="line"> String num2=<span class="string">"123456789"</span>;</span><br><span class="line"> String num3=<span class="string">"123456789123"</span>;</span><br><span class="line"> ArrayList<String> strings = <span class="keyword">new</span> ArrayList<>();</span><br><span class="line"> strings.add(num1);</span><br><span class="line"> strings.add(num2);</span><br><span class="line"> strings.add(num3);</span><br><span class="line"> System.out.println(sum(strings));</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="17-给定一个int数字-要求计算出int数字对应的二进制中1的个数"><a href="#17-给定一个int数字-要求计算出int数字对应的二进制中1的个数" class="headerlink" title="17 给定一个int数字 要求计算出int数字对应的二进制中1的个数"></a>17 给定一个int数字 要求计算出int数字对应的二进制中1的个数</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.yuewen;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Erjinzhi</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">int</span> n = scanner.nextInt();</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (n > <span class="number">0</span>){</span><br><span class="line"> n = n & (n-<span class="number">1</span>);</span><br><span class="line"> ans++;</span><br><span class="line"> }</span><br><span class="line"> System.out.println(ans);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="18-根据产品策略某本书可以设置包月到期时间,需要计算指定时间到包月到期时间还有多少分钟-不足60S的不计入。"><a href="#18-根据产品策略某本书可以设置包月到期时间,需要计算指定时间到包月到期时间还有多少分钟-不足60S的不计入。" class="headerlink" title="18 根据产品策略某本书可以设置包月到期时间,需要计算指定时间到包月到期时间还有多少分钟,不足60S的不计入。"></a>18 根据产品策略某本书可以设置包月到期时间,需要计算指定时间到包月到期时间还有多少分钟,不足60S的不计入。</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">dateSub</span><span class="params">(String a, String b)</span> </span>{</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> SimpleDateFormat sdf = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>);</span><br><span class="line"> sdf.parse(a);</span><br><span class="line"> LocalDateTime t1 = LocalDateTime.parse(a, DateTimeFormatter.ofPattern(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>)),</span><br><span class="line"> t2 = LocalDateTime.parse(b, DateTimeFormatter.ofPattern(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>));</span><br><span class="line"> <span class="keyword">long</span> pass = t1.until(t2, ChronoUnit.MINUTES);</span><br><span class="line"> <span class="keyword">return</span> String.valueOf(Math.abs(pass));</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> System.out.println(String.format(<span class="string">"格式转化错误:%s, 检查是否格式输入错误"</span>, e.getMessage()));</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"0"</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> System.out.println(<span class="string">"请输入指定的两个日期【小者在前,大者在后,格式:yyyy-MM-dd hh:mm:ss】:"</span>);</span><br><span class="line"> Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> String a = sc.nextLine(), b = sc.nextLine();</span><br><span class="line"> sc.close();</span><br><span class="line"> System.out.println(dateSub(a, b));</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="19-map是一种开发过程中经常使用的k-v数据结构,有个map保存了书名和书字数的关系,编写代码对map里面的书按照字数进行升序排序"><a href="#19-map是一种开发过程中经常使用的k-v数据结构,有个map保存了书名和书字数的关系,编写代码对map里面的书按照字数进行升序排序" class="headerlink" title="19 map是一种开发过程中经常使用的k-v数据结构,有个map保存了书名和书字数的关系,编写代码对map里面的书按照字数进行升序排序"></a>19 map是一种开发过程中经常使用的k-v数据结构,有个map保存了书名和书字数的关系,编写代码对map里面的书按照字数进行升序排序</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Map<String, Integer> <span class="title">sortMap</span><span class="params">(Map<String, Integer> map)</span> </span>{</span><br><span class="line"> TreeMap<Integer, List<String>> treeMap = <span class="keyword">new</span> TreeMap<>();</span><br><span class="line"> map.entrySet().forEach(entry -> {</span><br><span class="line"> List<String> indexList = treeMap.computeIfAbsent(entry.getValue(), k -> <span class="keyword">new</span> ArrayList<>());</span><br><span class="line"> indexList.add(entry.getKey());</span><br><span class="line"> });</span><br><span class="line"> Map<String, Integer> result = <span class="keyword">new</span> ListOrderedMap();</span><br><span class="line"> treeMap.entrySet().forEach(entry -> {</span><br><span class="line"> entry.getValue().forEach(key -> result.put(key, map.get(key)));</span><br><span class="line"> });</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Map<String, Integer> <span class="title">sortMap2</span><span class="params">(Map<String, Integer> map)</span> </span>{</span><br><span class="line"> Map result = <span class="keyword">new</span> ListOrderedMap();</span><br><span class="line"> map.entrySet().stream().</span><br><span class="line"> sorted(Map.Entry.comparingByValue()).</span><br><span class="line"> forEachOrdered(entry -> result.put(entry.getKey(), entry.getValue()));</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="20-起点APP上允许用户对作品进行评论,为了防止用户恶意评论,发表不当内容,需要对用户发布的内容进行过滤,请写程序过滤用户发布内容中带有的QQ号(6-10位数字组成)-允许对内容严格操作,如用户发表了-作者大大666666,为你点赞-,经过过滤后也可以为作者大大-,为你点赞-,将666666过滤掉了。"><a href="#20-起点APP上允许用户对作品进行评论,为了防止用户恶意评论,发表不当内容,需要对用户发布的内容进行过滤,请写程序过滤用户发布内容中带有的QQ号(6-10位数字组成)-允许对内容严格操作,如用户发表了-作者大大666666,为你点赞-,经过过滤后也可以为作者大大-,为你点赞-,将666666过滤掉了。" class="headerlink" title="20 起点APP上允许用户对作品进行评论,为了防止用户恶意评论,发表不当内容,需要对用户发布的内容进行过滤,请写程序过滤用户发布内容中带有的QQ号(6~10位数字组成) 允许对内容严格操作,如用户发表了 作者大大666666,为你点赞 ,经过过滤后也可以为作者大大****,为你点赞 ,将666666过滤掉了。"></a>20 起点APP上允许用户对作品进行评论,为了防止用户恶意评论,发表不当内容,需要对用户发布的内容进行过滤,请写程序过滤用户发布内容中带有的QQ号(6~10位数字组成) 允许对内容严格操作,如用户发表了 <strong>作者大大666666,为你点赞</strong> ,经过过滤后也可以为<strong>作者大大****,为你点赞</strong> ,将666666过滤掉了。</h2><p>把6-10位的数字替换成””;</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">filterQQ</span><span class="params">(String s)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> s.replaceAll(<span class="string">"\\d{6,10}"</span>,<span class="string">""</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="21-质数-又称素数-是指在大于1的自然数中-除了1和它本身外-不能被其他自然数整除-除0以外-的数称之为素数-质数-。请写个程序判断输入的数字是否是质数,如果是素数请输出:true,不是请输出false"><a href="#21-质数-又称素数-是指在大于1的自然数中-除了1和它本身外-不能被其他自然数整除-除0以外-的数称之为素数-质数-。请写个程序判断输入的数字是否是质数,如果是素数请输出:true,不是请输出false" class="headerlink" title="21 质数(又称素数),是指在大于1的自然数中,除了1和它本身外,不能被其他自然数整除(除0以外)的数称之为素数(质数)。请写个程序判断输入的数字是否是质数,如果是素数请输出:true,不是请输出false"></a>21 质数(又称素数),是指在大于1的自然数中,除了1和它本身外,不能被其他自然数整除(除0以外)的数称之为素数(质数)。请写个程序判断输入的数字是否是质数,如果是素数请输出:true,不是请输出false</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> leecode;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IsPrimeDemo</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isPrime</span><span class="params">(<span class="keyword">int</span> n)</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (n < <span class="number">1</span>){</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> i = <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">int</span> end = (<span class="keyword">int</span>) Math.sqrt(n);</span><br><span class="line"> <span class="keyword">while</span> (i <= end ){</span><br><span class="line"> <span class="keyword">if</span> (n % i == <span class="number">0</span>){</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> ++i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = <span class="number">7</span>;</span><br><span class="line"> System.out.println(isPrime(n));</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="22-有-n-个台阶,你一次能走-1-个或者-2-个台阶,那么请问,走完这-n-个台阶共有几种方式?"><a href="#22-有-n-个台阶,你一次能走-1-个或者-2-个台阶,那么请问,走完这-n-个台阶共有几种方式?" class="headerlink" title="22 有 n 个台阶,你一次能走 1 个或者 2 个台阶,那么请问,走完这 n 个台阶共有几种方式?"></a>22 有 n 个台阶,你一次能走 1 个或者 2 个台阶,那么请问,走完这 n 个台阶共有几种方式?</h2><p>经典爬楼梯问题</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.yuewen;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ZouTaiJie</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"></span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">int</span> n = scanner.nextInt();</span><br><span class="line"> <span class="keyword">int</span>[] dp = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line"> <span class="keyword">int</span> i = <span class="number">2</span>;</span><br><span class="line"> dp[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> dp[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">while</span> (i < n)</span><br><span class="line"> {</span><br><span class="line"> dp[i] = dp[i-<span class="number">1</span>] + dp[i-<span class="number">2</span>];</span><br><span class="line"> i++;</span><br><span class="line"> }</span><br><span class="line"> System.out.println(dp[n-<span class="number">1</span>]);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="23-给定一个字符串,返回这个字符串中有多少个回文子串。两个相同的回文子串出现在不同的位置,认为是2个回文子串。a、aa、aaa、aba、aabaa、abcba均认为是回文子串。"><a href="#23-给定一个字符串,返回这个字符串中有多少个回文子串。两个相同的回文子串出现在不同的位置,认为是2个回文子串。a、aa、aaa、aba、aabaa、abcba均认为是回文子串。" class="headerlink" title="23 给定一个字符串,返回这个字符串中有多少个回文子串。两个相同的回文子串出现在不同的位置,认为是2个回文子串。a、aa、aaa、aba、aabaa、abcba均认为是回文子串。"></a>23 给定一个字符串,返回这个字符串中有多少个回文子串。两个相同的回文子串出现在不同的位置,认为是2个回文子串。a、aa、aaa、aba、aabaa、abcba均认为是回文子串。</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> str string字符串 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> int整型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">palindromeCount</span> <span class="params">(String str)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> center = <span class="number">0</span>; center < str.length(); center++) {</span><br><span class="line"> ans += expand(str, center, center) + expand(str, center, center + <span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">expand</span><span class="params">(String str, <span class="keyword">int</span> left, <span class="keyword">int</span> right)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (left >= <span class="number">0</span> && right < str.length() && str.charAt(left) == str.charAt(right)) {</span><br><span class="line"> ans++;</span><br><span class="line"> left--;</span><br><span class="line"> right++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>24 将一个给定的单链表反转,例:1-2-3-4-5,反转为5-4-3-2-1</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * public class ListNode {</span></span><br><span class="line"><span class="comment"> * int val;</span></span><br><span class="line"><span class="comment"> * ListNode next = null;</span></span><br><span class="line"><span class="comment"> * }</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> head ListNode类 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> ListNode类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> ListNode <span class="title">reverseList</span> <span class="params">(ListNode head)</span> </span>{</span><br><span class="line"> ListNode pre = <span class="keyword">null</span>, post = head, tmp;</span><br><span class="line"> <span class="keyword">while</span> (post != <span class="keyword">null</span>) {</span><br><span class="line"> tmp = post.next;<span class="comment">//临时工具人结点</span></span><br><span class="line"> post.next = pre;<span class="comment">//反转</span></span><br><span class="line"> pre = post;<span class="comment">//pre进1</span></span><br><span class="line"> post = tmp;<span class="comment">//post进1</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> pre;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="25-给定一个二叉树-找到该树中两个指定节点的最近公共祖先。"><a href="#25-给定一个二叉树-找到该树中两个指定节点的最近公共祖先。" class="headerlink" title="25 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。"></a>25 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。</h2><p>例:图中给定树 {3,5,1,6,2,0,8,#,#,7,4} 中,节点6、节点4的最近公共祖先为5。</p><p><img src="https://s2.loli.net/2022/04/07/PO3bN7DACYedMka.png"></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * public class TreeNode {</span></span><br><span class="line"><span class="comment"> * int val = 0;</span></span><br><span class="line"><span class="comment"> * TreeNode left = null;</span></span><br><span class="line"><span class="comment"> * TreeNode right = null;</span></span><br><span class="line"><span class="comment"> * }</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> root TreeNode类 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> p TreeNode类 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> q TreeNode类 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> TreeNode类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> TreeNode <span class="title">nearestCommonAncestor</span> <span class="params">(TreeNode root, TreeNode p, TreeNode q)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (root == <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (root.val == p.val || root.val == q.val) {</span><br><span class="line"> <span class="keyword">return</span> root;</span><br><span class="line"> }</span><br><span class="line"> TreeNode leftAns = nearestCommonAncestor(root.left, p, q), </span><br><span class="line"> rightAns = nearestCommonAncestor(root.right, p, q);</span><br><span class="line"> <span class="keyword">if</span> (leftAns != <span class="keyword">null</span> && rightAns != <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> root;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> leftAns != <span class="keyword">null</span> ? leftAns : rightAns;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="26-给定一个递增排序的数组,查找某个数字是否在数组中,如果在数组中,则返回该数字在数组中第一次出现的位置(从0开始);如果不在数组中,返回-1-。不需要考虑给定的数组不是递增的情况。务必使用二分查找的方式。"><a href="#26-给定一个递增排序的数组,查找某个数字是否在数组中,如果在数组中,则返回该数字在数组中第一次出现的位置(从0开始);如果不在数组中,返回-1-。不需要考虑给定的数组不是递增的情况。务必使用二分查找的方式。" class="headerlink" title="26 给定一个递增排序的数组,查找某个数字是否在数组中,如果在数组中,则返回该数字在数组中第一次出现的位置(从0开始);如果不在数组中,返回-1 。不需要考虑给定的数组不是递增的情况。务必使用二分查找的方式。"></a>26 给定一个递增排序的数组,查找某个数字是否在数组中,如果在数组中,则返回该数字在数组中第一次出现的位置(从0开始);如果不在数组中,返回-1 。不需要考虑给定的数组不是递增的情况。务必使用二分查找的方式。</h2><p>好像是错误的!</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">binarySearch</span><span class="params">(<span class="keyword">int</span>* arr, <span class="keyword">int</span> arrLen, <span class="keyword">int</span> a)</span> </span>{</span><br><span class="line"> <span class="comment">// write code here</span></span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> right = arrLen-<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> mid = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(left<=right)</span><br><span class="line"> {</span><br><span class="line"> mid = (left+right)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(arr[mid]==a)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(arr[mid-<span class="number">1</span>]!=a)</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> mid-<span class="number">1</span>;</span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(arr[mid]>a)</span><br><span class="line"> right = mid - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> left = mid + <span class="number">1</span>;</span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><h2 id="27-请编写程序实现矩阵的乘法"><a href="#27-请编写程序实现矩阵的乘法" class="headerlink" title="27 请编写程序实现矩阵的乘法"></a>27 请编写程序实现矩阵的乘法</h2><p><img src="https://s2.loli.net/2022/04/07/4QUJE3cRkhZxzHn.png"></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span>[][] matrixMuilty(<span class="keyword">int</span>[][] a, <span class="keyword">int</span>[][] b) {</span><br><span class="line"> <span class="keyword">int</span> m = a.length, p = b.length, n = b[<span class="number">0</span>].length;</span><br><span class="line"> <span class="keyword">int</span>[][] res = <span class="keyword">new</span> <span class="keyword">int</span>[m][n];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < n; j++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k < p; k++) {</span><br><span class="line"> res[i][j] += a[i][k] * b[k][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//输入三个整数,分别表示:第一个矩阵的 行 列【也就是第二个矩阵的行】 第二个矩阵的列</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> String s = sc.nextLine();</span><br><span class="line"> String[] ss = s.split(<span class="string">","</span>);</span><br><span class="line"> <span class="keyword">int</span> m = Integer.parseInt(ss[<span class="number">0</span>]), p = Integer.parseInt(ss[<span class="number">1</span>]), n = Integer.parseInt(ss[<span class="number">2</span>]);</span><br><span class="line"> <span class="keyword">if</span> (m == <span class="number">0</span> || p == <span class="number">0</span> || n == <span class="number">0</span>) {</span><br><span class="line"> sc.close();</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span>[][] a = <span class="keyword">new</span> <span class="keyword">int</span>[m][p];</span><br><span class="line"> <span class="keyword">int</span>[][] b = <span class="keyword">new</span> <span class="keyword">int</span>[p][n];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> s = sc.nextLine();</span><br><span class="line"> ss = s.split(<span class="string">","</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < p; j++) {</span><br><span class="line"> a[i][j] = Integer.parseInt(ss[j]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < p; i++) {</span><br><span class="line"> s = sc.nextLine();</span><br><span class="line"> ss = s.split(<span class="string">","</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < n; j++) {</span><br><span class="line"> b[i][j] = Integer.parseInt(ss[j]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> sc.close();</span><br><span class="line"> <span class="keyword">int</span>[][] res = matrixMuilty(a, b);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> StringBuilder sb = <span class="keyword">new</span> StringBuilder();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < n; j++) {</span><br><span class="line"> sb.append(res[i][j]).append(<span class="string">","</span>);</span><br><span class="line"> }</span><br><span class="line"> System.out.println(sb.substring(<span class="number">0</span>, sb.length() - <span class="number">1</span>));</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><h2 id="28-求出一个正整数转换成二进制后的数字“1”的个数。"><a href="#28-求出一个正整数转换成二进制后的数字“1”的个数。" class="headerlink" title="28 求出一个正整数转换成二进制后的数字“1”的个数。"></a>28 求出一个正整数转换成二进制后的数字“1”的个数。</h2><p>例:数字23转为二进制为 10111,其中1的个数为4</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">binaryTo</span><span class="params">(<span class="keyword">int</span> num)</span> </span>{ <span class="keyword">int</span> sum = <span class="number">0</span>; <span class="keyword">while</span> (num > <span class="number">0</span>) {</span><br><span class="line"> sum += num % <span class="number">2</span>;</span><br><span class="line"> num = num / <span class="number">2</span>;</span><br><span class="line"> } <span class="keyword">return</span> sum;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="29-去除字符串中的重复字符,对于出现超过2次(包含2次)的字符,只保留第一个。"><a href="#29-去除字符串中的重复字符,对于出现超过2次(包含2次)的字符,只保留第一个。" class="headerlink" title="29 去除字符串中的重复字符,对于出现超过2次(包含2次)的字符,只保留第一个。"></a>29 去除字符串中的重复字符,对于出现超过2次(包含2次)的字符,只保留第一个。</h2><p>例:输入abcbdde,输出abcde。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> str string字符串 </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> string字符串</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">removeDuplicatedChars</span> <span class="params">(String str)</span> </span>{</span><br><span class="line"> <span class="comment">// write code here</span></span><br><span class="line"> <span class="keyword">boolean</span>[] isExistChar = <span class="keyword">new</span> <span class="keyword">boolean</span>[<span class="number">26</span>];</span><br><span class="line"> <span class="keyword">boolean</span>[] isExistNum = <span class="keyword">new</span> <span class="keyword">boolean</span>[<span class="number">10</span>];</span><br><span class="line"> <span class="keyword">char</span>[] chars = str.toCharArray();</span><br><span class="line"> StringBuilder sb = <span class="keyword">new</span> StringBuilder();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">char</span> c : chars) {</span><br><span class="line"> <span class="comment">//是字母</span></span><br><span class="line"> <span class="keyword">if</span>(c >= <span class="string">'a'</span> && c <= <span class="string">'z'</span>){</span><br><span class="line"> <span class="keyword">if</span>(!isExistChar[c - <span class="string">'a'</span>]){</span><br><span class="line"> sb.append(c);</span><br><span class="line"> isExistChar[c - <span class="string">'a'</span>] = <span class="keyword">true</span>;</span><br><span class="line"> } </span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//是数字</span></span><br><span class="line"> <span class="keyword">if</span>(c >= <span class="string">'0'</span> && c <= <span class="string">'9'</span>){</span><br><span class="line"> <span class="keyword">if</span>(!isExistNum[c - <span class="string">'0'</span>]){</span><br><span class="line"> sb.append(c);</span><br><span class="line"> isExistNum[c - <span class="string">'0'</span>] = <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> sb.toString();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>30 给定一个整型数组,移除数组的某个元素使其剩下的元素乘积最大,如果数组出现相同的元素 ,请输出第一次出现的元素</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> String[] nums = scanner.next().split(<span class="string">","</span>);</span><br><span class="line"> <span class="keyword">int</span> n = nums.length;</span><br><span class="line"> <span class="comment">// dp[i] -> 除 nums[i] 以外的其余所有元素的乘积</span></span><br><span class="line"> BigDecimal[] dp = <span class="keyword">new</span> BigDecimal[n];</span><br><span class="line"> dp[<span class="number">0</span>] = BigDecimal.valueOf(<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i < n; i++) {</span><br><span class="line"> dp[i] = dp[i - <span class="number">1</span>].multiply(BigDecimal.valueOf(Integer.parseInt(nums[i - <span class="number">1</span>])));</span><br><span class="line"> }</span><br><span class="line"> BigDecimal temp = BigDecimal.valueOf(<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = n - <span class="number">1</span>; i >= <span class="number">0</span>; i--) {</span><br><span class="line"> dp[i] = dp[i].multiply(temp);</span><br><span class="line"> temp = temp.multiply(BigDecimal.valueOf(Integer.parseInt(nums[i])));</span><br><span class="line"> }</span><br><span class="line"> BigDecimal max = dp[<span class="number">0</span>];</span><br><span class="line"> <span class="keyword">int</span> idx = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i < n; i++) {</span><br><span class="line"> <span class="keyword">if</span> (dp[i].compareTo(max) > <span class="number">0</span>) {</span><br><span class="line"> max = dp[i];</span><br><span class="line"> idx = i;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(idx);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="31-给定一个整型正方形矩阵-Matrix,请把该矩阵调整成顺时针旋转90度的样子。"><a href="#31-给定一个整型正方形矩阵-Matrix,请把该矩阵调整成顺时针旋转90度的样子。" class="headerlink" title="31 给定一个整型正方形矩阵 Matrix,请把该矩阵调整成顺时针旋转90度的样子。"></a>31 给定一个整型正方形矩阵 Matrix,请把该矩阵调整成顺时针旋转90度的样子。</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span>[][] rotationMatrix(<span class="keyword">int</span>[][] matrix) {</span><br><span class="line"> <span class="keyword">if</span> (matrix != <span class="keyword">null</span> && matrix.length > <span class="number">0</span> && matrix.length == matrix[<span class="number">0</span>].length) {</span><br><span class="line"> <span class="keyword">int</span>[][] result = <span class="keyword">new</span> <span class="keyword">int</span>[matrix.length][matrix.length];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < matrix.length; i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < matrix[i].length; j++) {</span><br><span class="line"> result[i][j] = matrix[matrix.length - j - <span class="number">1</span>][i];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="32-在字符串中找到第一个不重复的字符。"><a href="#32-在字符串中找到第一个不重复的字符。" class="headerlink" title="32 在字符串中找到第一个不重复的字符。"></a>32 在字符串中找到第一个不重复的字符。</h2><p>例:对于字符串“hellohehe”,第一个不重复的字符是“o”。如果每个字符都有重复,则抛出运行时异常。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">char</span> <span class="title">findFirstNonRepeatChar</span> <span class="params">(String str)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span>[] map = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">128</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">char</span> c: str.toCharArray()) {</span><br><span class="line"> map[c]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">char</span> c: str.toCharArray()) {</span><br><span class="line"> <span class="keyword">if</span> (map[c] == <span class="number">1</span>) {</span><br><span class="line"> <span class="keyword">return</span> c;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"没有只有一个的字符"</span>);</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><h2 id="33-假设有N个用户,其中有些人是朋友,有些则不是。A和B是朋友,B和C是朋友,这样ABC就是一个朋友圈,请计算给定的朋友关系的朋友圈数。"><a href="#33-假设有N个用户,其中有些人是朋友,有些则不是。A和B是朋友,B和C是朋友,这样ABC就是一个朋友圈,请计算给定的朋友关系的朋友圈数。" class="headerlink" title="33.假设有N个用户,其中有些人是朋友,有些则不是。A和B是朋友,B和C是朋友,这样ABC就是一个朋友圈,请计算给定的朋友关系的朋友圈数。"></a>33.假设有N个用户,其中有些人是朋友,有些则不是。A和B是朋友,B和C是朋友,这样ABC就是一个朋友圈,请计算给定的朋友关系的朋友圈数。</h2><p>给定一个 N * N 的矩阵 M,表示用户之间的朋友关系。如果M[i][j] = 1,表示已知第 i 个和 j 个人互为朋友关系,否则为不知道。你必须输出所有用户中的已知的朋友圈总数。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.HashSet;</span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">int</span> n = scanner.nextInt();</span><br><span class="line"> String[][] relations = <span class="keyword">new</span> String[n][n];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> relations[i] = scanner.next().split(<span class="string">","</span>);</span><br><span class="line"> }</span><br><span class="line"> scanner.close();</span><br><span class="line"> System.out.println(friendCircle(relations));</span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">friendCircle</span><span class="params">(String[][] relations)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = relations.length;</span><br><span class="line"> <span class="comment">// graph[i] -> 用户 i 的所有朋友</span></span><br><span class="line"> Set[] graph = <span class="keyword">new</span> HashSet[n];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> graph[i] = <span class="keyword">new</span> HashSet();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < n; j++) {</span><br><span class="line"> <span class="keyword">int</span> relation = Integer.parseInt(relations[i][j]);</span><br><span class="line"> <span class="keyword">if</span> (relation == <span class="number">1</span>) {</span><br><span class="line"> graph[i].add(j);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">boolean</span>[] visited = <span class="keyword">new</span> <span class="keyword">boolean</span>[n];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> <span class="keyword">if</span> (!visited[i]) {</span><br><span class="line"> ans++;</span><br><span class="line"> dfs(graph, i, visited);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">dfs</span><span class="params">(Set[] graph, <span class="keyword">int</span> cur, <span class="keyword">boolean</span>[] visited)</span> </span>{</span><br><span class="line"> visited[cur] = <span class="keyword">true</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> next : graph[cur]) {</span><br><span class="line"> <span class="keyword">if</span> (!visited[next]) {</span><br><span class="line"> dfs(graph, next, visited);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="34-假设有个文件,文件的每一行是书信息数据,分4个部分用逗号(-)进行分割-格式如下"><a href="#34-假设有个文件,文件的每一行是书信息数据,分4个部分用逗号(-)进行分割-格式如下" class="headerlink" title="34 假设有个文件,文件的每一行是书信息数据,分4个部分用逗号(,)进行分割,格式如下"></a>34 假设有个文件,文件的每一行是书信息数据,分4个部分用逗号(,)进行分割,格式如下</h2><p>id,category,words,updatetime</p><p>id 表示书id,long类型,id不重复;</p><p>category 表示书的分类,int类型,请注意全部数据的分类只有几个</p><p>words 表示书的字数,int类型</p><p>updatetime 表示书的更新时间 ,格式为2020-02-01 23:00:00 </p><p>请编写程序对文件数据进行排序后输出id,排序优先级为: <strong>category>updatetime > words > id , 增序排序</strong></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">int</span> n = scanner.nextInt();</span><br><span class="line"> Book[] books = <span class="keyword">new</span> Book[n];</span><br><span class="line"> SimpleDateFormat dateFormat = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> String[] row = scanner.next().split(<span class="string">","</span>);</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> String updateTime = row[<span class="number">3</span>] + <span class="string">" "</span> + scanner.next();</span><br><span class="line"> Book book =</span><br><span class="line"> <span class="keyword">new</span> Book(</span><br><span class="line"> Long.parseLong(row[<span class="number">0</span>]),</span><br><span class="line"> Integer.parseInt(row[<span class="number">1</span>]),</span><br><span class="line"> Integer.parseInt(row[<span class="number">2</span>]),</span><br><span class="line"> dateFormat.parse(updateTime));</span><br><span class="line"> books[i] = book;</span><br><span class="line"> } <span class="keyword">catch</span> (ParseException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> scanner.close();</span><br><span class="line"> <span class="comment">// 升序排列。字段优先级:category > updateTime > words > id</span></span><br><span class="line"> Arrays.sort(books, (book1, book2) -> {</span><br><span class="line"> <span class="keyword">if</span> (book1.getCategory() != book2.getCategory()) {</span><br><span class="line"> <span class="keyword">return</span> book1.getCategory() - book2.getCategory();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">long</span> update1 = book1.getUpdateTime().getTime(), update2 = book2.getUpdateTime().getTime();</span><br><span class="line"> <span class="keyword">if</span> (update1 != update2) {</span><br><span class="line"> <span class="keyword">return</span> (<span class="keyword">int</span>) (update1 - update2);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (book1.getWords() != book2.getWords()) {</span><br><span class="line"> <span class="keyword">return</span> book1.getWords() - book2.getWords();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> (<span class="keyword">int</span>) (book1.getId() - book2.getId());</span><br><span class="line"> });</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> System.out.println(books[i].getId());</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">long</span> id;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> category;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> words;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> Date updateTime;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Book</span><span class="params">(<span class="keyword">long</span> id, <span class="keyword">int</span> category, <span class="keyword">int</span> words, Date updateTime)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> <span class="keyword">this</span>.category = category;</span><br><span class="line"> <span class="keyword">this</span>.words = words;</span><br><span class="line"> <span class="keyword">this</span>.updateTime = updateTime;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">long</span> <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getCategory</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.category;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getWords</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.words;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Date <span class="title">getUpdateTime</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.updateTime;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="35请使用堆栈这一个数据结构实现简单FIFO(先入先出)队列,队列要实现两个方法:-push、pop。"><a href="#35请使用堆栈这一个数据结构实现简单FIFO(先入先出)队列,队列要实现两个方法:-push、pop。" class="headerlink" title="35请使用堆栈这一个数据结构实现简单FIFO(先入先出)队列,队列要实现两个方法: push、pop。"></a>35请使用堆栈这<strong>一个</strong>数据结构实现简单FIFO(先入先出)队列,队列要实现两个方法: push、pop。</h2><p>为自动测试方便,使用每行输入模拟操作:</p><p>1) push 1 表明向队列里面新增一个元素 1 , push 和元素之间用空格表示;</p><p>2) pop 表明输出当前队列里面的第一个元素,如果当前队列为空请输出null</p><p>请将每个输出以英文逗号拼接到一个字符串中。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">MyQueue</span> </span>{</span><br><span class="line"> <span class="keyword">int</span>[] queue;</span><br><span class="line"> <span class="keyword">int</span> lo, hi, size, capacity;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">MyQueue</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.lo = <span class="keyword">this</span>.hi = <span class="keyword">this</span>.size = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">this</span>.capacity = n;</span><br><span class="line"> <span class="keyword">this</span>.queue = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">MyQueue</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>(<span class="number">10</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">push</span><span class="params">(<span class="keyword">int</span> val)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (hi == capacity) {</span><br><span class="line"> <span class="keyword">if</span> (lo > <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">int</span> idx = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = lo; i < hi; i++) {</span><br><span class="line"> queue[idx++] = queue[i];</span><br><span class="line"> }</span><br><span class="line"> lo = <span class="number">0</span>;</span><br><span class="line"> hi = idx;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//扩容</span></span><br><span class="line"> <span class="keyword">int</span>[] newQueue = <span class="keyword">new</span> <span class="keyword">int</span>[capacity * <span class="number">2</span>];</span><br><span class="line"> System.arraycopy(queue, <span class="number">0</span>, newQueue, <span class="number">0</span>, capacity);</span><br><span class="line"> <span class="keyword">this</span>.queue = newQueue;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">this</span>.queue[hi++] = val;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">pop</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (lo == hi) <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.queue[lo++];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="comment">// int[] nums = Arrays.stream().mapToInt(Integer::parseInt).toArray();</span></span><br><span class="line"> String[] ss = scanner.nextLine().split(<span class="string">","</span>);</span><br><span class="line"> YuewenJavaTest.MyQueue myQueue = <span class="keyword">new</span> YuewenJavaTest.MyQueue();</span><br><span class="line"> <span class="keyword">for</span> (String s: ss) {</span><br><span class="line"> <span class="keyword">if</span> (s.startsWith(<span class="string">"push"</span>)) {</span><br><span class="line"> myQueue.push(Integer.parseInt(s.split(<span class="string">" "</span>)[<span class="number">1</span>]));</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> System.out.println(myQueue.pop());</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> scanner.close();</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="36-在和外部公司联调HTTP接口时,对方要求调用的接口需要计算token,给到的规则如下:"><a href="#36-在和外部公司联调HTTP接口时,对方要求调用的接口需要计算token,给到的规则如下:" class="headerlink" title="36 在和外部公司联调HTTP接口时,对方要求调用的接口需要计算token,给到的规则如下:"></a>36 在和外部公司联调HTTP接口时,对方要求调用的接口需要计算token,给到的规则如下:</h2><p>1) 所有的参数值必须经过urlencode,编码为utf-8;</p><p>2) 对编码后数据按照key值进行字典升序排序;</p><p>3)将所有的参数按照排序的顺序拼接成字符串 ,格式如下: k1=v1&k2=v2&k3=v3;</p><p>\4) 将第三步的算出的值计算md5值,md5加密的值小写</p><p>请你编写一段方法计算token值</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">getToken</span><span class="params">(Map<String, String> params)</span> </span>{</span><br><span class="line"> List<String> ss = params.keySet().stream().sorted().map(</span><br><span class="line"> k -> {</span><br><span class="line"> String v = params.get(k);</span><br><span class="line"> String kv = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> kv = k + <span class="string">"="</span> + URLEncoder.encode(v, <span class="string">"utf-8"</span>);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> kv;</span><br><span class="line"> }</span><br><span class="line"> ).collect(Collectors.toList());</span><br><span class="line"> String token = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> token = <span class="keyword">new</span> String(MessageDigest.getInstance(<span class="string">"md5"</span>).digest(String.join(<span class="string">"&"</span>, ss).getBytes()));</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> token;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><h1 id="阅文笔试复习"><a href="#阅文笔试复习" class="headerlink" title="阅文笔试复习"></a>阅文笔试复习</h1><h2 id="1-详细描述ThreadPoolExecutor的各个参数的含义,介绍一个任务提交到线程池后的执行流程"><a href="#1-详细描述ThreadPoolExecutor的各个参数的含义,介绍一个任务提交到线程池后的执行流程" class="headerlink" title="1.详细描述ThreadPoolExecutor的各个参数的含义,介绍一个任务提交到线程池后的执行流程"></a>1.详细描述ThreadPoolExecutor的各个参数的含义,介绍一个任务提交到线程池后的执行流程</h2><ul>
<li><p>corePoolSize : 线程池的核心大小,也可以理解为最小的线程池大小。</p>
</li>
<li><p>maximinPoolSize : 最大线程池大小</p>
</li>
<li><p>keepAliveTime:空余线程存活时间,指的是超过corePoolSize的空余线程达到多长时间才进行销毁。</p>
</li>
<li><p>util:销毁时间单位</p>
</li>
<li><p>WorkQueue:存储等待执行线程的工作队列。</p>
</li>
<li><p>threadFactory:创建线程的工厂,一般用默认即可。</p>
</li>
<li><p>handle:拒绝策略,当工厂队列,线程池全满时如何拒绝新任务,默认抛出异常</p></summary>
<category term="笔试" scheme="http://example.com/tags/%E7%AC%94%E8%AF%95/"/>
</entry>
<entry>
<title>Git-Git命令</title>
<link href="http://example.com/2022/03/26/git/"/>
<id>http://example.com/2022/03/26/git/</id>
<published>2022-03-26T03:56:56.000Z</published>
<updated>2023-09-26T13:09:42.669Z</updated>
<content type="html"><![CDATA[<h1 id="Git-RESTful-API-命令行"><a href="#Git-RESTful-API-命令行" class="headerlink" title="Git/RESTful API/命令行"></a>Git/RESTful API/命令行</h1><ul><li><a href="#git">Git</a><ul><li><a href="#git-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4">Git 常用命令</a></li><li><a href="#git-%E6%A0%87%E7%AD%BE%E7%AE%A1%E7%90%86">Git 标签管理</a></li><li><a href="#git-%E6%92%A4%E9%94%80%E4%B8%8E%E5%9B%9E%E6%BB%9A">Git 撤销与回滚</a></li><li><a href="#git-%E5%88%86%E6%94%AF%E7%AE%A1%E7%90%86">Git 分支管理</a></li></ul></li><li><a href="#restful-api">RESTful API</a></li><li><a href="#linux-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4">Linux 常用命令</a></li><li><a href="#%E5%8F%82%E8%80%83">参考</a></li></ul><hr><h2 id="Git"><a href="#Git" class="headerlink" title="Git"></a>Git</h2><h4 id="Git-常用命令"><a href="#Git-常用命令" class="headerlink" title="Git 常用命令"></a>Git 常用命令</h4><ul><li><code>git init</code></li><li><code>git clone</code></li><li><code>git remote add origin ***.git</code>、</li></ul><span id="more"></span><ul><li><code>git push -u origin master</code></li><li>推送到远程仓库的dev分支:<code>git push origin dev</code></li><li><code>git log</code></li><li><code>git log --graph --pretty=oneline --abbrev-commit</code></li><li><code>git status</code></li><li><code>git diff</code></li><li><code>git add *</code></li><li><code>git commit -m "message"</code></li><li>commit之后又改了一个小bug,但是又不想增加一个commit,可以用:<code>git commit --amend --no-edit</code>,直接将改动添加到上一次的commit中</li><li><code>git push</code></li><li><code>git pull</code></li><li><code>touch .gitignore</code></li></ul><h4 id="Git-标签管理"><a href="#Git-标签管理" class="headerlink" title="Git 标签管理"></a>Git 标签管理</h4><ul><li>首先切换到需要打标签的分支上,然后使用<code>git tag v1.0</code>就可以在当前commit打上v1.0的标签</li><li><code>git tag v1.0 commitID</code> 对特定commit打标签</li><li>打标签时加上message:<code>git tag -a <tagname> -m "message"</code></li><li><code>git tag</code> 查看所有标签</li><li><code>git show [tagname]</code> 查看标签详细信息</li><li><code>git push origin <tagname></code>可以推送一个本地标签到远程仓库</li><li><code>git push origin --tags</code>可以推送全部未推送过的本地标签</li><li><code>git tag -d <tagname></code>可以删除一个本地标签</li><li><code>git push origin :refs/tags/<tagname></code>可以删除一个远程标签(先从本地删除)</li></ul><h4 id="Git-撤销与回滚"><a href="#Git-撤销与回滚" class="headerlink" title="Git 撤销与回滚"></a>Git 撤销与回滚</h4><ul><li><strong>暂存区</strong>:<code>git add</code>之后commit之前存在的区域;<strong>工作区</strong>:<code>git commit</code>之后存在的区域;<strong>远程仓库</strong>:<code>git push</code>之后;</li><li>作了修改,但还没<code>git add</code>,撤销到上一次提交:<code>git checkout -f -- filename</code>;<code>git checkout -f -- .</code></li><li>作了修改,并且已经<code>git add</code>,但还没<code>git commit</code>:<ul><li>先将暂存区的修改撤销:<code>git reset HEAD filename</code>/<code>git reset HEAD</code>;此时修改只存在于工作区,变为了 “unstaged changes”;</li><li>再利用上面的checkout命令从工作区撤销修改</li></ul></li><li><code>git add</code>之后,作了修改,想丢弃这次修改:<code>git checkout -f --filename</code>会回到最近一次<code>git add</code></li><li>作了修改,并且已经<code>git commit</code>了,想撤销这次的修改:<ul><li><code>git revert commitID</code>. 其实,<code>git revert</code>可以用来撤销任意一次的修改,不一定要是最近一次</li><li><code>git reset --hard commitID</code>/<code>git reset --hard HEAD^</code>(HEAD表示当前版本,几个^表示倒数第几个版本,倒数第100个版本可以用HEAD~100);参数<code>--hard</code>:强制将暂存区和工作区都同步到指定的版本</li><li><code>git reset</code>和<code>git revert</code>的区别是:reset是用来回滚的,将HEAD的指针指向了想要回滚的版本,作为最新的版本,而后面的版本也都没有了;而revert只是用来撤销某一次更改,对之后的更改并没有影响</li><li>然后再用<code>git push -f</code>提交到远程仓库</li></ul></li></ul><h4 id="Git-分支管理"><a href="#Git-分支管理" class="headerlink" title="Git 分支管理"></a>Git 分支管理</h4><ul><li>创建分支: <code>git branch test</code></li><li>切换分支: <code>git checkout test</code></li><li>创建并切换分支:<code>git checkout -b test</code></li><li>将test分支的更改合并到master分支:先在test分支上commit、push,再:<code>git checkout master</code>; <code>git merge test</code></li><li>如果合并时产生冲突:先手动解决冲突,再合并</li><li>删除分支:<code>git branch -d test</code></li><li><code>git stash</code><ul><li>如果当前分支还有任务没有做完,也不想提交,但此时需要切换或者创建其它分支,就可以使用stash将当前分支的所有修改(包括暂存区)先储藏起来;然后就可以切换到其它分支</li><li>在其它分支工作完成之后,首先切换回原来的分支,然后使用<code>git stash list</code>命令查看</li><li>可以使用<code>git stash apply <stash number></code>恢复之前储藏的工作现场,再使用<code>git stash drop <stash number></code>删除掉储藏的内容</li><li>也可以直接用<code>git stash pop</code>恢复并删除内容</li></ul></li><li>如果在其它分支上做了一个修改(比如修复了一个bug,这次修改有一个commitID),想要将这次修改应用到当前分支上,可以使用:<code>git cherry-pick commitID</code>,可以复制一个特定的提交到当前分支</li></ul><h2 id="RESTful-API"><a href="#RESTful-API" class="headerlink" title="RESTful API"></a>RESTful API</h2><p>REST指Representational State Transfer,可以翻译为“表现层状态转化”</p><h4 id="主要思想"><a href="#主要思想" class="headerlink" title="主要思想"></a>主要思想</h4><ul><li>对网络上的所有资源,都有一个<strong>统一资源标识符</strong> URI(Uniform Resource Identifier);</li><li>这些资源可以有多种表现形式,即REST中的“表现层”Representation,比如,文本可以用txt格式表现,也可以用HTML格式、XML格式、JSON格式表现。URI只代表资源的实体,不代表它的形式;</li><li>“无状态(Stateless)”思想:服务端不应该保存客户端状态,只需要处理当前的请求,不需了解请求的历史,客户端每一次请求中包含处理该请求所需的一切信息;</li><li>客户端使用HTTP协议中的 GET/POST/PUT/DELETE 方法对服务器的资源进行操作,即REST中的”状态转化“</li></ul><h4 id="设计原则"><a href="#设计原则" class="headerlink" title="设计原则"></a>设计原则</h4><ul><li>URL设计<ul><li>最好只使用名词,而使用 GET/POST/PUT/DELETE 方法的不同表示不同的操作;比如使用<code>POST /user</code>代替<code>/user/create</code></li><li>GET:获取资源;POST:新建/更新资源;PUT:更新资源;DELETE:删除资源;</li><li>对于只支持GET/POST的客户端,使用<code>X-HTTP-Method-Override</code>属性,覆盖POST方法;</li><li>避免多级URL,比如使用<code>GET /authors/12?categories=2</code>代替<code>GET /authors/12/categories/2</code>;</li><li>避免在URI中带上版本号。不同的版本,可以理解成同一种资源的不同表现形式,所以应该采用同一个URI,版本号可以在HTTP请求头信息的Accept字段中进行区分</li></ul></li><li>状态码:服务器应该返回尽可能精确的状态码,客户端只需查看状态码,就可以判断出发生了什么情况。见计算机网络部分 – <a href="Computer%20Network.md#HTTP%E8%AF%B7%E6%B1%82%E6%9C%89%E5%93%AA%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%8A%B6%E6%80%81%E7%A0%81">HTTP请求有哪些常见状态码?</a></li><li>服务器回应:在响应中放上其它API的链接,方便用户寻找</li></ul><h2 id="Linux-常用命令"><a href="#Linux-常用命令" class="headerlink" title="Linux 常用命令"></a>Linux 常用命令</h2><h3 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h3><ul><li><a href="https://learngitbranching.js.org/?demo=&locale=zh_CN">Learn Git Branching - 可视化的学习 Git 操作</a></li><li><a href="https://www.liaoxuefeng.com/wiki/896043488029600">Git教程 - 廖雪峰的官方网站</a></li><li><a href="http://www.ruanyifeng.com/blog/2018/10/restful-api-best-practices.html">RESTful API 最佳实践 - 阮一峰的网络日志</a></li><li><a href="https://github.com/jlevy/the-art-of-command-line/blob/master/README-zh.md">GitHub - jlevy/the-art-of-command-line: Master the command line, in one page</a></li></ul>]]></content>
<summary type="html"><h1 id="Git-RESTful-API-命令行"><a href="#Git-RESTful-API-命令行" class="headerlink" title="Git/RESTful API/命令行"></a>Git/RESTful API/命令行</h1><ul>
<li><a href="#git">Git</a><ul>
<li><a href="#git-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4">Git 常用命令</a></li>
<li><a href="#git-%E6%A0%87%E7%AD%BE%E7%AE%A1%E7%90%86">Git 标签管理</a></li>
<li><a href="#git-%E6%92%A4%E9%94%80%E4%B8%8E%E5%9B%9E%E6%BB%9A">Git 撤销与回滚</a></li>
<li><a href="#git-%E5%88%86%E6%94%AF%E7%AE%A1%E7%90%86">Git 分支管理</a></li>
</ul>
</li>
<li><a href="#restful-api">RESTful API</a></li>
<li><a href="#linux-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4">Linux 常用命令</a></li>
<li><a href="#%E5%8F%82%E8%80%83">参考</a></li>
</ul>
<hr>
<h2 id="Git"><a href="#Git" class="headerlink" title="Git"></a>Git</h2><h4 id="Git-常用命令"><a href="#Git-常用命令" class="headerlink" title="Git 常用命令"></a>Git 常用命令</h4><ul>
<li><code>git init</code></li>
<li><code>git clone</code></li>
<li><code>git remote add origin ***.git</code>、</li>
</ul></summary>
</entry>
<entry>
<title>数据库-事务&隔离级别</title>
<link href="http://example.com/2022/03/25/%E6%95%B0%E6%8D%AE%E5%BA%93/"/>
<id>http://example.com/2022/03/25/%E6%95%B0%E6%8D%AE%E5%BA%93/</id>
<published>2022-03-25T07:43:04.704Z</published>
<updated>2023-09-26T13:08:02.388Z</updated>
<content type="html"><![CDATA[<h1 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h1><h2 id="事务的ACID特性"><a href="#事务的ACID特性" class="headerlink" title="事务的ACID特性"></a>事务的ACID特性</h2><ul><li><p>原子性 逻辑上是不可分割的操作单元,事务的所有操作要么全部提交成功,要么全部失败回滚(用回滚日志实现,反向执行日志中的操作);</p></li><li><p>一致性 事务的执行必须使数据库保持一致性状态,在一致性状态下,所有 </p></li><li><p>隔离性 一个事物所做的修改在最终提交以前,对其他事务是不可见的。</p></li><li><p>持久性 一旦事物提交成功,对数据的修改是永久性的</p><span id="more"></span></li></ul><h2 id="会出现哪些并发一致性问题?"><a href="#会出现哪些并发一致性问题?" class="headerlink" title="会出现哪些并发一致性问题?"></a>会出现哪些并发一致性问题?</h2><ul><li>丢失修改: 一个事务对数据进行了修改,在事务提交之前,另一个事务对同一个数据进行了修改,覆盖了之前的修改。</li><li>脏读: 一个事务读取了被另外一个事务修改,但未提交(进行了回滚)的数据,造成两个事物得到的数据不一致;</li><li>不可重复读 :在同一个事物中,某查询操作在一个时间读取某一行数据和之后一个时间读取该行数据,发现数据已经发生修改(针对update操作)</li><li>幻读 :当同一查询多次执行时,由于其它事物在这个数据范围内执行了插入操作,会导致每次返回不同的结果集(和不可重复读的区别:针对的是一个数据整体/范围;并针对insert)</li></ul><h2 id="数据库的四种隔离级别"><a href="#数据库的四种隔离级别" class="headerlink" title="数据库的四种隔离级别"></a>数据库的四种隔离级别</h2><ul><li>未提交读 在一个事物提交之前,它的执行结果对其他事物也是可见的。会导致脏读,不可重复读,幻读</li><li>提交读 一个事物只能空间已经提交的事务所作的改变。可避免脏读问题。</li><li>可重复读 可以确保同一个事务在多次读取同样的数据时得到相同的结果。可避免不可重复读</li><li>可串行化 强制事务所执行,使之不可能相互冲突,从而解决幻读问题。可能导致大量的超时现象和锁竞争,实际很少使用</li></ul>]]></content>
<summary type="html"><h1 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h1><h2 id="事务的ACID特性"><a href="#事务的ACID特性" class="headerlink" title="事务的ACID特性"></a>事务的ACID特性</h2><ul>
<li><p>原子性 逻辑上是不可分割的操作单元,事务的所有操作要么全部提交成功,要么全部失败回滚(用回滚日志实现,反向执行日志中的操作);</p>
</li>
<li><p>一致性 事务的执行必须使数据库保持一致性状态,在一致性状态下,所有 </p>
</li>
<li><p>隔离性 一个事物所做的修改在最终提交以前,对其他事务是不可见的。</p>
</li>
<li><p>持久性 一旦事物提交成功,对数据的修改是永久性的</p></summary>
<category term="数据库" scheme="http://example.com/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/"/>
</entry>
<entry>
<title>双非本的秋招总结</title>
<link href="http://example.com/2022/03/23/%E7%A7%8B%E5%A4%A9%E9%9D%A2%E8%AF%95%E7%9A%84%E5%87%84%E6%83%A8%E7%BB%8F%E5%8E%86/"/>
<id>http://example.com/2022/03/23/%E7%A7%8B%E5%A4%A9%E9%9D%A2%E8%AF%95%E7%9A%84%E5%87%84%E6%83%A8%E7%BB%8F%E5%8E%86/</id>
<published>2022-03-23T08:33:56.000Z</published>
<updated>2023-09-26T13:14:57.115Z</updated>
<content type="html"><![CDATA[<p>秋天面试的凄惨经历</p><h1 id="双非本的秋招总结"><a href="#双非本的秋招总结" class="headerlink" title="双非本的秋招总结"></a><strong>双非本的秋招总结</strong></h1><p>学历 双非本软件工程专业</p><p>简历挂的 数不胜数</p><p>一面挂的 泛微 青颖飞帆 汇川技术 哈啰实习</p><p>意向的 比亚迪 兑吧实习 北京华宇</p><span id="more"></span><p>从九月初开始投的秋招</p><p>但是雷声大雨点小 自己刚开始并没有收到笔试面试信息 后面有点慌了</p><p>有哈啰的群友说自己组里还差一个实习生 但不保证转正机会 想着在学校也没啥面试 就让那位朋友给我内推了一下 投完之后两天之内没反应 以为g了 朋友也以为g了,结果在几天后我在上课的时候 收到了哈啰hr的电话 约在了9月15号面试 </p><p>哈啰实习面试: </p><p>面试官大部分都在问实习期间做的东西 以及给我加需求 让我排查意外发生的原因是什么 这种我自然答得很一般 后面问了几个常规的八股 问了一下快速排序的实现,最近看了什么书籍 职业规划等等</p><p>详细面经地址<a href="https://blog.huttop.top/2022/09/18/%E5%93%88%E5%95%B0%E5%87%BA%E8%A1%8C/">https://blog.huttop.top/2022/09/18/%E5%93%88%E5%95%B0%E5%87%BA%E8%A1%8C/</a></p><p>后面和朋友沟通才发现面试我的是他们大部门的后端leader 后面和朋友沟通感觉希望不大</p><p>15号的当晚也做了奇安信的笔试</p><p>到了下一周 同花顺 趋势科技 小米 六方云 恒生 深信服 华宇,青颖飞帆接踵笔试而来</p><p>恒生让转岗技术支持 直接就拒了</p><p>我也收获了秋招的第一次正式面试</p><p>泛微Java一面</p><p>一个面试官 四个候选人 轮流提问</p><p>印象很深刻 有一个是我隔壁班一个学前端的同学 不过我两互不认识</p><p>一个西南大学本科东南大学机械本硕 转行C++ </p><p>一个东北大学计算机的C++的同学 </p><p>当时感觉咋学历都这么猛 这家工资也开的不高啊 后面基本上每个人4分钟就结束了 第二天晚上的时候查询状态就是不通过了</p><p>面试官后面查了一下是这家公司的副总经理 总之挂的很莫名其妙 问了几个生活问题就把你挂掉 感觉很难受</p><p>后面前实习的同事到了一家新公司实习 问我要不要过去 我觉得过去再刷一段实习也行</p><p>这家流程挺快的 从给简历到我同事到OC就三天不到的时间就有了</p><p>兑吧实习的面试</p><p> 一面 9.22号下午</p><p>是我前同事的组长 高强度JVM JUC MYSQL 八股文拷打 因为事先有准备 答得还可以</p><p>那天湖南降温 自己发烧了 还是我做深信服笔试的时候自己很不舒服 一直不能集中注意力 脑袋昏昏沉沉的 草草交卷。然后我拿体温计一量 38.5度 把我吓一跳 因为那时候是晚上了 出去去医院的话起码得半夜了 不想折腾 也约了第二天早上兑吧二面 然后自己拿两块冷毛巾给自己额头和身体降温</p><p>这里感谢我的室友 一直劝说我去医院 不过我自己态度坚决 后面也降到了38度以下 他们也就作罢劝说 非常感谢他们 我在寝室面试他们也给了我很多的便利。</p><p>幸好睡觉前降到了38度以下 早上起来量了一下37.5 自己精神状态也好很多了 后面就进行 兑吧二面 9.23 上午</p><p> 二面是我前同事的主管 也是我现在的主管 问了一点项目的问题 后面就是问职业规划 这种主管都喜欢问职业规划吗 然后问我对他们业务有没有了解 然后给我讲了挺久的业务 面完不久hr就来约hr面 越在了下午 因为那天是周五 想这周吧流程都走完</p><p>hr面 9.23 下午</p><p> 问了挺多常规的问题 后面面完不久 也是直接就OC了 说给我考虑到星期一的时间 因为哈啰那时候我还不知道挂没挂 后面在钉钉问了一下当时的面试官 得知没有通过 并且前同事说那里挺不错的加之自己对主管感觉还挺好的 然后就约在了国庆后入职</p><p>这周不是做了挺多的笔试吗 除了华宇后面,青颖飞帆有后续 其他的应该是都挂了</p><p>印象深刻的是小米的笔试 两道编程题都很简单 都ac了 不过Java写C++的选择题 C++写Java的 后面也是发了感谢信</p><p>在华宇笔试的时候收到了比亚迪的电话二面</p><p>比亚迪二面</p><p>那边是嵌入式方向的 刚开始就问我学了什么课程</p><p>问我学过数电模电没 我就意识到对面应该是嵌入式方面的了</p><p>后面就简单聊了一下 看的懂信号板子和电路板子吗 我说我学过 但是现在可能有点不太记得了</p><p>讲讲你最熟悉的排序方法 我当时讲了个快速排序 然后面试官问我就几个for循环</p><p>给我整懵了 我说快速排序没有for循环 后面他哦了一下 问我冒泡排序的原理 然后问我几个for循环 我说两个 我当时没绷住 笑了一下</p><p>后面说面试通过了 反馈给人事 让我加让他微信 给我发一些他们那边需要学习的课程</p><p>我当时:大哥我好像没说我一定去吧?</p><p>然后九月的最后一周 青颖飞帆 汇川技术一面</p><p>28号青颖飞帆一面</p><p>感觉主要挂的原因是给了道手撕mid代码题 没写出来 二叉排序搜索树转换成双向有序列表</p><p>因为面试官是口述题目 加上说了一长段 不能用list 不能新建结点 不能用啥</p><p>然后自己陷入了思路死掉了 一直想怎么给他们转一下 后面发现其实不难 只需要建两个引用(我以为引用也算新建结点) 但是感觉很挫败 自己也刷了600道lc题了 结果第一家手撕代码没有出来 有点自闭</p><p>30 汇川技术一面 这家其实九月初来我们学校宣讲就投了 一直以为没了 结果九月底给我发了一个面试</p><p>当时是下午 上来我先自我介绍</p><p>就问我我家邵阳和岳阳挨得近吗 你学校是在长沙吗 长沙和岳阳挨得近吗 说了几分钟家常大概</p><p>然后就开始问项目 问了一点八股文吧</p><p>后面就问 你为什么会来投汇川技术这家公司 有了解吗 我说只知道好像以前是从华为出来的一群高管创业的 然后互联网不稳定啊 觉得贵公司很有前途之类的话</p><p>说我为什么选苏州这个地方 然后我就嘎吹了一下长三角 什么未来的经济发展中心啊</p><p>还有你觉得制造业和互联网哪个发展更好 我就说肯定是制造业 </p><p>后面说和同事对齐一下 再反馈面试结果给我 我当时想着这很华为 当时感觉聊的还好</p><p>应该有二面吗</p><p>结果晚上就收到了他的感谢信 g</p><p>然后就到了十月 国庆回来 华宇约了面试</p><p>12号华宇面试 下班时间在公司电话亭面的 面我的也不问八股 问我一些实操问题</p><p>怎么排查项目占有率很高 内存泄漏时什么导致的</p><p>问我github这么多star是哪里来的 是哪个项目</p><p>又聊了一下人生规划</p><p>反问</p><p>我当时应该问了很多个 因为我觉得很合得来 </p><p>印象最深刻的时候就是您认为工作和生活怎么平衡</p><p>说按他的经验来说 可能像我们这种刚出社会的应届生来说可能就是工作倾斜更多一点 后面年龄大了就偏向生活里 聊了挺多的生活态度</p><p>感觉他问我问题的时候和我问他问题的时间是55开</p><p>然后周五晚上 通过面试的邮件以及让我补充一些材料</p><p>20号华宇OC</p><p>当时我以为后面还有hr面 就找当时的hr问了一下进度 hr说她去反馈给同事 </p><p>然后下午突然就OC了 说是有谈薪 是定死的 应该不给A</p><p>后面和hr了解到 一面面试官是北京研发中心的大leader 感觉还是比较nice的</p><p>然后就福利什么的介绍了一下 我问三方什么签订 说是不急 整个10月份应该是不会进行的 </p>]]></content>
<summary type="html"><p>秋天面试的凄惨经历</p>
<h1 id="双非本的秋招总结"><a href="#双非本的秋招总结" class="headerlink" title="双非本的秋招总结"></a><strong>双非本的秋招总结</strong></h1><p>学历 双非本软件工程专业</p>
<p>简历挂的 数不胜数</p>
<p>一面挂的 泛微 青颖飞帆 汇川技术 哈啰实习</p>
<p>意向的 比亚迪 兑吧实习 北京华宇</p></summary>
<category term="面试" scheme="http://example.com/tags/%E9%9D%A2%E8%AF%95/"/>
</entry>
<entry>
<title>线程与进程的区别</title>
<link href="http://example.com/2022/03/22/%E7%BA%BF%E7%A8%8B%E4%B8%8E%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%8C%BA%E5%88%AB/"/>
<id>http://example.com/2022/03/22/%E7%BA%BF%E7%A8%8B%E4%B8%8E%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%8C%BA%E5%88%AB/</id>
<published>2022-03-22T07:59:35.167Z</published>
<updated>2023-09-26T13:08:23.107Z</updated>
<content type="html"><![CDATA[<h1 id="线程与进程的区别"><a href="#线程与进程的区别" class="headerlink" title="线程与进程的区别"></a>线程与进程的区别</h1><ul><li><p>进程是系统进行资源分配和调度的基本单位</p></li><li><p>线程是CPU调度和分派的基本单位</p></li><li><p>线程依赖于进程而存在,一个进程至少有一个线程</p><span id="more"></span></li><li><p>进程有自己的独立地址空间,线程共享所属进程的地址空间。</p></li><li><p>进程是拥有系统资源的一个独立单位,而线程自己基本上 不拥有系统资源。只拥有一点在运行中必不可少的资源(资源计数器,一组寄存器和栈。)和其他线程共享本线程的相关资源,如内存,I/O,cpu等。</p></li><li><p>线程之间的通信更方便,同一进程下的线程共享全局变量等数据,而进程之间的通信需要以进程间通信(IPC)的方式进行;</p></li><li><p>多线程程序只要有一个线程崩溃,整个程序就崩溃了,但多进程程序中一个进程崩溃并不会对其它进程造成影响,因为进程有自己的独立地址空间,因此多进程更加健壮</p></li></ul><h1 id="同一进程中的线程可以共享哪些数据?"><a href="#同一进程中的线程可以共享哪些数据?" class="headerlink" title="同一进程中的线程可以共享哪些数据?"></a>同一进程中的线程可以共享哪些数据?</h1><ul><li>进程代码段</li><li>进程的公有数据</li><li>进程打开的文件描述符</li><li>进程的当前目录</li><li>信号处理器/信号处理函数:对收到的信号的处理方式</li><li>进程ID和进程组ID</li></ul><h1 id="进程独占哪些资源"><a href="#进程独占哪些资源" class="headerlink" title="进程独占哪些资源"></a>进程独占哪些资源</h1><ul><li>线程ID</li><li>一组寄存器的值和</li><li>线程自身的栈(堆是共享的)</li><li>错误返回码:线程可能会产生不同的错误返回码,一个线程的错误返回码不应该被其它线程修改;</li><li>信号掩码/信号屏蔽字(Signal mask):表示是否屏蔽/阻塞相应的信号(SIGKILL,SIGSTOP除外)</li></ul><h1 id="管程"><a href="#管程" class="headerlink" title="管程"></a>管程</h1><p>管程将共享变量以及对这些共享变量的操作封装起来,形成一个具有一定接口的功能模块,这样只能通过管程提供的某个过程才能访问管程中的资源。进程只能互斥的使用管程,使用完之后必须释放管程并唤醒入口等待队列中的进程。</p><p>当一个进程试图进入 管程时,在入口等待队列等待。若P进程唤醒了Q进程,则Q进程先执行,P在<strong>紧急等待队列</strong>中等待。(<strong>HOARE管程</strong>)</p><p>wait操作:执行wait操作的进程进入条件变量链末尾,唤醒紧急等待队列以及入口队列中的进程,signal操作:唤醒条件变量链中的进程,自己进入紧急等待队列,若条件变量链为空,则继续执行。</p><p>MESA管程:将HOARE中的signal换成了notify(或者broadcast通知所有满足条件的),进行通知而不是立马交换管程的使用权,在合适的时候,条件队列首位可以进入,进入之前必须用while检查条件是否合适,优点:没用额外的进程切换。</p><h1 id="临界区的概念"><a href="#临界区的概念" class="headerlink" title="临界区的概念"></a>临界区的概念</h1><p>各个进程中对临界资源(互斥资源/共享变量,一次只能给一个进程使用)进行操作的程序片段。</p><h1 id="同步与互斥的概念"><a href="#同步与互斥的概念" class="headerlink" title="同步与互斥的概念"></a>同步与互斥的概念</h1><ul><li>同步 : 多个进程因为合作的原因使得进程的执行有先后顺序,比如某个进程需要另外一个进程提供的消息。获取消息之前进入阻塞态</li><li>互斥 : 多个进程在同一时刻只有一个进程能够进入临界区。</li></ul><h1 id="并发,并行,异步"><a href="#并发,并行,异步" class="headerlink" title="并发,并行,异步"></a>并发,并行,异步</h1><ul><li><p>并发 :在一段时间同时有多个程序运行,但其实在任一时刻,只有一个程序在CPU上运行,宏观的并发是通过不断切换实现的</p><blockquote><p>好比于一个人在一段时间里面打完电话之后去做饭</p></blockquote></li><li><p>并行 : 在CPU里面,多个程序不论是宏观还是微观上都是同时执行的。</p></li><li><p>多线程: 并发允许的一段代码,是实现异步的手段。</p></li><li><p>异步:同步是顺序执行,异步就是在等待某个资源资源做自己的事。</p></li></ul><h1 id="进程调度算法"><a href="#进程调度算法" class="headerlink" title="进程调度算法"></a>进程调度算法</h1><h2 id="先来先服务"><a href="#先来先服务" class="headerlink" title="先来先服务"></a>先来先服务</h2><p>按照请求的顺序进程调度,非剥夺式,开销小,无饥饿问题,响应时间不确定。</p><blockquote><p>对段进程不利,对I/O密集流不利</p></blockquote><h2 id="短作业优先"><a href="#短作业优先" class="headerlink" title="短作业优先"></a>短作业优先</h2><p>按照允许时间最短的顺序进行调度,非剥夺式,吞吐量高,开销可能较大。</p><blockquote><p>对短进程提供好的响应时间,对长进程不利。</p></blockquote><h2 id="最短剩余时间算法"><a href="#最短剩余时间算法" class="headerlink" title="最短剩余时间算法"></a>最短剩余时间算法</h2><p>按照进程剩余时间顺序进行调度,剥夺式,吞吐量早,开销可能较大,提供好的响应时间。</p><blockquote><p>可能导致饥饿问题,对长时间不利。</p></blockquote><h2 id="最高响应比优先调度算法"><a href="#最高响应比优先调度算法" class="headerlink" title="最高响应比优先调度算法"></a>最高响应比优先调度算法</h2><p>响应比 = 1 + 进程等待时间/处理时间。同时考虑了等待时间的长短和估计需要的执行时间长短,很好的平衡了长短进程。非抢占,吞吐量高,开销可能较大,提供好的响应时间,无饥饿问题。</p><blockquote><ol><li><strong>交互式系统</strong><br>交互式系统有大量的用户交互操作,在该系统中调度算法的目标是快速地进行响应。</li></ol></blockquote><h2 id="时间片轮转调度算法"><a href="#时间片轮转调度算法" class="headerlink" title="时间片轮转调度算法"></a>时间片轮转调度算法</h2><p>将所有就绪进程按照先来先服务算法排列,用完时间片的还未完成进程的排到最后去,剥夺式,开销小,无饥饿问题,为短进程提供好的响应时间。</p><blockquote><p>若时间片小,进程切换频繁,吞吐量低,若时间片太长,实时性得不到保障。</p></blockquote><h2 id="优先级调度算法"><a href="#优先级调度算法" class="headerlink" title="优先级调度算法"></a>优先级调度算法</h2><p>为每一个进程分配一个优先级,按优先级进行调度,为了防止低优先级的进程永远等不到调度,可以随着时间的推移增加等待进程的优先级。</p><h2 id="多级反馈队列调度算法"><a href="#多级反馈队列调度算法" class="headerlink" title="多级反馈队列调度算法"></a>多级反馈队列调度算法</h2><p>设置多个就绪队列1,2,3…优先级递减,时间片递增,只有等到更高级的优先级队列为空时才会调度当前队列的进程,如果进程用完了当前队列的时间片还未执行而安,则会移到下一队列。</p><blockquote><p>剥夺,开销可能比较大,对IO型进程有利,可能会出现饥饿问题。</p></blockquote><h1 id="什么叫优先级反转-如何解决?"><a href="#什么叫优先级反转-如何解决?" class="headerlink" title="什么叫优先级反转?如何解决?"></a>什么叫优先级反转?如何解决?</h1><p>高优先级的进程等待被一个低优先级占有资源时,就会出现优先级反转,即优先级较低的进程比优先级较高的进程先进行,此处详细解释优先级反转带来的问题,如果有一个中等优先级的进程将低优先级的进程抢占,那么此时低优先级的进程无法正常进行并在后续释放被占用的资源,导致高优先级的任务一直被挂起,直到中等优先级的进程完成后,低优先级的进程才可以继续并在后续释放占用的资源,最后高优先级的进程才可以执行。导致的问题就是高优先级的进程在中等优先级的进程调度之后。</p><h2 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h2><ul><li>优先级天花板 :当任务申请某资源时,把该任务的优先级提升到可访问这个资源的所有用户中的最高优先级,这个优先级称为该资源的优先级天花板。简单易行。</li><li>优先级继承: 当任务A申请共享资源S时,如果S正在被任务C使用,通过比较任务C与自身的优先级,如发现任务C的优先级小于自身的优先级,则将任务C的优先级提升到自身的优先级,任务C释放资源S后,再恢复任务C的原优先级。</li></ul>]]></content>
<summary type="html"><h1 id="线程与进程的区别"><a href="#线程与进程的区别" class="headerlink" title="线程与进程的区别"></a>线程与进程的区别</h1><ul>
<li><p>进程是系统进行资源分配和调度的基本单位</p>
</li>
<li><p>线程是CPU调度和分派的基本单位</p>
</li>
<li><p>线程依赖于进程而存在,一个进程至少有一个线程</p></summary>
<category term="操作系统" scheme="http://example.com/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/"/>
</entry>
<entry>
<title>第一届ACC杯(AcWing Cup)全国高校联赛</title>
<link href="http://example.com/2022/03/20/%E7%AC%AC%E4%B8%80%E5%B1%8AACC(AcWing%20Cup)%E5%85%A8%E5%9B%BD%E9%AB%98%E6%A0%A1%E8%81%94%E8%B5%9B/"/>
<id>http://example.com/2022/03/20/%E7%AC%AC%E4%B8%80%E5%B1%8AACC(AcWing%20Cup)%E5%85%A8%E5%9B%BD%E9%AB%98%E6%A0%A1%E8%81%94%E8%B5%9B/</id>
<published>2022-03-20T12:52:32.554Z</published>
<updated>2023-09-26T13:07:06.322Z</updated>
<content type="html"><![CDATA[<h1 id="第一届ACC-AcWing-Cup-全国高校联赛"><a href="#第一届ACC-AcWing-Cup-全国高校联赛" class="headerlink" title="第一届ACC(AcWing Cup)全国高校联赛"></a>第一届ACC(AcWing Cup)全国高校联赛</h1><p>Acwing举办的高校联赛比赛 </p><p>Rank(1381/7567) 过题2/3</p><span id="more"></span><h2 id="题目A"><a href="#题目A" class="headerlink" title="题目A"></a>题目A</h2><p>十六进制是一种基数为 1616 的计数系统,是一种逢 1616 进 11 的进位制。</p><p>通常用数字 0、1、2、3、4、5、6、7、8、90、1、2、3、4、5、6、7、8、9 和字母 A、B、C、D、E、FA、B、C、D、E、F 表示,其中: A∼FA∼F 表示 10∼1510∼15,这些称作十六进制数字。</p><p>观察这些数字的图案,我们可以发现,有些数字上面包含圈圈,具体来说:</p><ul><li>数字 0,4,6,9,A,D0,4,6,9,A,D 中包含一个圈。</li><li>数字 8,B8,B 中包含两个圈。</li><li>数字 1,2,3,5,7,C,E,F1,2,3,5,7,C,E,F 中不含圈。</li></ul><p>现在,给定一个十进制整数 nn,请你将其转化为十六进制表示,并数一数其十六进制表示中一共含有多少个圈圈。</p><h4 id="输入格式"><a href="#输入格式" class="headerlink" title="输入格式"></a>输入格式</h4><p>一个整数 nn。</p><h4 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h4><p>一个整数,表示整数 nn 的十六进制表示包含的圈圈总数。</p><h4 id="数据范围"><a href="#数据范围" class="headerlink" title="数据范围"></a>数据范围</h4><p>前三个测试点满足 0≤n≤1000≤n≤100,<br>所有测试点满足 0≤n≤2×1090≤n≤2×109。</p><h4 id="输入样例1:"><a href="#输入样例1:" class="headerlink" title="输入样例1:"></a>输入样例1:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">11</span><br></pre></td></tr></table></figure><h4 id="输出样例1:"><a href="#输出样例1:" class="headerlink" title="输出样例1:"></a>输出样例1:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">2</span><br></pre></td></tr></table></figure><h4 id="输入样例2:"><a href="#输入样例2:" class="headerlink" title="输入样例2:"></a>输入样例2:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">14</span><br></pre></td></tr></table></figure><h4 id="输出样例2:"><a href="#输出样例2:" class="headerlink" title="输出样例2:"></a>输出样例2:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">0</span><br></pre></td></tr></table></figure><p>简单模拟即可 注意特判0 每次都在这里栽跟头…</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// package com.ACC;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">long</span> n = scanner.nextLong();</span><br><span class="line"> <span class="keyword">long</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">0</span>) System.out.println(<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">else</span>{<span class="keyword">while</span>(n > <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">long</span> m = n% <span class="number">16</span>;</span><br><span class="line"><span class="comment">// System.out.println(m);</span></span><br><span class="line"> <span class="keyword">if</span>(m == <span class="number">0</span>|| m == <span class="number">4</span> || m == <span class="number">6</span> || m==<span class="number">9</span> || m== <span class="number">10</span>||m == <span class="number">13</span>) ans++;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(m == <span class="number">8</span> || m == <span class="number">11</span>) ans +=<span class="number">2</span>;</span><br><span class="line"> n = n / <span class="number">16</span>;</span><br><span class="line"> }</span><br><span class="line"> System.out.println(ans);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="题目B"><a href="#题目B" class="headerlink" title="题目B"></a>题目B</h2><p>农夫约翰有 nn 片连续的农田,编号依次为 1∼n1∼n。</p><p>其中有 kk 片农田中装有洒水器。</p><p>装有洒水器的农田的编号<strong>从小到大</strong>依次为 x1,x2,…,xkx1,x2,…,xk。</p><p>在某个炎热的中午,约翰觉得是时候给他的所有农田浇水了。</p><p>每个洒水器在打开以后,向两侧方向洒水,并且随着开启时间延长,有效覆盖距离也不断增长。</p><p>具体来说,我们将第 xixi 片农田中的洒水器打开,经过 11 秒后,第 xixi 片农田被其覆盖,经过 22 秒后,第 [xi−1,xi+1][xi−1,xi+1] 片农田被其覆盖,经过 jj 秒后,第 [xi−(j−1),xi+(j−1)][xi−(j−1),xi+(j−1)] 片农田被其覆盖。</p><p>注意,每个洒水器的有效覆盖距离在每经过<strong>整数秒</strong>后,才会有所增长。</p><p>例如,经过 2.52.5 秒后,被第 xixi 片农田中的洒水器覆盖的农田仍是第 [xi−1,xi+1][xi−1,xi+1] 片农田,而不是第 [xi−1.5,xi+1.5][xi−1.5,xi+1.5] 片农田。</p><p>现在,约翰将所有洒水器同时打开,请问经过多少秒后,所有农田均被灌溉。</p><h4 id="输入格式-1"><a href="#输入格式-1" class="headerlink" title="输入格式"></a>输入格式</h4><p>第一行包含整数 TT,表示共有 TT 组测试数据。</p><p>每组数据第一行包含两个整数 n,kn,k。</p><p>第二行包含 kk 个整数 x1,x2,…,xkx1,x2,…,xk。</p><h4 id="输出格式-1"><a href="#输出格式-1" class="headerlink" title="输出格式"></a>输出格式</h4><p>每组数据输出一行答案。</p><h4 id="数据范围-1"><a href="#数据范围-1" class="headerlink" title="数据范围"></a>数据范围</h4><p>前三个测试点满足 1≤n≤51≤n≤5,<br>所有测试点满足 1≤T≤2001≤T≤200,1≤n≤2001≤n≤200,1≤k≤n1≤k≤n,1≤xi≤n1≤xi≤n,xi−1<xixi−1<xi,TT 组数据的 nn 相加之和不超过 200200。</p><h4 id="输入样例:"><a href="#输入样例:" class="headerlink" title="输入样例:"></a>输入样例:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">3</span><br><span class="line">5 1</span><br><span class="line">3</span><br><span class="line">3 3</span><br><span class="line">1 2 3</span><br><span class="line">4 1</span><br><span class="line">1</span><br></pre></td></tr></table></figure><h4 id="输出样例:"><a href="#输出样例:" class="headerlink" title="输出样例:"></a>输出样例:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">3</span><br><span class="line">1</span><br><span class="line">4</span><br></pre></td></tr></table></figure><p>模拟即可</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// package com.ACC;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"> <span class="keyword">int</span> T = scanner.nextInt();</span><br><span class="line"> <span class="keyword">int</span>[] n = <span class="keyword">new</span> <span class="keyword">int</span>[T];</span><br><span class="line"> <span class="keyword">int</span>[] k = <span class="keyword">new</span> <span class="keyword">int</span>[T];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < T; i++) {</span><br><span class="line"> n[i] = scanner.nextInt();</span><br><span class="line"> k[i] = scanner.nextInt();</span><br><span class="line"> <span class="keyword">int</span>[] a = <span class="keyword">new</span> <span class="keyword">int</span>[k[i]];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < k[i]; j++) {</span><br><span class="line"> a[j] = scanner.nextInt();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> q = n[i] - a[k[i] -<span class="number">1</span>]+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> ans = Math.max(a[<span class="number">0</span>],q);</span><br><span class="line"> <span class="keyword">if</span>(k[i] == <span class="number">1</span>) ans =Math.max(a[<span class="number">0</span>],n[i] - a[<span class="number">0</span>]+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j + <span class="number">1</span> < k[i]; j++) {</span><br><span class="line"> ans = Math.max(ans, (a[j + <span class="number">1</span>] - a[j]+<span class="number">2</span>)/<span class="number">2</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(ans);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>题目C</p>]]></content>
<summary type="html"><h1 id="第一届ACC-AcWing-Cup-全国高校联赛"><a href="#第一届ACC-AcWing-Cup-全国高校联赛" class="headerlink" title="第一届ACC(AcWing Cup)全国高校联赛"></a>第一届ACC(AcWing Cup)全国高校联赛</h1><p>Acwing举办的高校联赛比赛 </p>
<p>Rank(1381/7567) 过题2/3</p></summary>
<category term="比赛" scheme="http://example.com/tags/%E6%AF%94%E8%B5%9B/"/>
</entry>
<entry>
<title>计算机网络-HTTP的几种请求</title>
<link href="http://example.com/2022/03/17/HTTP/"/>
<id>http://example.com/2022/03/17/HTTP/</id>
<published>2022-03-17T02:11:02.533Z</published>
<updated>2023-09-26T13:10:15.448Z</updated>
<content type="html"><![CDATA[<h1 id="HTTP"><a href="#HTTP" class="headerlink" title="HTTP"></a>HTTP</h1><h1 id="HTTP请求方法"><a href="#HTTP请求方法" class="headerlink" title="HTTP请求方法"></a>HTTP请求方法</h1><blockquote><p>HTTP请求方法表明了要对给定资源执行的操作,每一个请求方法都实现了不同的语义,包括:GET,HEAD.POST,PUT,PATCH,DELECT,OPTIONS,以及不常用的CONNECT,TRACE.</p></blockquote><h3 id="GET"><a href="#GET" class="headerlink" title="GET"></a>GET</h3><p>获取服务器的指定资源。</p><h3 id="HEAD"><a href="#HEAD" class="headerlink" title="HEAD"></a>HEAD</h3><p>与 GET 方法一样,都是发出一个获取服务器指定资源的请求,但服务器只会返回 Header 而不会返回 Body。用于确认 URI 的有效性及资源更新的日期时间等。一个典型应用是下载文件时,先通过 HEAD 方法获取 Header,从中读取文件大小 <code>Content-Length</code>;然后再配合 <code>Range</code> 字段,分片下载服务器资源</p><span id="more"></span><h3 id="POST"><a href="#POST" class="headerlink" title="POST"></a>POST</h3><p>提交资源到服务器或者在服务器上新建资源</p><h3 id="PUT"><a href="#PUT" class="headerlink" title="PUT"></a>PUT</h3><p>:替换整个目标资源</p><h3 id="PATCH"><a href="#PATCH" class="headerlink" title="PATCH"></a>PATCH</h3><p>:替换目标资源的部分内容</p><h3 id="DELETE"><a href="#DELETE" class="headerlink" title="DELETE"></a>DELETE</h3><p>:指定的资源。</p><h3 id="OPTIONS"><a href="#OPTIONS" class="headerlink" title="OPTIONS"></a>OPTIONS</h3><p>:用于描述目标资源的通信选项。可以用于检测服务器支持哪些 HTTP 方法,或者在 CORS 中发起一个预检请求,以检测实际请求是否可以被服务器所接受</p><h3 id="CONNECT"><a href="#CONNECT" class="headerlink" title="CONNECT"></a>CONNECT</h3><p>:建立一个到由目标资源标识的服务器的隧道</p><h3 id="TRACE"><a href="#TRACE" class="headerlink" title="TRACE"></a>TRACE</h3><p>:执行一个消息环回测试,返回到服务端的路径。客户端请求连接到目标服务器时可能会通过代理中转,通过 TRACE 方法可以查询发送出去的请求的一系列操作(<a href="https://imageslr.com/media/15993132198241.jpg">图示</a>)</p><h3 id="幂等的"><a href="#幂等的" class="headerlink" title="幂等的"></a><strong>幂等的</strong></h3><p> 一个HTTP方法是幂等的,指的是同样的请求执行一次与执行多次的效果是一样的 ,幂等方法不应该具有副作用。</p><ul><li>常见的幂等方法: GET HEAD PUT DELETE OPTIONS</li><li>常见的非幂等方法:POST</li></ul><h3 id="安全的"><a href="#安全的" class="headerlink" title="安全的"></a><strong>安全的</strong></h3><p> 一个HTTP方法是幂等的,说明此方法只对服务器进行只读的方法,不会修改服务器数据。</p><ul><li>常见的安全方法:GET,HEAD,OPTIONS</li><li>常见的不安全方法:PUT,DELETE,POST</li><li>所有安全的方法都是幂等的;有些不安全的方法如 DELETE 是幂等的,有些不安全的方法如 PUT 和 DELETE 则不是</li></ul><p><strong>可缓存的</strong>:GET、HEAD。</p><h3 id="GET与POST的区别"><a href="#GET与POST的区别" class="headerlink" title="GET与POST的区别"></a>GET与POST的区别</h3><table><thead><tr><th align="left"></th><th align="left">GET</th><th>POST</th></tr></thead><tbody><tr><td align="left">应用</td><td align="left">获取浏览器的数据</td><td>添加/修改浏览器的数据</td></tr><tr><td align="left">历史记录 / 书签</td><td align="left">可保留在浏览器历史记录中,或者收藏为书签</td><td>不可以</td></tr><tr><td align="left">Cacheable</td><td align="left">会被浏览器缓存</td><td>不会缓存</td></tr><tr><td align="left">幂等</td><td align="left">幂等,不会改变浏览器上的资源。</td><td>非幂等</td></tr><tr><td align="left">后退/刷新</td><td align="left">GET是无害的</td><td>重复提交POST表单</td></tr><tr><td align="left">参数位置</td><td align="left">query 中(直接明文暴露在链接中)</td><td>query 或 body 中</td></tr><tr><td align="left">参数长度</td><td align="left">2KB(2048个字符)</td><td>无限制</td></tr></tbody></table><h2 id="HTTP状态码"><a href="#HTTP状态码" class="headerlink" title="HTTP状态码"></a>HTTP状态码</h2><h3 id="信息响应(100–199)"><a href="#信息响应(100–199)" class="headerlink" title="信息响应(100–199)"></a>信息响应(100–199)</h3><ul><li>100 Continue:表明到目前为止都很正常,客户端可以继续发送请求或者忽略这个响应</li></ul><h3 id="成功响应-200-299"><a href="#成功响应-200-299" class="headerlink" title="成功响应(200-299)"></a>成功响应(200-299)</h3><ul><li>200 OK</li><li>201 Created 该请求已成功,并因此创建了一个新的资源,这通常是在POST请求之后后悔的响应</li><li>204 No Content:该请求已成功处理,但是返回的响应报文不包含实体的主体部分。通常用于只需要从客户端往服务器发送信息,而不需要返回数据时</li><li>206 Partial Content:服务器已经成功处理了部分 GET 请求,该请求必须包含 <code>Range</code> 头信息来指示客户端希望得到的内容范围。通常使用此类响应来实现断点续传,或者将一个大文档分为多个片段然后并行下载</li></ul><p>重定向(300-399)</p><ul><li><p>301 Moved Permanently:永久性重定向</p></li><li><p>302 临时性重定向,常见应用场景是是通过 302 跳转将所有的 HTTP 流量重定向到 HTTPS</p></li><li><p>303 See Other :和302有着相同的功能,但303明确要求客户端应该采用GET方法获取资源</p></li><li><p><strong>304 Not Modified</strong>:如果客户端发送了一个带条件的 GET 请求且该请求已被允许,而文档的内容(自上次访问以来或者根据请求的条件)并没有改变,则服务器应当返回这个状态码。304 响应不包含消息体</p></li><li><p><strong>307 Temporary Redirect</strong>:临时重定向。307 与 302 之间的唯一区别在于,当发送重定向请求的时候,307 状态码可以确保<strong>请求方法</strong>和消息主体不会发生变化;而如果使用 302 响应状态码,一些旧客户端会错误地将请求方法转换为 GET</p></li></ul><h3 id="客户端错误-400-499"><a href="#客户端错误-400-499" class="headerlink" title="客户端错误(400- 499)"></a>客户端错误(400- 499)</h3><ul><li>400 Bad Request :请求报文中存在语法问题,或者参数有误。</li><li>401 Unauthorized:未认证(没有登录)</li><li>403 Forbidden :没有权限(登陆了但没有权限)</li><li>404 Not Found :找不到资源</li><li>405 Method Not Allowed</li></ul><h3 id="服务器错误-500-599"><a href="#服务器错误-500-599" class="headerlink" title="服务器错误(500-599)"></a>服务器错误(500-599)</h3><ul><li><p>500 Internet Server error :服务器遇见了不知道如何处理的情况</p></li><li><p>502 Bad Gateway:网关错误,作为网关或代理角色的服务器,从上游服务器(如tomcat、php-fpm)中接收到的响应是无效的</p></li><li><p>Service Unavailable:服务器无法处理请求,常见原因是服务器因维护或重载而停机</p></li></ul>]]></content>
<summary type="html"><h1 id="HTTP"><a href="#HTTP" class="headerlink" title="HTTP"></a>HTTP</h1><h1 id="HTTP请求方法"><a href="#HTTP请求方法" class="headerlink" title="HTTP请求方法"></a>HTTP请求方法</h1><blockquote>
<p>HTTP请求方法表明了要对给定资源执行的操作,每一个请求方法都实现了不同的语义,包括:GET,HEAD.POST,PUT,PATCH,DELECT,OPTIONS,以及不常用的CONNECT,TRACE.</p>
</blockquote>
<h3 id="GET"><a href="#GET" class="headerlink" title="GET"></a>GET</h3><p>获取服务器的指定资源。</p>
<h3 id="HEAD"><a href="#HEAD" class="headerlink" title="HEAD"></a>HEAD</h3><p>与 GET 方法一样,都是发出一个获取服务器指定资源的请求,但服务器只会返回 Header 而不会返回 Body。用于确认 URI 的有效性及资源更新的日期时间等。一个典型应用是下载文件时,先通过 HEAD 方法获取 Header,从中读取文件大小 <code>Content-Length</code>;然后再配合 <code>Range</code> 字段,分片下载服务器资源</p></summary>
<category term="计算机网络" scheme="http://example.com/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/"/>
</entry>
<entry>
<title>操作系统-操作系统</title>
<link href="http://example.com/2022/03/14/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/"/>
<id>http://example.com/2022/03/14/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/</id>
<published>2022-03-14T07:27:02.479Z</published>
<updated>2023-09-26T13:07:00.432Z</updated>
<content type="html"><![CDATA[<h1 id="操作系统"><a href="#操作系统" class="headerlink" title="操作系统"></a>操作系统</h1><h2 id="什么是操作系统?"><a href="#什么是操作系统?" class="headerlink" title="什么是操作系统?"></a>什么是操作系统?</h2><ul><li><p>操作系统(Operating System)是管理计算机硬件和软件资源的程序 是计算机的基石</p></li><li><p><strong>操作系统本质上是一个运行在计算机上的软件程序 ,用于管理计算机硬件和软件资源。</strong> 举例:运行在你电脑上的所有应用程序都通过操作系统来调用系统内存以及磁盘等等硬件。</p></li><li><p><strong>操作系统存在屏蔽了硬件层的复杂性。</strong> 操作系统就像是硬件使用的负责人,统筹着各种相关事项。</p></li><li><p><strong>操作系统的内核(Kernel)是操作系统的核心部分,它负责系统的内存管理,硬件设备的管理,文件系统的管理以及应用程序的管理</strong>。 内核是连接应用程序和硬件的桥梁,决定着系统的性能和稳定性。</p><span id="more"></span></li></ul><p>根据进程访问资源的特点,我们可以把进程在系统上的运行分为两个级别:</p><ol><li>用户态(user mode) : 用户态运行的进程可以直接读取用户程序的数据。</li><li>系统态(kernel mode):可以简单的理解系统态运行的进程或程序几乎可以访问计算机的任何资源,不受限制。</li></ol><h2 id="线程与进程的关系"><a href="#线程与进程的关系" class="headerlink" title="线程与进程的关系"></a>线程与进程的关系</h2><p>线程是进程划分成最小的运行单位,一个进程在其执行的过程中可以产生多个线程。</p><p>线程与进程最大的区别是 </p><p>进程各部分是独立的 而各线程则不一定·</p><p>因为同一进程中的线程极有可能会相互影响。线程执行开销小,但不利于资源的管理和保护;而进程正相反</p><p>进程有哪几种状态</p><ul><li><strong>创建状态:</strong> 进程目前正在被创建,尚未到就绪状态。</li><li><strong>就绪状态:</strong> 进程已处于准备允许状态。即进程获得了除了处理器之外的一切所需资源。一但得到处理器资源(处理器分配的时间片)即可运行。</li><li><strong>运行状态</strong>: 进程正在处理器上上运行(单核 CPU 下任意时刻只有一个进程处于运行状态)。</li><li>**阻塞状态: **又称为等待状态,进程正在等待某一事件而暂停运行如等待某资源为可用或等待 IO 操作完成。即使处理器空闲,该进程也不能运行。</li><li><strong>结束状态:</strong> 进程正在从系统中消失。可能是进程正常结束或其他原因中断退出运行。</li></ul><h4 id="进程的五种状态"><a href="#进程的五种状态" class="headerlink" title="进程的五种状态"></a>进程的五种状态</h4><ul><li>创建状态 进程正在被创建,尚未到就绪状态</li><li>就绪状态 进程已处于准备运行状态,即进程获得了除了处理器之外的一切所需资源,一旦得到处理器资源(处理器分配的时间片)即可运行。</li><li>运行状态 进程正在处理器上上运行</li><li>阻塞状态 又称为等待状态,进程正在等待某一事件而暂停运行如等待某资源为可用或等待 IO 操作完成。即使处理器空闲,该进程也不能运行。</li><li>结束状态 进程正在从系统中消失。可能是进程正常结束或其他原因中断退出运行。</li></ul><h4 id="进程间的通信方式"><a href="#进程间的通信方式" class="headerlink" title="进程间的通信方式"></a>进程间的通信方式</h4><p>几种? 大约有七种</p><ul><li>管道/匿名管道</li><li>有名管道</li><li>信号</li><li>消息队列</li><li>信号量</li><li>共享内存</li><li>套接字</li></ul><h4 id="线程间的同步的方式"><a href="#线程间的同步的方式" class="headerlink" title="线程间的同步的方式"></a>线程间的同步的方式</h4><p>线程同步是两个或者多个共享关键资源的线程的并发执行,应该同步线程以避免关键的资源使用冲突,操作系统一般有下面三种线程同步的方式</p><p>1.互斥量 采采用互斥对象机制,只有拥有互斥对象的线程才有访问公共资源的权限。因为互斥对象只有一个,所以可以保证公共资源不会被多个线程同时访问。比如 Java 中的 synchronized 关键词和各种 Lock 都是这种机制。</p><p>2.信号量 它允许同一时刻多个线程访问同一资源,但是需要控制同一时刻访问此资源的最大线程数量。、</p><p>3.事件 通过通知操作的方式来保持多线程同步,还可以方便的实现多线程优先级的比较操作。</p><p>进程的调度算法 </p><ul><li>先来先服务</li><li>时间片调度</li><li>短作业优先</li><li>优先级调度</li><li><strong>多级反馈队列调度算法</strong> </li></ul><h4 id="什么是死锁"><a href="#什么是死锁" class="headerlink" title="什么是死锁"></a>什么是死锁</h4><p>多个进程可以竞争有限数量的资源。当一个进程申请资源时,如果这时没有可用资源,那么这个进程进入等待状态。有时,如果所申请的资源被其他等待进程占有,那么该等待进程有可能再也无法改变状态。这种情况称为 <strong>死锁</strong>。</p><h4 id="死锁产生的四个必要条件"><a href="#死锁产生的四个必要条件" class="headerlink" title="死锁产生的四个必要条件"></a>死锁产生的四个必要条件</h4><ul><li>互斥</li><li>非剥夺</li><li>占有并等待</li><li>循环等待</li></ul>]]></content>
<summary type="html"><h1 id="操作系统"><a href="#操作系统" class="headerlink" title="操作系统"></a>操作系统</h1><h2 id="什么是操作系统?"><a href="#什么是操作系统?" class="headerlink" title="什么是操作系统?"></a>什么是操作系统?</h2><ul>
<li><p>操作系统(Operating System)是管理计算机硬件和软件资源的程序 是计算机的基石</p>
</li>
<li><p><strong>操作系统本质上是一个运行在计算机上的软件程序 ,用于管理计算机硬件和软件资源。</strong> 举例:运行在你电脑上的所有应用程序都通过操作系统来调用系统内存以及磁盘等等硬件。</p>
</li>
<li><p><strong>操作系统存在屏蔽了硬件层的复杂性。</strong> 操作系统就像是硬件使用的负责人,统筹着各种相关事项。</p>
</li>
<li><p><strong>操作系统的内核(Kernel)是操作系统的核心部分,它负责系统的内存管理,硬件设备的管理,文件系统的管理以及应用程序的管理</strong>。 内核是连接应用程序和硬件的桥梁,决定着系统的性能和稳定性。</p></summary>
<category term="学习" scheme="http://example.com/tags/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title>计算机网络总结</title>
<link href="http://example.com/2022/03/13/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/"/>
<id>http://example.com/2022/03/13/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/</id>
<published>2022-03-13T08:41:49.559Z</published>
<updated>2023-09-26T13:07:27.173Z</updated>
<content type="html"><![CDATA[<h1 id="计算机网络"><a href="#计算机网络" class="headerlink" title="计算机网络"></a>计算机网络</h1><h2>OSI/IP 七层结构</h2><p> 从下到上</p><ul><li><p>物理层</p></li><li><p>数据链路层</p></li><li><p>网络层</p></li><li><p>传输层</p></li><li><p>会话层</p></li><li><p>表示层</p></li><li><p>应用层</p><span id="more"></span></li></ul><p>生动形象的照片理解</p><p><img src="https://s2.loli.net/2022/03/13/TtXnPW231YhvGcN.png"></p><h4>应用层</h4><p> 应用层位于传输层之上,主要提供两个终端设备上的应用程序之间信息交换的服务,它定义了信息交换的格式,消息会交给下一层传输层来传输。我们把应用层交互的数据单元称为报文。</p><p>支持协议 支持Web应用的Http 协议 邮件STMP服务</p><h4>传输层</h4><p>传输层的主要任务就是负责向两台终端设备进程之中的通信提供通用的数据传输服务</p><p><strong>传输控制协议TCP</strong> 是面向连接的 可靠的数据传输服务</p><p><strong>用户数据协议UDP</strong> 是无连接的 尽最大努力的数据传输服(不保证数据传输的可靠性)</p><h4 id="网络层"><a href="#网络层" class="headerlink" title="网络层"></a>网络层</h4><p>网络层负责为分组交换网上的不同主机提供通信服务</p><blockquote><p>在发送数据时,网络层把运输层产生的报文段或用户数据报封装成分组和包进行传送。在 TCP/IP 体系结构中,由于网络层使用 IP 协议,因此分组也叫 IP 数据报,简称数据报。</p></blockquote><h1 id="HTTP与HTTPS"><a href="#HTTP与HTTPS" class="headerlink" title="HTTP与HTTPS"></a>HTTP与HTTPS</h1><h2 id="HTTP协议"><a href="#HTTP协议" class="headerlink" title="HTTP协议"></a>HTTP协议</h2><p>HTTP协议,全称为超文本传输协议,顾名思义,HTTP 协议就是用来规范超文本的传输,超文本,也就是网络上的包括文本在内的各式各样的消,具体来说,主要是来规范浏览器和服务器端的行为的。</p><p>并且,HTTP是一个无状态协议,也就是说服务器不维护任何有关客户端过去所发请求的消息。这其实是一种懒政,有状态协议会更加复杂,需要维护状态(历史信息),而且如果客户或服务器失效,会产生状态的不一致,解决这种不一致的代价更高。</p><p>HTTP为应用层协议 它以TCP协议为底层协议(传输层) 默认端口80</p><p>通信过程主要如下</p><ul><li>服务器在80端口等待客户的等待</li><li>浏览器发起到服务器的TCP连接(创建套接字Socker )</li><li>服务器接受来自浏览器的TCP连接</li><li>浏览器与服务器交换HTTP消息</li><li>关闭TCP连接</li></ul><p>HTTP协议优点</p><p>扩展性强,速度快,跨平台支持性好</p><h2 id="HTTPS协议"><a href="#HTTPS协议" class="headerlink" title="HTTPS协议"></a>HTTPS协议</h2><p>HTTPS 协议(Hyper Text Transfer Protocol Secure),是 HTTP 的加强安全版本。HTTPS 是基于 HTTP 的,也是用 TCP 作为底层协议,并额外使用 SSL/TLS 协议用作加密和安全认证。默认端口号是 443.</p><blockquote><p>HTTPS 协议中,SSL 通道通常使用基于密钥的加密算法,密钥长度通常是 40 比特或 128 比特。</p></blockquote><p>HTTPS协议优点</p><p>保密性好,信任度高。</p><h5 id="HTTP的核心-–-SSL-TLS协议"><a href="#HTTP的核心-–-SSL-TLS协议" class="headerlink" title="HTTP的核心 – SSL/TLS协议"></a>HTTP的核心 – SSL/TLS协议</h5><p>HTTPS 之所以能达到较高的安全性要求,就是结合了 SSL/TLS 和 TCP 协议,对通信数据进行加密,解决了 HTTP 数据透明的问题。接下来重点介绍一下 SSL/TLS 的工作原理。</p><p>SSL/TLS协议的核心要素<strong>非对称加密算法</strong></p><p>非对称加密采用两个密钥——一个公钥,一个私钥。在通信时,私钥仅由解密者保存,公钥由任何一个想与解密者通信的发送者(加密者)所知。可以设想一个场景,</p><blockquote><p>在某个自助邮局,每个通信信道都是一个邮箱,每一个邮箱所有者都在旁边立了一个牌子,上面挂着一把钥匙:这是我的公钥,发送者请将信件放入我的邮箱,并用公钥锁好。但是公钥只能加锁,并不能解锁。解锁只能由邮箱的所有者——因为只有他保存着私钥。</p><p>这样,通信信息就不会被其他人截获了,这依赖于私钥的保密性。</p></blockquote><h5 id="对称加密"><a href="#对称加密" class="headerlink" title="对称加密"></a>对称加密</h5><p>使用 SSL/TLS 进行通信的双方需要使用非对称加密方案来通信,但是非对称加密设计了较为复杂的数学算法,在实际通信过程中,计算的代价较高,效率太低,因此,SSL/TLS 实际对消息的加密使用的是对称加密。</p><blockquote><p>对称加密:通信双方共享唯一密钥 k,加解密算法已知,加密方利用密钥 k 加密,解密方利用密钥 k 解密,保密性依赖于密钥 k 的保密性。</p></blockquote>]]></content>
<summary type="html"><h1 id="计算机网络"><a href="#计算机网络" class="headerlink" title="计算机网络"></a>计算机网络</h1><h2>OSI/IP 七层结构</h2>
<p> 从下到上</p>
<ul>
<li><p>物理层</p>
</li>
<li><p>数据链路层</p>
</li>
<li><p>网络层</p>
</li>
<li><p>传输层</p>
</li>
<li><p>会话层</p>
</li>
<li><p>表示层</p>
</li>
<li><p>应用层</p></summary>
<category term="计算机网络" scheme="http://example.com/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/"/>
</entry>
<entry>
<title>新年总结以及未来展望</title>
<link href="http://example.com/2022/02/22/%E6%96%B0%E5%B9%B4%E6%80%BB%E7%BB%93%E4%BB%A5%E5%8F%8A%E6%9C%AA%E6%9D%A5%E5%B1%95%E6%9C%9B/"/>
<id>http://example.com/2022/02/22/%E6%96%B0%E5%B9%B4%E6%80%BB%E7%BB%93%E4%BB%A5%E5%8F%8A%E6%9C%AA%E6%9D%A5%E5%B1%95%E6%9C%9B/</id>
<published>2022-02-22T04:55:36.000Z</published>
<updated>2023-09-26T13:11:18.247Z</updated>
<content type="html"><![CDATA[<h1 id="自我介绍"><a href="#自我介绍" class="headerlink" title="自我介绍"></a>自我介绍</h1><p>我是一个从湖南省西南地区的一个小山村里走出来的孩子</p><span id="more"></span><h2 id="高中"><a href="#高中" class="headerlink" title="高中"></a>高中</h2><p>就读于本县的一所比较好的高中,这所高中95%的同学都来自于农村,其中的学习氛围与志同道合的人不在少数,其中很多同学都聊得来,在那里与他们可以用方言直接交流。进校时自己是一个平平无奇的同学,后面高三厚积薄发名列班级第一或许是自己这辈子以来,很骄傲的一个事把。后面出成绩之后,其实 成绩并不是很理想,但是也算正常发挥。后面选择了湖南工业大学的计算机类专业。在高三之前,自己其实的想法是读这个师范专业,不要钱包分配的最好,这是长辈们根深蒂固对我思想的印象,后面我初中差点去读了师范中专。还好,我爸力排众议,让我先读高中,然后高中读完再去读师范,从这点看出,我爸其实想的更远一些,填志愿的时候,也是我一手操办,后面我选了计算机专业,我爸还时不时的唠叨,让我出去找个老师的工作,逃不出安稳两个字。</p><h2 id="大学"><a href="#大学" class="headerlink" title="大学"></a>大学</h2><p>大一是个大类专业 上学期高数学的很严重,作业也完成不错,可是大一下学期疫情来临,在家里自我懈怠。作业只管抄。过的浑浑噩噩。后面专业分流 成绩排名 200/700 自己跟着大部队选了课比较少的软件工程。</p><p>大二也是我一个人生的改变点吧。自己性格是个很热心肠的人,后面上台竞选了班长。但这也给我自己带来了许多的麻烦。比如很多事都尽心尽力,很多小事同学们都会来找我,只是自己为了对得起<strong>为大家服务</strong>这五个字,经常心力憔悴。与辅导员的交流过程大都是搪塞过去的,与辅导员和学生会的认识过程中,自己遇见了一些不能接受的时期,无法与他们同流合污,以及交流,后面我已经不想和她们进行交流,只想尽快卸任。</p><p>大三上学期终于卸任掉了自己的班长,也解放了天性似乎,想逃课就逃课,想干嘛就干嘛,这是一个很快乐的学期,但是因为自己面临着找工作与考研的选择而倍感焦虑。时长因为自己大一大二的贪玩感到后悔,只是后悔有什么用,后悔就能真正的回到过去吗,只是在浪费时间更加的后悔不已。</p><p>原本自己寒假立了个flag好好学习,也因为家里环境不好以及无休止的拜年胎死腹中。 此时此刻无比焦虑写下来可能比较好。希望自己能够找到一份比较满意的实习</p><p>…….待续</p>]]></content>
<summary type="html"><h1 id="自我介绍"><a href="#自我介绍" class="headerlink" title="自我介绍"></a>自我介绍</h1><p>我是一个从湖南省西南地区的一个小山村里走出来的孩子</p></summary>
<category term="自我介绍" scheme="http://example.com/tags/%E8%87%AA%E6%88%91%E4%BB%8B%E7%BB%8D/"/>
</entry>
<entry>
<title>年终总结</title>
<link href="http://example.com/2022/01/01/%E5%B9%B4%E7%BB%88%E6%80%BB%E7%BB%93/"/>
<id>http://example.com/2022/01/01/%E5%B9%B4%E7%BB%88%E6%80%BB%E7%BB%93/</id>
<published>2022-01-01T15:40:37.000Z</published>
<updated>2023-09-26T13:07:44.474Z</updated>
<content type="html"><![CDATA[<p> 送走了迷茫的2021年 迎来了可能最为艰难的2022年 上学期突然下定决心决定工作,这学期开学到现在陆陆续续的刷了150道力扣 其实很多都是水题。不过像我这样努力了一个月也去拿了个程序设计二等奖。也明白了我现在一直坚持的一句话,</p><p><strong>任何时候努力都不算晚</strong></p><span id="more"></span><p><img src="https://s2.loli.net/2022/01/01/fbXKBq5ujagpmzk.png"></p><p>也许愿一下自己来年的愿望吧 希望自己能够春招实习能够找到一份好的实习,秋招能够找到自己满意的工作。去想去的上海,弥补高考的遗憾。</p><p>也给来年一些期许吧</p><p>1.力扣打卡 每月满勤</p><p>2.数据结构全部再过一遍</p><p>3.学好八股!</p>]]></content>
<summary type="html"><p> 送走了迷茫的2021年 迎来了可能最为艰难的2022年 上学期突然下定决心决定工作,这学期开学到现在陆陆续续的刷了150道力扣 其实很多都是水题。不过像我这样努力了一个月也去拿了个程序设计二等奖。也明白了我现在一直坚持的一句话,</p>
<p><strong>任何时候努力都不算晚</strong></p></summary>
<category term="碎碎念" scheme="http://example.com/tags/%E7%A2%8E%E7%A2%8E%E5%BF%B5/"/>
</entry>
</feed>