-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed.xml
More file actions
415 lines (332 loc) · 35.9 KB
/
feed.xml
File metadata and controls
415 lines (332 loc) · 35.9 KB
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
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.2.1">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2022-02-19T18:09:39+08:00</updated><id>/feed.xml</id><title type="html">咖啡不加奶</title><subtitle>爱喝咖啡,乳糖不耐受</subtitle><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><entry><title type="html">Slow Fast 指针算法笔记1</title><link href="/algorithm/fat-slow-basic/" rel="alternate" type="text/html" title="Slow Fast 指针算法笔记1" /><published>2022-02-14T00:00:00+08:00</published><updated>2022-02-14T00:00:00+08:00</updated><id>/algorithm/fat-slow-basic</id><content type="html" xml:base="/algorithm/fat-slow-basic/"><![CDATA[<p>今天是2022年的情人,停更了一周,主要还是拖,写写blog,总结之前还是比较充实。还有一天就是元宵了,也意味着2022年春节即将过完。回想看看之前春节期间每天都写,虽然有点水,但还是每天写。</p>
<p>言归正传,快慢指针主要解决链表的问题,通过快慢两个速度,根据快慢指针是否相遇去解决问题。
题目,给出一个链表,编写方法判断是否存在循环,如果存在返回true,不存在返回false。如下:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class ListNode {
int value = 0;
ListNode next;
ListNode(int value){
this.value = value;
}
}
class LinkedListCycle{
public static void main(String[] args){
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);
}
public static boolean checkCycle(ListNode head){
// to do
}
}
</code></pre></div></div>
<h3 id="思路">思路</h3>
<p>思路很简单,定义两个指针遍历链表,一个指针一次走一步,一个指针一次走两步,如果这个链表是循环的,那么两个指针就会相遇,即他们指向的数相等;如果走两步的指针指向空,那么表示这个链表已经走完。关于相遇的问题是经过数学推理的,一下找不到链接,后续找到链接再贴出来。直接代码</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public static boolean checkCycle(ListNode head){
ListNode slow = head;
ListNode fast = head;
while(fast != null && slow != null){
fast = fast.next.next;
slow = slow.next;
if(slow == fast){
return true;
}
}
return false;
}
</code></pre></div></div>
<p>此时时间复杂度是O(N),N是链表节点, 空间复杂度是O(1)。</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Slow Fast Pointer" /><summary type="html"><![CDATA[今天是2022年的情人,停更了一周,主要还是拖,写写blog,总结之前还是比较充实。还有一天就是元宵了,也意味着2022年春节即将过完。回想看看之前春节期间每天都写,虽然有点水,但还是每天写。]]></summary></entry><entry><title type="html">Two Pointers笔记#2 排除相同数</title><link href="/algorithm/remove-duplicates/" rel="alternate" type="text/html" title="Two Pointers笔记#2 排除相同数" /><published>2022-02-06T00:00:00+08:00</published><updated>2022-02-06T00:00:00+08:00</updated><id>/algorithm/remove-duplicates</id><content type="html" xml:base="/algorithm/remove-duplicates/"><![CDATA[<p>题目, 给出一个排序数组,找出数组中重复的数,其中不能使用额外的存储,返回新的不重复数组长度。示例如下:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子1
输入: arr=[2, 3, 3, 3, 6, 9, 9]
输出: 4
解释: 其中不重复项是[2,3,6,9]
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子2
输入: arr=[2, 2, 2, 11]
输出: 2
解释: 其中不重复项是[2, 11]
</code></pre></div></div>
<h3 id="思路">思路</h3>
<p>我们使用Two Pointers的模式解决,其中不能使用额外的存储,意味着只能在数组中进行处理。我们使用一个Pointer去循环整个数组,另一个pointer去做不重复的指向。例如一开始两个Pointer指向2,循环开始,循环数组的pointer下标1,指向3,不重复pointer指向3,继续循环,循环数组的pointer下标2, 指向3,此时不重复pointers指向3,与循环pointer指向数值一样,不重复pointer指向不变,继续循环…看代码</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public static int remove(int[] arr){
int non = 1;
for(int i=0; i< arr.length; i++){
if(arr[non-1] != arr[i]){
arr[non] = arr[i];
non++;
}
}
return non;
}
</code></pre></div></div>
<p>此时时间复杂度是O(N),N是数组长度, 空间复杂度是O(1)。</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Two Pointers" /><summary type="html"><![CDATA[题目, 给出一个排序数组,找出数组中重复的数,其中不能使用额外的存储,返回新的不重复数组长度。示例如下:]]></summary></entry><entry><title type="html">Two Pointers笔记#1 找出相等数</title><link href="/algorithm/pair-with-target-sum/" rel="alternate" type="text/html" title="Two Pointers笔记#1 找出相等数" /><published>2022-02-05T00:00:00+08:00</published><updated>2022-02-05T00:00:00+08:00</updated><id>/algorithm/pair-with-target-sum</id><content type="html" xml:base="/algorithm/pair-with-target-sum/"><![CDATA[<p>题目, 给出一个排序数组和一个目标数,找出数组中两两相加等于目标数。示例如下:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子1
输入: arr=[1, 2, 3, 4, 6], target = 6
输出: [1, 3]
解释: arr的索引1是2,索引3是4,所以2+4 = 6
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子2
输入: arr=[2, 5, 9, 11], target = 11
输出: [0, 2]
解释: 见例1
</code></pre></div></div>
<h3 id="思路">思路</h3>
<p>这道题不算难,简单的迭代也可以求出两数和,时间复杂度是O(N*logN),因为会逐个循环这个数组迭代。我们使用Two Pointers方法,首先数组进行了排序,所以我们会用根据两数求和不断进行左边或者右边加或者减。</p>
<p>如1+6 > 6, 那么我们需要小一点的数,就是右边开始下一个项,即1+4 < 6, 说明总和小于6,那么左边需要下一个项。参考代码如下</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public static int[] search(int[] arr, int target){
int left = 0, right = arr.length - 1;
while(left < right){
int current = arr[left] + arr[right];
if(current == target)
return new int[] { left, right};
if(target> current){
left++;
}else{
right--;
}
}
return new int[] { -1,-1};
}
</code></pre></div></div>
<p>此时时间复杂度是O(N),N是数组长度, 空间复杂度是O(1)。</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Two Pointers" /><summary type="html"><![CDATA[题目, 给出一个排序数组和一个目标数,找出数组中两两相加等于目标数。示例如下:]]></summary></entry><entry><title type="html">Sliding Window 系列笔记6 找出不重复的字符</title><link href="/algorithm/no-repeat-substring/" rel="alternate" type="text/html" title="Sliding Window 系列笔记6 找出不重复的字符" /><published>2022-02-04T00:00:00+08:00</published><updated>2022-02-04T00:00:00+08:00</updated><id>/algorithm/no-repeat-substring</id><content type="html" xml:base="/algorithm/no-repeat-substring/"><![CDATA[<p>题目, 给出一个字符串,找出里面最长的不重复字符长度。示例如下:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子1
输入: String="aabccbb"
输出: 3
解释: 最长的,不重复字符是"abc",长度是3
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子2
输入: String="abbbb"
输出: 2
解释: 最长的,不重复字符是"ab",长度是2
</code></pre></div></div>
<h3 id="思路">思路</h3>
<p>依然采用Sliding Window解法,根据之前找出k个不同子字符思路,对比使用HashMap,然后对N项迭代一一进行比对即可。参看如下代码</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class MyClass {
public static int findMaxLength(String str){
int left = 0, maxLength = 0;
Map<Character, Integer> chars = new HashMap<>();
for(int right = 0; right < str.length(); right++){
char rightChar = str.charAt(right);
if(chars.containsKey(rightChar)){
left = Math.max(left, chars.get(rightChar) + 1);
}
chars.put(rightChar, right);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
}
</code></pre></div></div>
<p>此时时间复杂度是O(N),N是子字符长度, 空间复杂度是O(K),K表示存在的不重复子项个数.</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Sliding Window" /><summary type="html"><![CDATA[题目, 给出一个字符串,找出里面最长的不重复字符长度。示例如下:]]></summary></entry><entry><title type="html">Sliding Window 系列笔记5 找出最长的2种组合</title><link href="/algorithm/fruits-into-baskets/" rel="alternate" type="text/html" title="Sliding Window 系列笔记5 找出最长的2种组合" /><published>2022-02-03T00:00:00+08:00</published><updated>2022-02-03T00:00:00+08:00</updated><id>/algorithm/fruits-into-baskets</id><content type="html" xml:base="/algorithm/fruits-into-baskets/"><![CDATA[<p>题目, 给出一个字符数组,里面每个字符代表一个类别,只能够从中挑出两种类别,挑选过程中并且不能够中断,求挑选出最长的字符长度。示例如下:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子1
输入: String=['A', 'B', 'C', 'A', 'C']
输出: 3
解释: 可以从 'C' 开始挑选,那么就是 'C' 'A' 'C',长度是3
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子2
输入: String=['A', 'B', 'C', 'B', 'B', 'C']
输出: 5
解释: 可以从 'C' 开始挑选,那么就是 'C' 'B' 'B' 'B' 'C',长度是5
</code></pre></div></div>
<h3 id="思路">思路</h3>
<p>依然采用Sliding Window解法,有点类似之前的找出K个不同字符的代码,这次的K换为2,参看如下代码</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class MyClass {
public static int findFruitLength(char[] arr){
int left = 0, maxLength = 0;
Map<Character, Integer> fruits = new HashMap<>();
for(int right = 0; right < arr.length; right++){
fruits.put(arr[right], fruits.getOrDefault(arr[right], 0) + 1);
while(fruits.size() > 2){
fruits.put(arr[left],fruits.get(arr[left])-1);
if(fruits.get(arr[left]) == 0){
fruits.remove(arr[left]);
}
left++;
}
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
}
</code></pre></div></div>
<p>此时时间复杂度是O(N),N是子字符长度, 空间复杂度是O(1)。</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Sliding Window" /><summary type="html"><![CDATA[题目, 给出一个字符数组,里面每个字符代表一个类别,只能够从中挑出两种类别,挑选过程中并且不能够中断,求挑选出最长的字符长度。示例如下:]]></summary></entry><entry><title type="html">Sliding Window 系列笔记4 数组中最长唯一子字符长度</title><link href="/algorithm/sliding-window-longest-substring-with-k-distinct-character/" rel="alternate" type="text/html" title="Sliding Window 系列笔记4 数组中最长唯一子字符长度" /><published>2022-02-02T00:00:00+08:00</published><updated>2022-02-02T00:00:00+08:00</updated><id>/algorithm/sliding-window-longest-substring-with-k-distinct-character</id><content type="html" xml:base="/algorithm/sliding-window-longest-substring-with-k-distinct-character/"><![CDATA[<h3 id="题目">题目</h3>
<p>给出一个字符串,找出字符串中唯一的,最长的并且子字符个数等于K的长度,示例如下:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子1
输入: String="araaci", K=2
输出: 4
解释: 最长子字符串长度是4,"araa"
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子2
输入: String="araaci", K=1
输出: 2
解释: 最长子字符串长度是2,"aa"
</code></pre></div></div>
<h3 id="思路">思路</h3>
<p>依然采用Sliding Window解法,由于需要不停的比对判断是否超过k的个数,所以考虑使用HashMap;</p>
<p>1 - 首先我们逐个加入子字符.</p>
<p>2 - 判断加入的子字符是否超过 K 个数.</p>
<p>3 - 如果超过 K 个数,那么就要移除第一个 子字符</p>
<p>4 - 一直比对,保留最长的个数</p>
<p>参看如下代码</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class LongestSubstringKDistinct {
public static int findLength(String str, int k){
if (str == null || str.length() == 0 || str.length() < k)
throw new IllegalArgumentException();
int left = 0, maxLength = 0;
Map<Character, Integer> charsMap = new HashMap<>();
for(int right =0; right < str.length(); right++){
char rightChar = str.charAt(right);
charsMap.put(rightChar, charsMap.getOrDefault(rightChar, 0) + 1);
while(charsMap.size()>k){
char leftChar = str.charAt(left);
charsMap.put(leftChar, charsMap.get(leftChar) -1);
if(charsMap.get(leftChar) == 0){
charsMap.remove(leftChar);
}
left++;
}
maxLength = Math.max(maxLength, right-left+1);
}
return maxLength;
}
}
</code></pre></div></div>
<p>此时时间复杂度是O(N), 空间复杂度是O(K)。</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Sliding Window" /><summary type="html"><![CDATA[题目]]></summary></entry><entry><title type="html">Sliding Window 系列 笔记3</title><link href="/algorithm/sliding-window-2/" rel="alternate" type="text/html" title="Sliding Window 系列 笔记3" /><published>2022-02-01T00:00:00+08:00</published><updated>2022-02-01T00:00:00+08:00</updated><id>/algorithm/sliding-window-2</id><content type="html" xml:base="/algorithm/sliding-window-2/"><![CDATA[<h3 id="题目">题目</h3>
<p>给出一个非负数组和一个非负数s,找出数组中总和大于或者等于s的最小连续子乡长度。示例如下,</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子1
输入: [2, 1, 5, 2, 3, 2], s=7
输出: 2
解释: 最小的子项大于或者等于7。[5,2]的长度是2。
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>例子2
输入: [2, 1, 5, 2, 8], s=7
输出: 1
解释: 最小的子项大于或者等于7。[8]的长度是1。
</code></pre></div></div>
<h3 id="思路">思路</h3>
<p>根据之前的Sliding Window 解法,目前需要考虑的是连续子项的个数变化,需要把子项不断相加,直到总和大于或者等于s,那么记录这些子项的个数,同时把第一个子项去除,继续加入新的子项。见代码</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class MinSizeSubArraySum {
public static int findMinSubArray(int[] arr, int s){
int windowSum = 0, minLength = Integer.MAX_VALUE;
int left = 0;
for(int right = 0; right<= arr.length, right++){
windowSum += arr[right];
if(windowSum >= s){
minLength = Math.min(minLength, right - left + 1);
windowSum -= arr[left];
left++;
}
}
return minLength == Integer.MAX_VALUE ? 0 : minLength;
}
}
</code></pre></div></div>
<p>时间复杂度是O(N), 空间复杂度是O(1)。</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Sliding Window" /><summary type="html"><![CDATA[题目]]></summary></entry><entry><title type="html">Sliding Window 系列笔记2</title><link href="/algorithm/sliding-window-1/" rel="alternate" type="text/html" title="Sliding Window 系列笔记2" /><published>2022-01-31T00:00:00+08:00</published><updated>2022-01-31T00:00:00+08:00</updated><id>/algorithm/sliding-window-1</id><content type="html" xml:base="/algorithm/sliding-window-1/"><![CDATA[<p>紧接昨日思路,再来个递进,看题目,给出一个非负数组和一个非负数的K,找出数组中最大的连续K子项总和。示例如下,</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Input: [2, 1, 5, 1, 3, 2], k=3
Output: 9
Explanation: Subarray with maximum sum is [5, 1, 3].
</code></pre></div></div>
<p>因为数组的缘故,可以先考虑迭代解决。代码如下</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class MaxSumSubArrayOfSizeK {
public static int findMaxSumSubArray(int[] arr, int k){
int maxSum = 0, windowSum;
for(int i = 0; i<= arr.length -k, i++){
windowSum = 0;
for(int j = i; j <i+k; j++){
windowSum += arr[j];
}
maxSum = Math.max(maxSum, windowSum);
}
return maxSum;
}
}
</code></pre></div></div>
<p>同昨天缘故一样,时间复杂度还是O(N * K),N是 数组长度。采用Sliding Window把 时间复杂度降下来。
思路,找出每k个子项的相同项,不同的都是在一头一尾,头尾替换就可以。</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class MaxSumSubArrayOfSizeK {
public static int findMaxSumSubArray(int[] arr, int k){
int maxSum = 0, windowSum = 0;
int left = 0;
for(int right = 0; right<= arr.length, right++){
windowSum += arr[right];
if(right >= k-1){
maxSum = Math.math(maxSum, windowSum);
windowSum -= arr[left];
left++;
}
}
return maxSum;
}
}
</code></pre></div></div>
<p>时间复杂度是O(N), 空间复杂度是O(1)。</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Sliding Window" /><summary type="html"><![CDATA[紧接昨日思路,再来个递进,看题目,给出一个非负数组和一个非负数的K,找出数组中最大的连续K子项总和。示例如下,]]></summary></entry><entry><title type="html">Sliding Window 系列笔记。找出连续k个数的平均值</title><link href="/algorithm/slide-window/" rel="alternate" type="text/html" title="Sliding Window 系列笔记。找出连续k个数的平均值" /><published>2022-01-30T00:00:00+08:00</published><updated>2022-01-30T00:00:00+08:00</updated><id>/algorithm/slide-window</id><content type="html" xml:base="/algorithm/slide-window/"><![CDATA[<p>问题,给出一个数组,找出所有连续的K个子项平均值。</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Array: [2, 4, 2, 6, -3, -5, 8], K = 3
</code></pre></div></div>
<p>如上数组,求解</p>
<p>第一个平均值 (2 + 4 + 2)/3 => 2.6</p>
<p>第二个平均值 (4 + 2 + 6)/3 => 4</p>
<p>第三个平均值 (2 + 6 + (-3))/3 => 1.6</p>
<p>…</p>
<p>根据以上思路,得出最简单的解法,直接迭代求出</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class AverageOfSubarrayOfSizeK{
public static double[] find(int[]arr, int K){
double[] ans = new double[arr.length - k +1];
for(int i = 0; i<= arr.length -k; i++){
double sum = 0;
for(int j = i; j<i+K; j++){
sum += arr[j];
}
ans[i] = sum /K;
}
return ans;
}
}
</code></pre></div></div>
<p>我们来看下时间复杂度,每次循环一个子项的时候,都要在这个子项的情况下,在持续加K个子项,所以应该是O(N*K),N是数组的子项个数。</p>
<p>这个时间复杂度我们还能够优化,我们来分析,我们在进行逐项迭代时候,例如第一个(2 + 4 + 2),第二个 (4 + 2 + 6)。这里面存在着重复的项目,4,2是这两个迭代里面出现的。所以我们可以这样处理,在进行第二个项目(4 + 2 + 6)计算时候,可以把第一个项目(2 + 4 + 2)中的第一个2移除,就是总数减去2,然后总数加上6,就能够在保留砍头去尾,达到Sliding Window 效果,直接上代码</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class AverageOfSubarrayOfSizeK{
public static double[] find(int[]arr, int K){
double[] ans = new double[arr.length - k +1];
double sum = 0;
int left = 0;
for(int right = 0; right<= arr.length; right++){
sum += arr[right];
if(sum >= K-1){
ans[left] = sum/K;
sum -= arr[left];
left++;
}
}
return ans;
}
}
</code></pre></div></div>
<p>现在这个时间复杂度就是O(N)</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="Algorithm" /><category term="Sliding Window" /><summary type="html"><![CDATA[问题,给出一个数组,找出所有连续的K个子项平均值。]]></summary></entry><entry><title type="html">如何在网站上增加Google analytics,手把手操作</title><link href="/google%20analytics/add-google-analist-to-github/" rel="alternate" type="text/html" title="如何在网站上增加Google analytics,手把手操作" /><published>2022-01-29T00:00:00+08:00</published><updated>2022-01-29T00:00:00+08:00</updated><id>/google%20analytics/add-google-analist-to-github</id><content type="html" xml:base="/google%20analytics/add-google-analist-to-github/"><![CDATA[<p>如何在网站上增加Google的分析,大致说下思路,在Google Analytics上创建你的账号,然后填写相关需要观察网站的信息,顺利验证后会生成一段代码,主要用于Google对网站的访问情况记录,把这段代码放在网站的head标签内即可。下面直接来手把手操作。</p>
<p>需要技能:HTML, CSS, Javascript</p>
<h3 id="步骤一创建账号">步骤一:创建账号</h3>
<p>首先去到网站创建账号,创建账号码,需要填写几个tab上的信息</p>
<p><img src="https://s3.bmp.ovh/imgs/2022/01/2ce13d293c5cae86.png" alt="" />
基本信息</p>
<p><img src="https://s3.bmp.ovh/imgs/2022/01/fd101b86a66d6c20.png" alt="" />
你的business信息</p>
<p><img src="https://s3.bmp.ovh/imgs/2022/01/581ca14a203eb3d3.png" alt="" />
网站的话选择web</p>
<h3 id="步骤二获取代码">步骤二:获取代码</h3>
<p>成功后填入网站的相关信息,名称以及URL。验证顺利后就会生成一段代码,如图
<img src="https://s3.bmp.ovh/imgs/2022/01/28424e138d8d1b13.png" alt="" /></p>
<p>获取代码</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
</script>
</code></pre></div></div>
<h3 id="步骤三加入代码">步骤三:加入代码</h3>
<p>根据步骤三生成的代码,找到网站的首页head标签加入代码。建议这个头部html写成模版,便于每个页面利用。</p>
<p>把代码复制好后就完成了,完成效果图如图:</p>
<p><img src="https://s3.bmp.ovh/imgs/2022/01/71386d7df9db8eb9.png" alt="" /></p>
<h3 id="最后">最后</h3>
<p>非常简单的一个内容,做个记录分享。</p>]]></content><author><name>{"name"=>nil, "avatar"=>"/assets/images/espresso-avatar.jpg", "bio"=>"博主平时写点代码,读点书,偶尔跑步,参加铁人三项,也喜欢音乐", "location"=>nil, "email"=>"yxr.306789@gmail.com", "links"=>[{"label"=>"Email", "icon"=>"fas fa-fw fa-envelope-square"}, {"label"=>"Website", "icon"=>"fas fa-fw fa-link"}, {"label"=>"Twitter", "icon"=>"fab fa-fw fa-twitter-square"}, {"label"=>"Facebook", "icon"=>"fab fa-fw fa-facebook-square"}, {"label"=>"GitHub", "icon"=>"fab fa-fw fa-github"}, {"label"=>"Instagram", "icon"=>"fab fa-fw fa-instagram"}]}</name><email>yxr.306789@gmail.com</email></author><category term="google analytics" /><category term="website" /><summary type="html"><![CDATA[如何在网站上增加Google的分析,大致说下思路,在Google Analytics上创建你的账号,然后填写相关需要观察网站的信息,顺利验证后会生成一段代码,主要用于Google对网站的访问情况记录,把这段代码放在网站的head标签内即可。下面直接来手把手操作。]]></summary></entry></feed>