@@ -84,7 +84,7 @@ public IList<int> GetRow(int rowIndex) {
84
84
}
85
85
```
86
86
* [ #66 Plus One] ( http://blog.csdn.net/daigualu/article/details/71056697 )
87
- ```
87
+ ``` C#
88
88
public int [] PlusOne (int [] digits )
89
89
{
90
90
int index = digits .Length - 1 ;
@@ -111,7 +111,7 @@ public IList<int> GetRow(int rowIndex) {
111
111
}
112
112
```
113
113
* [ #121 Best time to buy and sell stock] ( http://blog.csdn.net/daigualu/article/details/71038726 )
114
- ```
114
+ ``` C#
115
115
public int MaxProfit (int [] prices ) {
116
116
int premax = 0 ;
117
117
int curmax = 0 ;
@@ -125,7 +125,7 @@ public IList<int> GetRow(int rowIndex) {
125
125
}
126
126
```
127
127
* [ #26 Remove Duplicates from Sorted Array] ( http://blog.csdn.net/daigualu/article/details/71064545 )
128
- ```
128
+ ``` C#
129
129
public int RemoveDuplicates (int [] nums )
130
130
{
131
131
// nums have been sorted
@@ -149,6 +149,37 @@ public IList<int> GetRow(int rowIndex) {
149
149
}
150
150
```
151
151
152
+ * [ #122 BestTimeToBuyandSellStockII] ( http://blog.csdn.net/daigualu/article/details/71104584 )
153
+ ``` C#
154
+ public int MaxProfit (int [] prices ) {
155
+ int totalProfit = 0 ;
156
+ for (int i = 0 ;i < prices .Length - 1 ;i ++ )
157
+ {
158
+ if (prices [i + 1 ]> prices [i ])
159
+ totalProfit += prices [i + 1 ] - prices [i ];
160
+ }
161
+ return totalProfit ;
162
+ }
163
+ ```
164
+ * [ #27 Remove element] ( http://blog.csdn.net/daigualu/article/details/71104482 )
165
+ ``` C#
166
+ public int RemoveElement (int [] nums , int val )
167
+ {
168
+ int i = 0 ; // 指向不等于元素val
169
+ for (int j = 0 ; j < nums .Length ;j ++ )
170
+ {
171
+ while (j < nums .Length && nums [j ]== val )
172
+ j ++ ;
173
+ if (i < j && j < nums .Length )
174
+ nums [i ]= nums [j ];
175
+ if (j < nums .Length )
176
+ i ++ ;
177
+ }
178
+ return i ;
179
+ }
180
+ ```
181
+
182
+
152
183
## Hash Table
153
184
* [ #136 Single number] ( /HashTable/HashTable.Lib/SingleOneSln.cs )
154
185
@@ -300,7 +331,7 @@ public int[] TwoSum(int[] nums, int target)
300
331
```
301
332
## Linked List
302
333
[ #141 Linked List Cycle] ( http://blog.csdn.net/daigualu/article/details/69055927 )
303
- ```
334
+ ``` C#
304
335
public bool HasCycle (ListNode head )
305
336
{
306
337
if (head == null ) return false ;
@@ -325,7 +356,7 @@ public int[] TwoSum(int[] nums, int target)
325
356
}
326
357
```
327
358
[ #237 Delete Node in a Linked List] ( http://blog.csdn.net/daigualu/article/details/69055991 )
328
- ```
359
+ ``` C#
329
360
void deleteNode (ListNode node ) {
330
361
if (node == null )
331
362
return ;
@@ -334,7 +365,7 @@ void deleteNode(ListNode node) {
334
365
}
335
366
```
336
367
[ #83 Remove Duplicates from Sorted List] ( http://blog.csdn.net/daigualu/article/details/69093677 )
337
- ```
368
+ ``` C#
338
369
public ListNode DeleteDuplicates (ListNode head )
339
370
{
340
371
ListNode diff = head ;
@@ -354,7 +385,7 @@ void deleteNode(ListNode node) {
354
385
}
355
386
```
356
387
[ #160 Intersection of Two Linked Lists] ( http://blog.csdn.net/daigualu/article/details/69526717 )
357
- ```
388
+ ``` C#
358
389
public ListNode GetIntersectionNode (ListNode headA , ListNode headB )
359
390
{
360
391
if (headA == null || headB == null )
@@ -386,7 +417,7 @@ void deleteNode(ListNode node) {
386
417
```
387
418
388
419
[ #203 Remove Linked List Elements] ( http://blog.csdn.net/daigualu/article/details/69389243 )
389
- ```
420
+ ``` C#
390
421
public ListNode RemoveElements (ListNode head , int val )
391
422
{
392
423
if (head == null )
@@ -418,7 +449,7 @@ void deleteNode(ListNode node) {
418
449
}
419
450
```
420
451
[ #206 Reverse Linked List] ( http://blog.csdn.net/daigualu/article/details/69372119 )
421
- ```
452
+ ``` C#
422
453
public ListNode ReverseList (ListNode head )
423
454
{
424
455
if (head == null || head .next == null )
@@ -437,7 +468,7 @@ void deleteNode(ListNode node) {
437
468
}
438
469
```
439
470
[ #234 Palindrome Linked List] ( http://blog.csdn.net/daigualu/article/details/69388513 )
440
- ```
471
+ ``` C#
441
472
public bool IsPalindrome (ListNode head )
442
473
{
443
474
int nodeCnt = nodeCount (head );
@@ -473,7 +504,7 @@ void deleteNode(ListNode node) {
473
504
}
474
505
```
475
506
[ #21 Merge Two Sorted Lists] ( http://blog.csdn.net/daigualu/article/details/69565969 )
476
- ```
507
+ ``` C#
477
508
public ListNode MergeTwoLists (ListNode l1 , ListNode l2 )
478
509
{
479
510
if (l1 == null ) return l2 ;
@@ -526,7 +557,7 @@ void deleteNode(ListNode node) {
526
557
## Math
527
558
[ #231 Power of Two] ( http://blog.csdn.net/daigualu/article/details/69102931 )
528
559
529
- ```
560
+ ``` C#
530
561
public bool IsPowerOfTwo (int n )
531
562
{
532
563
if (n <= 0 ) return false ;
@@ -541,7 +572,7 @@ void deleteNode(ListNode node) {
541
572
542
573
[ #268 Missing Number] ( http://blog.csdn.net/daigualu/article/details/69220202 )
543
574
544
- ```
575
+ ``` C#
545
576
public int MissingNumber (int [] nums )
546
577
{
547
578
int xor = 0 , i = 0 ;
@@ -556,7 +587,7 @@ void deleteNode(ListNode node) {
556
587
557
588
[ #507 Perfect Number] ( http://blog.csdn.net/daigualu/article/details/69233798 )
558
589
559
- ```
590
+ ``` C#
560
591
public bool CheckPerfectNumber (int num )
561
592
{
562
593
int sum = 1 ;
@@ -575,7 +606,7 @@ void deleteNode(ListNode node) {
575
606
## Two Pointers
576
607
[ #345 Reverse Vowels of a String] ( http://blog.csdn.net/daigualu/article/details/69257693 )
577
608
578
- ```
609
+ ``` C#
579
610
private List < char > vowels = new List <char > {'a' , 'A' , 'e' , 'E' , 'i' , 'I' , 'o' , 'O' , 'u' , 'U' };
580
611
private char [] chars ;
581
612
@@ -618,7 +649,7 @@ void deleteNode(ListNode node) {
618
649
[ #283 Move Zeroes] ( http://blog.csdn.net/daigualu/article/details/69329038 )
619
650
620
651
621
- ```
652
+ ``` C#
622
653
public void MoveZeroes (int [] nums )
623
654
{
624
655
int i , j = 0 ; // j始终指向非零数
@@ -643,7 +674,7 @@ void deleteNode(ListNode node) {
643
674
644
675
[ #88 Merge Sorted Array] ( http://blog.csdn.net/daigualu/article/details/69367334 )
645
676
646
- ```
677
+ ``` C#
647
678
public void Merge (int [] nums1 , int m , int [] nums2 , int n )
648
679
{
649
680
int i = m - 1 ; // 指向nums1
@@ -661,7 +692,7 @@ void deleteNode(ListNode node) {
661
692
662
693
[ #234Palindrome Linked List] ( http://blog.csdn.net/daigualu/article/details/69388513 )
663
694
664
- ```
695
+ ``` C#
665
696
public bool IsPalindrome (ListNode head )
666
697
{
667
698
int nodeCnt = nodeCount (head );
@@ -701,7 +732,7 @@ void deleteNode(ListNode node) {
701
732
## String
702
733
[ #58 Length of Last Word] ( http://blog.csdn.net/daigualu/article/details/69568460 )
703
734
704
- ```
735
+ ``` C#
705
736
public int LengthOfLastWord1 (string s )
706
737
{
707
738
s = s .Trim (); // 去掉前,后空格
@@ -714,7 +745,7 @@ public int LengthOfLastWord1(string s)
714
745
715
746
[ #20 Valid Parentheses] ( http://blog.csdn.net/daigualu/article/details/69569622 )
716
747
717
- ```
748
+ ``` C#
718
749
public bool IsValid (string s )
719
750
{
720
751
if (string .IsNullOrEmpty (s )) return true ;
@@ -754,7 +785,7 @@ public bool IsValid(string s)
754
785
755
786
[ #520 Detect Capital] ( http://blog.csdn.net/daigualu/article/details/69663210 )
756
787
757
- ```
788
+ ``` C#
758
789
public bool DetectCapitalUse (string word )
759
790
{
760
791
if (string .IsNullOrEmpty (word )) return true ;
@@ -797,7 +828,7 @@ public bool DetectCapitalUse(string word)
797
828
798
829
[ #459 Repeated Substring Pattern] ( http://blog.csdn.net/daigualu/article/details/69663545 )
799
830
800
- ```
831
+ ``` C#
801
832
public bool RepeatedSubstringPattern (string s )
802
833
{
803
834
for (int i = s .Length / 2 ; i > 0 ; i -- )
@@ -826,7 +857,7 @@ public bool DetectCapitalUse(string word)
826
857
827
858
[ #434 Number of Segments in a String] ( http://blog.csdn.net/daigualu/article/details/69664369 )
828
859
829
- ```
860
+ ``` C#
830
861
public int CountSegments (string s )
831
862
{
832
863
if (string .IsNullOrEmpty (s ))
@@ -849,7 +880,7 @@ public int CountSegments(string s)
849
880
850
881
[ #14 Longest Common Prefix] ( http://blog.csdn.net/daigualu/article/details/69665015 )
851
882
852
- ```
883
+ ``` C#
853
884
public string LongestCommonPrefix (string [] strs )
854
885
{
855
886
if (strs == null || strs .Length == 0 )
@@ -895,7 +926,7 @@ public int CountSegments(string s)
895
926
896
927
[ #383 Ransom Note] ( http://blog.csdn.net/daigualu/article/details/69665190 )
897
928
898
- ```
929
+ ``` C#
899
930
public bool CanConstruct (string ransomNote , string magazine )
900
931
{
901
932
if (magazine == null && ransomNote == null ) return true ;
@@ -925,7 +956,7 @@ public bool CanConstruct(string ransomNote, string magazine)
925
956
926
957
## Binary Search
927
958
* [ #367 Valid Perfect Square] ( http://blog.csdn.net/daigualu/article/details/69787644 )
928
- ```
959
+ ``` C#
929
960
public bool IsPerfectSquare (int num )
930
961
{
931
962
int low = 1 , high = num ;
@@ -949,7 +980,7 @@ public bool IsPerfectSquare(int num)
949
980
}
950
981
```
951
982
* [ #167 Two Sum II - Input array is sorted] ( http://blog.csdn.net/daigualu/article/details/69787679 )
952
- ```
983
+ ``` C#
953
984
public int [] TwoSum2 (int [] num , int target )
954
985
{
955
986
// 因为一定存在解,所以不做边界检查
@@ -968,7 +999,7 @@ public bool IsPerfectSquare(int num)
968
999
}
969
1000
```
970
1001
* [ #441 Arranging Coins] ( http://blog.csdn.net/daigualu/article/details/69788500 )
971
- ```
1002
+ ``` C#
972
1003
public int ArrangeCoins (int n )
973
1004
{
974
1005
long low = 1 , high = n ;
@@ -985,7 +1016,7 @@ public bool IsPerfectSquare(int num)
985
1016
```
986
1017
987
1018
* [ #278 First Bad Version] ( http://blog.csdn.net/daigualu/article/details/69802371 )
988
- ```
1019
+ ``` C#
989
1020
public int FirstBadVersion (int n )
990
1021
{
991
1022
long lo = 0 ; // 指向好的版本
@@ -1005,7 +1036,7 @@ public bool IsPerfectSquare(int num)
1005
1036
1006
1037
* [ #349 Intersection of Two Arrays 350. Intersection of Two Arrays II] ( http://blog.csdn.net/daigualu/article/details/69666351 )
1007
1038
1008
- ```
1039
+ ``` C#
1009
1040
// 交集定义,元素可重复
1010
1041
public int [] Intersection (int [] nums1 , int [] nums2 )
1011
1042
{
@@ -1089,7 +1120,7 @@ public bool IsPerfectSquare(int num)
1089
1120
```
1090
1121
* [ #349 Intersection of Two Arrays] ( http://blog.csdn.net/daigualu/article/details/69666198 )
1091
1122
1092
- ```
1123
+ ``` C#
1093
1124
public int [] Intersection (int [] nums1 , int [] nums2 )
1094
1125
{
1095
1126
if (nums1 == null || nums1 .Length == 0 ) return new int [] { };
0 commit comments