@@ -2488,10 +2488,6 @@ Console.WriteLine(charArr[0] == '\u0000'); // True
24882488int [] intArr2 = [9 , 3 , 2 , 100 , 4 ];
24892489Console .WriteLine (string .Join (" , " , intArr2 )); // 9, 3, 2, 100, 4
24902490
2491- int [] intArr3 = Enumerable .Repeat (int .MaxValue , 5 ).ToArray ();
2492- Console .WriteLine (string .Join (" , " , intArr3 )); // 2147483647, 2147483647, 2147483647, 2147483647, 2147483647
2493- Console .WriteLine (" -----" );
2494-
24952491// arr[idx] = val (set) in O(1)
24962492intArr [2 ] = 100 ;
24972493Console .WriteLine (string .Join (" , " , intArr )); // 0, 0, 100, 0, 0
@@ -2771,29 +2767,6 @@ Console.WriteLine(minChar); // A
27712767Console .WriteLine (maxChar ); // Z
27722768Console .WriteLine (" -----" );
27732769
2774- // list.BinarySearch(val) in O(logn)
2775- // if found, res is non-negative:
2776- // if have duplicate target vals, not guarantee which idx of them will be returned
2777- // try to get first/last one of them will cost additional O(k)
2778- // if not found, res is negative:
2779- // we can use complement num to get insertion point
2780- List < int > list5 = [20 , 49 , 88 , 120 ];
2781- int foundIdx = list5 .BinarySearch (20 );
2782- Console .WriteLine (foundIdx ); // 0
2783-
2784- int foundIdx2 = list5 .BinarySearch (49 );
2785- Console .WriteLine (foundIdx2 ); // 1
2786-
2787- int nonFoundIdx = list5 .BinarySearch (8 );
2788- Console .WriteLine (~ nonFoundIdx ); // 0
2789-
2790- int nonFoundIdx2 = list5 .BinarySearch (50 );
2791- Console .WriteLine (~ nonFoundIdx2 ); // 2
2792-
2793- int nonFoundIdx3 = list5 .BinarySearch (130 );
2794- Console .WriteLine (~ nonFoundIdx3 ); // 4
2795- Console .WriteLine (" -----" );
2796-
27972770```
27982771
27992772# Dictionary
@@ -2893,78 +2866,7 @@ Key: B, Value: 500
28932866Key: C, Value: 10
28942867*/
28952868
2896- // dict.TryGetValue(key, out value) in O(1) (avg)
2897- Dictionary < string , int > map3 = [];
2898- if (map3 .TryGetValue (" A" , out int valFromTryGet ))
2899- {
2900- map3 [" A" ] = valFromTryGet + 1 ;
2901- }
2902- else
2903- {
2904- map3 [" A" ] = 1 ;
2905- }
2906- Console .WriteLine (string .Join (" , " , map3 )); // [A, 1]
2907- if (map3 .TryGetValue (" A" , out int valFromTryGet2 ))
2908- {
2909- map3 [" A" ] = valFromTryGet2 + 1 ;
2910- }
2911- else
2912- {
2913- map3 [" A" ] = 1 ;
2914- }
2915- if (map3 .TryGetValue (" B" , out int valFromTryGet3 ))
2916- {
2917- map3 [" B" ] = valFromTryGet3 + 1 ;
2918- }
2919- else
2920- {
2921- map3 [" B" ] = 1 ;
2922- }
2923- Console .WriteLine (string .Join (" , " , map3 )); // [A, 2], [B, 1]
2924-
2925- Dictionary < string , List < int >> map4 = [];
2926- if (map4 .TryGetValue (" A" , out List <int >? valFromTryGet4 ))
2927- {
2928- valFromTryGet4 .Add (1 );
2929- }
2930- else
2931- {
2932- map4 [" A" ] = [1 ];
2933- }
2934- foreach (var pair in map4 )
2935- {
2936- Console .WriteLine ($" {pair .Key }: [{string .Join (" , " , pair .Value )}]" );
2937- }
2938- /*
2939- A: [1]
2940- */
2941- if (map4 .TryGetValue (" A" , out List <int >? valFromTryGet5 ))
2942- {
2943- valFromTryGet5 .Add (1 );
2944- }
2945- else
2946- {
2947- map4 [" A" ] = [1 ];
2948- }
2949- if (map4 .TryGetValue (" B" , out List <int >? valFromTryGet6 ))
2950- {
2951- valFromTryGet6 .Add (2 );
2952- }
2953- else
2954- {
2955- map4 [" B" ] = [2 ];
2956- }
2957- foreach (var pair in map4 )
2958- {
2959- Console .WriteLine ($" {pair .Key }: [{string .Join (" , " , pair .Value )}]" );
2960- }
2961- Console .WriteLine (" -----" );
2962- /*
2963- A: [1, 1]
2964- B: [2]
2965- */
2966-
2967- // dict.Min() or dict.Max() in O(n)
2869+ // dict.Min() or dict.Max() or dict.MaxBy() in O(n)
29682870string ? minKey = map2 .Keys .Min ();
29692871Console .WriteLine (minKey ); // A
29702872int maxValue = map2 .Values .Max ();
@@ -3157,7 +3059,7 @@ Console.WriteLine(val); // 100
31573059Console .WriteLine (string .Join (" , " , queue )); // 30, 66
31583060Console .WriteLine (" -----" );
31593061
3160- // queue.Peek() and in O(1)
3062+ // queue.Peek() in O(1)
31613063int oldest = queue .Peek ();
31623064Console .WriteLine (oldest ); // 30
31633065Console .WriteLine (" -----" );
@@ -3169,16 +3071,50 @@ Console.WriteLine("-----");
31693071
31703072```
31713073
3074+ # LinkedList (use as Deque)
3075+ ``` csharp
3076+ // LinkedList<T>
3077+
3078+ // deque init in O(1)
3079+ LinkedList < int > deque = [];
3080+ Console .WriteLine (" -----" );
3081+
3082+ // deque.AddLast(val) or deque.AddFirst(val) in O(1)
3083+ deque .AddLast (100 );
3084+ deque .AddLast (30 );
3085+ deque .AddLast (66 );
3086+ Console .WriteLine (string .Join (" , " , deque )); // 100, 30, 66
3087+ Console .WriteLine (" -----" );
3088+
3089+ // deque.RemoveFirst() or deque.RemoveLast() in O(1)
3090+ deque .RemoveFirst ();
3091+ Console .WriteLine (string .Join (" , " , deque )); // 30, 66
3092+ Console .WriteLine (" -----" );
3093+
3094+ // deque.First.Value or deque.Last.Value in O(1)
3095+ int oldest = deque .First .Value ;
3096+ int newest = deque .Last .Value ;
3097+ Console .WriteLine (oldest ); // 30
3098+ Console .WriteLine (newest ); // 66
3099+ Console .WriteLine (" -----" );
3100+
3101+ // deque.Count in O(1)
3102+ int count = deque .Count ;
3103+ Console .WriteLine (count ); // 2
3104+ Console .WriteLine (" -----" );
3105+
3106+ ```
3107+
31723108# string
31733109``` csharp
31743110// string
31753111
31763112// string: immutable
31773113// StringBuilder: mutable
31783114
3179- // str.Length in O(1)
3180- using System .Text ;
3115+ using System .Text ; // For StringBuilder
31813116
3117+ // str.Length in O(1)
31823118string s = " Apple" ;
31833119int len = s .Length ;
31843120Console .WriteLine (len ); // 5
0 commit comments