[Top 101 must-haves for Niuke interviews] Day4.BM15 Delete duplicate elements in the ordered linked list-I and BM17 Binary search-I

About the author: Hello everyone, my name is Weiyang;

Blog homepage: Weiyang.303

Column series: TOP101 must-dos for Niuke interviews

Quote of the day: There is only one chance in a person's life to make a difference, and that is now! ! ! ! !

Article directory

Preface

1. Delete duplicate elements in the ordered linked list-I

Question description

Problem solving analysis

2. Binary search-I

Question description

Problem solving analysis

Summarize



Preface

Today is our 4th day of the TOP101 must-do list for Niuke interviews. It is relatively simple, so be sure to master it clearly; see you every day! ! !


1. Delete duplicate elements in the ordered linked list-I

Question description

describe

Delete duplicate elements in the given linked list (elements in the linked list are ordered from small to large) so that all elements in the linked list appear only once


For example:
the given linked list is 1→1→2, and 1→2 is returned. The
given linked list is 1→1→2→3→3, and 1→2→3 is returned.


Data range: The length of the linked list satisfies 0≤n≤100, and the value of any node in the linked list satisfies ∣val∣≤100;

Advanced: space complexity O(1), time complexity O(n)


Example 1:


Example 2:


Problem solving analysis:

Problem-solving ideas:

Since we want to delete duplicate elements in this question, we choose to use the traversal deletion method;

Idea:

Since only one consecutive identical element is left, which one is best for us to keep? Of course it is the first element encountered!

Code:

if(cur.val == cur.next.val) 
    cur.next = cur.next.next;

Because the first element is directly connected to the previous linked list node, you don’t need to worry about it. You only need to skip the repeated elements and connect the first non-repeating element. Connect the following elements in the linked list. It is more convenient than connecting the previous elements, because it cannot be accessed in reverse order.


Problem solving steps:

  • Step 1: Determine whether the linked list is an empty linked list. The empty linked list will be returned directly without processing.

  • Step 2: Use a pointer to traverse the linked list. If the value of the current node of the pointer is the same as the next node, we skip the next node, and the current node is directly connected to the next node.


  • Step 3: If the current node has different values ​​from the next node, continue traversing backwards.


  • Step 4: During the loop, two node values ​​are used each time, and it is necessary to check whether two consecutive nodes are empty.

Code writing:


2. Binary search-I

Topic description:

describe:

Please implement binary search for ascending arrays without duplicate numbers

Given an integer array nums with elements in ascending order and without repeated numbers and a target value target, write a function to search for target in nums. If the target value exists, return the subscript (the subscript starts from 0), otherwise return -1;


Data range: 0≤len(nums)≤2×10^5, any value in the array satisfies ∣val∣≤10^9;

Advanced: time complexity O(logn), space complexity O(1);


Example 1:


Example 2:


Example 3:

Problem solving analysis:

Problem-solving ideas:

Main information about the topic:

  • Given an integer array nums with elements in ascending order without repeated numbers and a target value target
  • Find the subscript of the target value
  • Returns -1 if not found

This question is a binary search type, so we use the binary method to solve it;


Knowledge point: divide and conquer

Divide and conquer means "divide and conquer";

"Dividing" refers to dividing a large and complex problem into multiple sub-problems of the same nature but smaller scale. The sub-problems continue to be divided in this way until the problem can be easily solved;

"Treat" refers to dealing with sub-problems individually. The solutions to the sub-problems after divide and conquer need to be merged to obtain the solution to the original problem, so the entire divide-and-conquer process is often implemented using recursion.


Idea:

Originally, we could traverse the array and search directly, checking whether the current element is the value we are looking for each time.

for(int i = 0; i < nums.length; i++)
    if(nums[i] == target)
        return i;

But in this way we do not fully utilize this ordered array. Let's think about it. If the target value is relatively small, it must be in the first half of the range. If the target value is relatively large, it must be in the second half of the range. How to evaluate the size? We can use the midpoint value as a benchmark to divide the entire array into two intervals. By comparing the target value with the midpoint value, we can know which interval it will be in. This is the thinking of divide and conquer.


Problem solving steps:

  • Step 1: Starting from the beginning and end of the array, take the midpoint value each time.

  • Step 2: If the midpoint value is equal to the target, it is found, and the subscript can be returned. If the midpoint value is greater than the target, it means that everything after the midpoint is greater than the target, so the target is in the left half of the midpoint. If the midpoint value is less than the target, then on the contrary.


  • Step 3: Enter the corresponding interval based on comparison until the left and right ends of the interval meet, which means it is not found.


Illustration description:

Code writing:

Summarize

Guess you like

Origin blog.csdn.net/qq_64861334/article/details/132767511