@@ -25,8 +25,8 @@ Tags are following:
25
25
* [ Stack] ( /Stack )
26
26
* [ Tree] ( /Tree )
27
27
28
- # Details:
29
- ## Array tag:
28
+ # Details
29
+ ## Array
30
30
* [ #35 Search Insert Position] ( /Array/Array.Console/Array.Lib/SearchInsertPosition.cs )
31
31
32
32
* [ #118 Pascal’s Triangle] ( /Array/Array.Console/Array.Lib/PascalsTriangle.cs )
@@ -36,100 +36,16 @@ Tags are following:
36
36
* [ #414 Third Maximum Number] ( /Array/Array.Console/Array.Lib/ThirdMaximumNumber.cs )
37
37
38
38
* [ #66 Plus One] ( http://blog.csdn.net/daigualu/article/details/71056697 )
39
- ``` C#
40
- public int [] PlusOne (int [] digits )
41
- {
42
- int index = digits .Length - 1 ;
43
- if (digits [index ]< 9 )
44
- {
45
- digits [index ]++ ;
46
- return digits ;
47
- }
48
- if (index == 0 ) return new int []{1 ,0 };
49
- int i = index ;
50
- while (digits [i ]== 9 )
51
- {
52
- digits [i ] = 0 ; // 位溢出
53
- if (i == 0 ) // 所有的位溢出
54
- {
55
- int [] rtn = new int [index + 2 ];
56
- rtn [0 ]= 1 ;
57
- return rtn ;
58
- }
59
- i -- ;
60
- }
61
- digits [i ]++ ; // 第i位不为9(i > 0)
62
- return digits ;
63
- }
64
- ```
39
+
65
40
* [ #121 Best time to buy and sell stock] ( http://blog.csdn.net/daigualu/article/details/71038726 )
66
- ``` C#
67
- public int MaxProfit (int [] prices ) {
68
- int premax = 0 ;
69
- int curmax = 0 ;
70
- for (int i = 1 ; i < prices .Length ; i ++ )
71
- {
72
- int cal = prices [i ]- prices [i - 1 ]; // 相邻项差值
73
- curmax = Math .Max (cal ,cal + curmax ); // 参考当前项后的最大值
74
- premax = Math .Max (curmax ,premax ); // 赚钱最大值
75
- }
76
- return premax ;
77
- }
78
- ```
41
+
79
42
* [ #26 Remove Duplicates from Sorted Array] ( http://blog.csdn.net/daigualu/article/details/71064545 )
80
- ``` C#
81
- public int RemoveDuplicates (int [] nums )
82
- {
83
- // nums have been sorted
84
- if (nums .Length == 0 ) return 0 ;
85
- if (nums .Length == 1 ) return 1 ;
86
- int j = 0 ; // 指向不同元素的指针
87
- for (int i = 0 ; i < nums .Length ; i ++ )
88
- {
89
- while (i + 1 < nums .Length && nums [i ]== nums [i + 1 ])
90
- i ++ ;
91
- if (i + 1 < nums .Length )
92
- {
93
- j ++ ;
94
- if (j < i + 1 ) // [j-1,i]间元素相等
95
- nums [j ] = nums [i + 1 ];//
96
- }
97
- else
98
- j ++ ;
99
- }
100
- return j ;
101
- }
102
- ```
43
+
103
44
104
45
* [ #122 BestTimeToBuyandSellStockII] ( http://blog.csdn.net/daigualu/article/details/71104584 )
105
- ``` C#
106
- public int MaxProfit (int [] prices ) {
107
- int totalProfit = 0 ;
108
- for (int i = 0 ;i < prices .Length - 1 ;i ++ )
109
- {
110
- if (prices [i + 1 ]> prices [i ])
111
- totalProfit += prices [i + 1 ] - prices [i ];
112
- }
113
- return totalProfit ;
114
- }
115
- ```
46
+
116
47
* [ #27 Remove element] ( http://blog.csdn.net/daigualu/article/details/71104482 )
117
- ``` C#
118
- public int RemoveElement (int [] nums , int val )
119
- {
120
- int i = 0 ; // 指向不等于元素val
121
- for (int j = 0 ; j < nums .Length ;j ++ )
122
- {
123
- while (j < nums .Length && nums [j ]== val )
124
- j ++ ;
125
- if (i < j && j < nums .Length )
126
- nums [i ]= nums [j ];
127
- if (j < nums .Length )
128
- i ++ ;
129
- }
130
- return i ;
131
- }
132
- ```
48
+
133
49
#### 532 K-diff Pairs in an Array
134
50
* [ Github:#532 K-diff Pairs in an Array] ( /Array/Array.Lib/FindPairsSln.cs )
135
51
* [ CSDN:#532 K-diff Pairs in an Array] ( http://blog.csdn.net/daigualu/article/details/71129806 )
@@ -157,238 +73,23 @@ Tags are following:
157
73
## Hash Table
158
74
* [ #136 Single number] ( /HashTable/HashTable.Lib/SingleOneSln.cs )
159
75
160
- ``` C#
161
- public int SingleNumber (int [] nums )
162
- {
163
- HashSet < int > hash = new HashSet <int >();
164
- foreach (int item in nums )
165
- {
166
- if (hash .Contains (item ))
167
- hash .Remove (item );
168
- else
169
- hash .Add (item );
170
- }
171
- return hash .Min ();
172
-
173
- }
174
- ```
175
76
* [ #1 Two Sum] ( /HashTable/HashTable.Lib/TwoSumSln.cs )
176
- ``` C#
177
- public int [] TwoSum (int [] nums , int target )
178
- {
179
- Dictionary < int , int > dict = new Dictionary <int , int >();
180
- for (int i = 0 ; i < nums .Length ;i ++ )
181
- {
182
- if (dict .ContainsKey (target - nums [i ]))
183
- return new int [] { dict [target - nums [i ]] ,i };
184
- else
185
- {
186
- if (! dict .ContainsKey (nums [i ])) // 同一个元素不能用两次
187
- dict .Add (nums [i ], i );
188
- }
189
77
190
- }
191
- return null ;
192
- }
193
- ```
194
78
195
79
* [ #447 Number of Boomerangs] ( /HashTable/HashTable.Lib/Boomerangs.cs )
196
- ``` C#
197
- public static int NumberOfBoomerangs (int [,] points )
198
- {
199
- Dictionary < double , int > dict = new Dictionary <double , int >();
200
- int len = points .GetUpperBound (0 );
201
- int rtnCnt = 0 ;
202
- for (int i = 0 ; i <= len ; i ++ )
203
- {
204
- // 3点变2点
205
- for (int j = 0 ; j <= len ; j ++ )
206
- {
207
- if (i == j ) continue ;
208
- double d = distance (points [i , 0 ], points [j , 0 ], points [i , 1 ], points [j , 1 ]);
209
- if (dict .ContainsKey (d ))
210
- dict [d ]++ ;
211
- else dict .Add (d , 1 );
212
- }
213
- foreach (var item in dict )
214
- {
215
- if (item .Value > 1 )
216
- {
217
- // 如果找到了value个,因为有顺序,所以排序
218
- rtnCnt += item .Value * (item .Value - 1 );
219
- }
220
- }
221
- dict .Clear ();
222
- }
223
- return rtnCnt ;
224
- }
225
-
226
- private double distance (int x1 , int x2 , int y1 , int y2 )
227
- {
228
- int x = x1 - x2 ;
229
- int y = y1 - y2 ;
230
-
231
- return Math .Sqrt (x * x + y * y );
232
- }
233
- ```
234
-
235
-
236
-
237
80
238
81
* [ #463 . Island Perimeter] ( /HashTable/HashTable.Lib/IslandPerimeter.cs )
239
- ``` C#
240
- public int LandPerimeter (int [,] grid )
241
- {
242
- int rtn = 0 ;
243
- for (int i = 0 ; i <= grid .GetUpperBound (0 );i ++ )
244
- {
245
- for (int j = 0 ;j <= grid .GetUpperBound (1 );j ++ )
246
- {
247
- if (grid [i , j ] == 1 )
248
- rtn += 4 - surroundingIsland (grid ,i ,j );
249
- }
250
- }
251
- return rtn ;
252
- }
253
-
254
- // 获取某个网格周围的网格数([0,4])
255
- // noy: y向网格索引(行)
256
- // nox:x向网格索引(列)
257
- private int surroundingIsland (int [,] grid , int noy , int nox )
258
- {
259
- int rtnCnt = 0 ;
260
- if (nox > 0 )
261
- {
262
- if (grid [noy , nox - 1 ] == 1 )
263
- rtnCnt ++ ;
264
- }
265
- if (nox < grid .GetUpperBound (1 ))// nox小于(列)最大索引吗
266
- {
267
- if (grid [noy , nox + 1 ] == 1 )
268
- rtnCnt ++ ;
269
- }
270
- if (noy > 0 )
271
- {
272
- if (grid [noy - 1 , nox ] == 1 )
273
- rtnCnt ++ ;
274
- }
275
- if (noy < grid .GetUpperBound (0 ))// noy小于最大(行)索引吗
276
- {
277
- if (grid [noy + 1 , nox ] == 1 )
278
- rtnCnt ++ ;
279
- }
280
- return rtnCnt ;
281
- }
282
- ```
283
-
284
-
285
82
286
83
* [ #409 Longest Palindrome] ( /HashTable/HashTable.Lib/LongestPalindrome.cs )
287
84
288
- ``` C#
289
- public int NumberOfPalindrome (string s )
290
- {
291
- HashSet < int > hash = new HashSet <int >();
292
- int count = 0 ;
293
- foreach (var item in s )
294
- {
295
- if (hash .Contains (item ))
296
- {
297
- hash .Remove (item ); // 配对成功,
298
- count ++ ; // count加1
299
- }
300
- else
301
- hash .Add (item );
302
- }
303
- return hash .Count > 0 ? count * 2 + 1 : count * 2 ;
304
- }
305
- ```
306
85
## Linked List
307
86
[ #141 Linked List Cycle] ( http://blog.csdn.net/daigualu/article/details/69055927 )
308
- ``` C#
309
- public bool HasCycle (ListNode head )
310
- {
311
- if (head == null ) return false ;
312
- if (head .next == null ) return false ;
313
- if (head .next .next == null ) return false ;
314
-
315
- ListNode slow = head ;
316
- ListNode fast = head ;
317
87
318
- while (fast != null && fast .next != null )
319
- {
320
- slow = slow .next ;
321
- fast = fast .next .next ;
322
- if (fast == null )
323
- return false ; // 快指针如果为null,不存在环
324
- if (fast .val == slow .val )
325
- {
326
- return true ; // 找到节点数据域相等点,存在环
327
- }
328
- }
329
- return false ;
330
- }
331
- ```
332
88
[ #237 Delete Node in a Linked List] ( http://blog.csdn.net/daigualu/article/details/69055991 )
333
- ``` C#
334
- void deleteNode (ListNode node ) {
335
- if (node == null )
336
- return ;
337
- node .val = node .next .val ;
338
- node .next = node .next .next ;
339
- }
340
- ```
89
+
341
90
[ #83 Remove Duplicates from Sorted List] ( http://blog.csdn.net/daigualu/article/details/69093677 )
342
- ``` C#
343
- public ListNode DeleteDuplicates (ListNode head )
344
- {
345
- ListNode diff = head ;
346
- ListNode tmp = head ;
347
- while (tmp != null && tmp .next != null )
348
- {
349
- while (tmp .next != null && tmp .next .val == tmp .val )
350
- {
351
- tmp = tmp .next ;
352
- }
353
- diff .next = tmp .next ;// 找到一个新值
354
- diff = diff .next ;// 迭代
355
- tmp = diff ;
356
- }
357
91
358
- return head ;
359
- }
360
- ```
361
92
[ #160 Intersection of Two Linked Lists] ( http://blog.csdn.net/daigualu/article/details/69526717 )
362
- ``` C#
363
- public ListNode GetIntersectionNode (ListNode headA , ListNode headB )
364
- {
365
- if (headA == null || headB == null )
366
- return null ;
367
-
368
- ListNode a = headA ;
369
- ListNode b = headB ;
370
-
371
- while (a != b )
372
- {
373
-
374
- if (a == null )
375
- a = headB ;
376
- else
377
- {
378
- a = a .next ;
379
- }
380
-
381
- if (b == null )
382
- b = headA ;
383
- else
384
- {
385
- b = b .next ;
386
- }
387
-
388
- }
389
- return a ;
390
- }
391
- ```
392
93
393
94
[ #203 Remove Linked List Elements] ( http://blog.csdn.net/daigualu/article/details/69389243 )
394
95
``` C#
0 commit comments