Double pointer traversal (solve the problem of sorted arrays)

1. Number of squared numbers (sorted array)

Problem Description

Sort the array, after squaring, how many different numbers are in the array (the same count as one).

ideas

Here is the sorted array. Since there are repetitions, there must be negative numbers, such as 0 and 1. After squaring, the two ends are large and the middle is small. You can use the head and tail pointers to scan to the middle together, remove duplicate elements during scanning, and use a sum to record how many different numbers there are.

code

	func sortSquare(nums []int) int {
    
    
	var sum int
	left, right := 0, len(nums) - 1
	for left <= right {
    
    
		if nums[left] + nums[right] == 0 {
    
     	// 如果nums[left] == -nums[right]
			sum++							// 左指针右移并去重
			temp := nums[left]				// 右指针左移并去重
			// 左指针右移,去重
			for left <= right && nums[left] == temp {
    
    
				left++
			}
			// 右指针左移,去重
			for left <= right && nums[right] == -temp {
    
    
				right--
			}
		} else if nums[left] + nums[right] < 0 {
    
     // 如果nums[left]过小,左指针右移并去重
			sum++
			temp := nums[left]
			for left <= right && nums[left] == temp {
    
    
				left++
			}
		} else {
    
     // 如果nums[right]过大,右指针左移并去重
			sum++
			temp := nums[right]
			for left <= right && nums[right] == temp {
    
    
				right--
			}
		}
	}
	return sum
}

2. Find the number of unique arrays

1. Problem description

A data is incremented first and then decremented to find the number of non-repeated arrays, such as [1, 3, 9, 1], the result is 3, no extra space can be used, and the complexity is o(n)

2. Ideas

The double pointers approach from both ends to the middle, and duplicate elements are removed during scanning.

3. Code

func findNotRepeatNums(nums []int) int {
    
    
	left, right := 0, len(nums) - 1
	var sum int
	for left <= right {
    
    
		if nums[left] == nums[right] {
    
    
			sum++
			temp := nums[left]
			for left <= right && nums[left] == temp {
    
    
				left++
			}
			for left <= right && nums[right] == temp  {
    
    
				right--
			}
		} else if nums[left] < nums[right] {
    
    
			sum++
			temp := nums[left]
			for left <= right && nums[left] == temp {
    
    
				left++
			}
		} else {
    
    
			sum++
			temp := nums[right]
			for left <= right && nums[right] == temp {
    
    
				right--
			}
		}
	}
	return sum
}

3. Sum of two numbers II extension (find all pairs of numbers whose sum is k)

1. Problem description

Increment the array to find all pairs whose sum is k

Note: This question is similar to the sum of two numbers II leetcode167, except that leetcode167 only needs to find a set of pairs, and it should return all pairs

2. Ideas

Double pointer traversal: start traversing with two pointers at the head and tail respectively, when the sum of the two numbers is greater than k, the right pointer moves forward, and when it is less than k, the left pointer moves backward

3. Code

func sumk(nums []int, k int) [][2]int {
    
    
	left, right := 0, len(nums) - 1
	var res [][2]int

	for left <= right {
    
    
		if nums[left] + nums[right] == k {
    
    
			res = append(res,[2]int{
    
    nums[left], nums[right]} )
			temp1 := nums[left]
			for left <= right && nums[left] == temp1{
    
    
				left++
			}
			temp2 := nums[right]
			for left <= right && nums[right] == temp2{
    
    
				right--
			}
		} else if nums[left] + nums[right] < k {
    
    
			temp := nums[left]
			for left <= right && nums[left] == temp{
    
    
				left++
			}
		} else {
    
    
			temp := nums[right]
			for left <= right && nums[right] == temp{
    
    
				right--
			}
		}
	}
	return res
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324156596&siteId=291194637