@@ -219,6 +219,54 @@ else if (target <= nums[right]) {
219
219
}
220
220
}
221
221
222
+ /**
223
+ * longest substring without repeating characters
224
+ * */
225
+
226
+ public static int lengthOfLongestSubstring (String s ) {
227
+ // a b c a b c b b
228
+ // 0 1 2 3 4 5 6 7
229
+ if (s .length () <= 1 )
230
+ return s .length ();
231
+ Map <Character , Integer > seenCharacters = new HashMap <>();
232
+ int left = 0 , longest = 0 ;
233
+ for (int right = 0 ; right < s .length (); right ++) {
234
+ char currentChar = s .charAt (right );
235
+ if (null != seenCharacters .get (currentChar ) && seenCharacters .get (currentChar ) >= left ) {
236
+ left = seenCharacters .get (currentChar ) + 1 ;
237
+ }
238
+ longest = Math .max (longest , right -left +1 );
239
+ seenCharacters .put (currentChar , right );
240
+ }
241
+
242
+ return longest ;
243
+
244
+ }
245
+
246
+ /**
247
+ * Container with largest amount of water
248
+ * concept - 2 shifting pointers
249
+ */
250
+ public static int maxArea (int [] height ) {
251
+
252
+ if (height .length <= 1 ) return 0 ;
253
+
254
+ int left = 0 ;
255
+ int right = height .length - 1 ;
256
+ int maxArea = 0 ;
257
+
258
+ while (left <= right ) {
259
+ int leftNum = height [left ];
260
+ int rightNum = height [right ];
261
+
262
+ maxArea = Math .max (maxArea , Math .min (leftNum , rightNum ) * (right -left ));
263
+ if (leftNum < rightNum ) left ++;
264
+ else right --;
265
+ }
266
+
267
+ return maxArea ;
268
+ }
269
+
222
270
public static void main (String [] args ) {
223
271
int index = search (new int []{2 ,2 ,2 ,3 ,4 ,2 }, 3 , 0 , 5 );
224
272
System .out .println ("index of 3 is " + index );
0 commit comments