Leetcode3-4 (longest substring without repeated characters, finding the median of two positive arrays)

Posted by TCovert on Mon, 14 Feb 2022 06:51:08 +0100

1. Longest substring without repeated characters

① Violent solution

According to the topic, convert the string to char type, cycle once, and then use a temp variable of temporary StringBuffer type for judgment. If the current byte does not exist in the temp string, it will be traced. Otherwise, it will be intercepted, and then continue to run for judgment.

The code is as follows:

public int lengthOfLongestSubstring(String s) {
	int max = 0;
	StringBuffer temp = new StringBuffer();
	
	char[] array = s.toCharArray();
	for(char arr:array){
		String str = temp.toString();
		String a = String.valueOf(arr);
		
		if(!str.contains(a)){
			temp.append(a);
		}else{
			//Maximum judgment
			max = str.length()>max?str.length():max;
			//Intercept and add the current value
			temp = new StringBuffer(temp.substring(str.indexOf(a)+1)+a);
		}
	}
	max = temp.length()>max?temp.length():max;
	return max;
}

② Window movement mode

Refer to the official analysis. Use the movement mode to define the left and right pointers. The left and right pointers move together. The length of the string is compared at the moment when the right pointer stops. The storage method uses the non duplicate feature of HashSet.

The code is as follows:

public int lengthOfLongestSubstring(String s) {
	Set<Character> sets = new HashSet<Character>();
	int right = -1;
	int temp = 0;
	
	int len = s.length();
	
	for(int i=0;i<len;i++){
		//Move left to remove data
		if(i!=0){
			sets.remove(s.charAt(i-1));
		}
		//Move right to add data
		while(right+1<len&&!sets.contains(s.charAt(right+1))){
			sets.add(s.charAt(right+1));
			right++;
		}
		//Compare size
		temp = Math.max(temp, right-i+1);
	}
	return temp;
}

Summary: compared with the method I came up with, this method uses two pointers to move at the same time, while my method is equivalent to using only one pointer. The own method is equivalent to using the StringBuffer for storage and comparison. The window method uses the HashSet for storage and comparison. The judgment and comparison hash efficiency is higher. The StringBuffer also needs to do interception and other operations.

2. Find the median of two ordered arrays

There are many methods in this algorithm. The violent solution is to combine two arrays and then use arrays Sort is OK, but this method is inefficient and there are many official solutions. It is not listed one by one. Here is a way to write by yourself, which is easy to understand.

Inspired by the "longest substring without repeated characters", it can be understood as defining two pointers, one for the movement of array 1 and the other for the movement of array 2. Compare the sizes of the two, take the smaller one, and then move the smaller one back.

There are even and odd cases for the purpose of this question. For example, if the odd number is 0,1,2, take 1, that is, the total length / 2; If the even number is 0,1,2,3, take 1,2, i.e. total length / 2-1 + total length / 2. We only need to cycle once, and cycle to the total length / 2-1, and then go down again, and the cycle length only needs to be less than the total length / 2.

The code is as follows:

public double findMedianSortedArrays(int[] nums1, int[] nums2) {
		
	int l1 = nums1.length;
	int l2 = nums2.length;
	int sum = l1+l2;
	
	int n = sum/2;
	
	int a = 0,b = 0;
	int a_n = 0,b_n = 0;//The first array and the second array
	
	for(int i=0;i<n;i++){
		//Get the value of the first array and the value of the second array
		if(a_n<l1){
			a = nums1[a_n];
		}else{
			a = nums2[b_n];
		}
		if(b_n<l2){
			b = nums2[b_n];
		}else{
			b = nums1[a_n];
		}
		//Compare the value sizes of two arrays
		if(a_n<l1&&a<=b){
			a_n++;
		}else{
			b_n++;
		}
	}
	
	int count = sum%2==0?2:1;//Median number
	int temp = 0;
	if(count==2){
		temp = a<=b?a:b;
	}
	
	//Go down again
	if(a_n<l1){
		a = nums1[a_n];
	}else{
		a = nums2[b_n];
	}
	if(b_n<l2){
		b = nums2[b_n];
	}else{
		b = nums1[a_n];
	}
	temp += (a<=b?a:b);
	
	return ((double)temp)/count;
}

Record their own problem-solving process and methods, plus their own opinions, the official analysis is more standardized and clearer.

Topics: Java Algorithm leetcode