Test Development Preparation for Autumn Recruitment Interview 17-Double Pointers for Niu Ke's Questions

After working hard for so many years, looking back, it is almost all long setbacks and sufferings. For most people's life, smooth sailing is only occasional, and frustration, unbearable, anxiety and confusion are the main theme. We take the stage we did not choose, play the script we did not choose. Keep going!

Table of contents

1. Merge two sorted arrays

2. Determine whether it is a palindrome string

3. Reverse the string

4. The longest non-repetitive subarray

5. Merge interval

6. The container with the most water

7. The problem of receiving rainwater


1. Merge two sorted arrays

Topic Link: Merge Two Ordered Arrays

Idea: You can use double pointers for this question. I will store them all in the list and sort them, which is convenient.

Java version:

import java.util.*;
public class Solution {
    public void merge(int A[], int m, int B[], int n) {
        List<Integer> list = new ArrayList<>() ;
        
        for(int i=0; i<m; i++){
            list.add(A[i]) ;
        }
        for(int y : B){
            list.add(y) ;
        }
        Collections.sort(list) ;
        for(int i=0; i<m+n; i++){
            A[i] = list.get(i) ;
        }
        
    }
}

2. Determine whether it is a palindrome string

Topic link: Judging whether it is a palindrome string_Niu Ke Topic_Niu Ke.com

Idea: Just define a double pointer to traverse and compare.

Java version:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param str string字符串 待判断的字符串
     * @return bool布尔型
     */
    public boolean judge (String str) {
        // write code here
        int low = 0, high = str.length() - 1 ;
        while(low<high){
            if(str.charAt(low) != str.charAt(high)){
                return false ;
            }
            low ++ ;
            high -- ;
        }
        return true ;
    }
}

3. Reverse the string

Topic Link: Reverse String_Niuke Topic_Niuke.com

Idea: One line of code is enough to adjust the api to reverse, there is no need for double pointers.
Java version:

import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        return new StringBuilder(str).reverse().toString() ;
    }
}

4. The longest non-repetitive subarray

Topic link: The longest non-repeating subarray_Niuke Topic_Niuke.com

Idea: implement with the help of a queue, a layer of loop, if it does not exist, enter the queue, if it exists, pop it up until it does not contain the element before entering the queue.

Java version:
 

import java.util.*;


public class Solution {
    /**
     *
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public int maxLength (int[] arr) {
        // write code here
       
        int max = 0 ;

        Queue<Integer> queue = new LinkedList<>() ;
        for(int ans : arr){
            if(!queue.contains(ans)){
                queue.add(ans) ;
            }else{
                while(queue.contains(ans)){
                    queue.poll() ;
                }
                queue.add(ans) ;
            }

            max = max <= queue.size() ? queue.size() : max ;
        }
    return max ;
        
    }
}

5. Merge interval

Topic Link: Merge Intervals_Niuke题盘_Niuke.com
Idea: First, sort by intervals. After the sorting is completed, judge the merges that have intersections in turn, and directly add the ones that have no intersections to the set.
Java version:

import java.util.*;
/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public static ArrayList<Interval> merge(ArrayList<Interval> intervals) {
        if (intervals.size() == 0) {
            return new ArrayList<>() ;
        }
        ArrayList<Interval> list = new ArrayList<>() ;
        Collections.sort(intervals, new Comparator<Interval>() {
            public int compare(Interval o1, Interval o2) {
                if (o1.start != o2.start)
                    return o1.start - o2.start;
                else
                    return o1.end - o2.end;
            }
        });
        list.add(intervals.get(0)) ;
        for (int i = 1; i < intervals.size(); i++) {
            int l1 = list.get(list.size() - 1).start ;
            int r1 = list.get(list.size() - 1).end ;
            int l2 = intervals.get(i).start ;
            int r2 = intervals.get(i).end ;
            if (l2 >= l1 && l2 <= r1) {
                list.remove(list.size() - 1) ;
                list.add(new Interval(l1, Math.max(r1, r2))) ;
            } else if (l1 >= l2 && l1 <= r2) {
                list.remove(list.size() - 1) ;
                list.add(new Interval(l2, Math.max(r1, r2))) ;
            } else {
                    list.add(intervals.get(i));
            }
        }
        return list ;
    }
}

6. The container with the most water

Link to the topic: The container with the most water

Idea: traverse from both sides to the middle, enumerate to find the largest container, and move the smaller side to the middle each time.

Java version:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param height int整型一维数组 
     * @return int整型
     */
    public int maxArea (int[] height) {
        // write code here
        if(height.length<=1){
            return 0 ;
        }
        int left = 0, right = height.length - 1 ;
        int max = 0 ;
        while(left<right){
            int tmp = (right - left) * Math.min(height[left], height[right]) ;
            max = Math.max(max, tmp) ;
            if(height[left] >= height[right]){
                right -- ;
            }else{
                left ++ ;
            }
        }
        return max ;
    }
}

7. The problem of receiving rainwater

Topic link: Rainwater problem_Niuke Question Master_Niuke.com

Idea: Accumulate sequentially from both sides to the middle, record the maximum value of the left and right borders, and accumulate the puddle value each time.

Java Edition:

import java.util.*;


public class Solution {
    /**
     * max water
     * @param arr int整型一维数组 the array
     * @return long长整型
     */
    public long maxWater (int[] arr) {
        // write code here
        int left = 0, right = arr.length - 1 ;
        int ml = 0, mr = 0 ;
        int ans = 0 ;
        while(left < right){
            ml = Math.max(ml, arr[left]) ;
            mr = Math.max(mr, arr[right]) ;
            if(ml<mr){
                ans += ml - arr[left++] ;
            }else{
                ans += mr - arr[right--] ;
            }
        }
        return ans ;
    }
}

Guess you like

Origin blog.csdn.net/nuist_NJUPT/article/details/131159414