"Likou Brush Question Notes" product of arrays other than itself && copy linked list with random pointers

content

1. The focus of this chapter

2. Product of arrays other than themselves

3. Copy the linked list with random pointers

Fourth, the last


There is no one in ancient times, the lonely mark Ling Yundao is a friend, the sword is my life, and it is both mad and chivalrous and extraordinary.

 

1. The focus of this chapter

Problem 1: Product of arrays other than themselves

Question 2: Copy a linked list with random pointers

2. Product of arrays other than themselves

Difficulty: Moderate

Given an array of integers nums, return the array answer, where answer[i] is equal to the product of the elements of nums except nums[i].

The title data ensures that the product of all prefix elements and suffixes of any element in the array nums is within the range of 32-bit integers.

Please do not use division and do this in O(n) time complexity.

Source: LeetCode

Example 1:

Input: nums =[1,2,3,4]

output:[24,12,8,6]

Example 2:

Input: nums = [-1,1,0,-3,3]

Output: [0,0,9,0,0]

Ideas:

It could have been solved by division, that is, multiplying all numbers to get the total product, and the value of the first new array is the total product divided by the value of the original array. But this takes into account the presence of 0 in the original array.

Since the title requires not to use division, then we can only change the way of thinking.

Multiply the left product by the right product:

The product of an array other than itself, that is, the left product of the element multiplied by the right product. Put the left product of each element into the opened new array first, then multiply the right product of each element by the value of the new array and put the result into the new array, then the elements of the new array are the elements of the original array The product of the left product and the right product, that is, the product of the arrays other than themselves.

The left product of the first element is equal to 1

The left product of the second element is equal to the left product of the first element times the first element.

The left product of the third element is equal to the left product of the second element times the second element.

And so on. . . . . .

So we can find the left product method of each element:

int left=1;
for(i=0;i<numsSize;i++)
    {
        if(i>0)
            left=left*nums[i-1];
        answer[i]=left;
    }

The right product of the last element is equal to 1

The right product of the second-to-last element is equal to the right-product of the first-to-last element multiplied by the first-to-last element

The right product of the third-to-last element is equal to the right-product of the second-to-last element multiplied by the second-to-last element.

And so on. . . . . .

So we can get the right product method for each element:

int right=1; 
for(i=numsSize-1;i>=0;i--)
    {
        if(i<numsSize-1)
            right=right*nums[i+1];
        answer[i]*=right;
    }

Reference Code:

int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
    int* answer=(int*)malloc(sizeof(int)*numsSize);
    int i=0;
    int left=1;
    int right=1;
    for(i=0;i<numsSize;i++)
    {
        if(i>0)
            left=left*nums[i-1];
        answer[i]=left;
    }
    for(i=numsSize-1;i>=0;i--)
    {
        if(i<numsSize-1)
            right=right*nums[i+1];
        answer[i]*=right;
    }
    *returnSize=numsSize;
    return answer;
}

Summary of ideas:

Two steps:

first step:

Open up a new array and find the left product of each element of the original array (if it is the first array element, then its left product is one)

And put their left product correspondingly into the new array.

Step 2:

Find the right product of each element of the original array, multiply it by the element corresponding to the new array, and put the multiplied result into the corresponding element of the new array.

3. Copy the linked list with random pointers

Difficulty: Moderate

Question: Give you a linked list of length n, each node contains an additional random pointer random, which can point to any node in the linked list or an empty node.

Constructs a deep copy of this linked list. A deep copy should consist of exactly n new nodes, where the value of each new node is set to the value of its corresponding original node. The next pointer and random pointer of the new node should also point to the new node in the copied linked list, so that these pointers in the original linked list and the copied linked list can represent the same linked list state. None of the pointers in the copied linked list should point to the nodes in the original linked list.

For example, if there are two nodes X and Y in the original linked list, where X.random --> Y . Then the corresponding two nodes x and y in the replication linked list also have x.random --> y . Returns the head node of the replicated linked list.

A linked list in input/output is represented by a linked list of n nodes. Each node is represented by a [val,random_index]:

val: An integer representing Node.val.
random_index: The index of the node pointed to by the random pointer (ranging from 0 to n-1); null if it does not point to any node.
Your code only accepts the head node of the original linked list as an incoming parameter.

Source: LeetCode

Title:

Taking the linked list as an example, we need to copy an exact copy of the linked list, similar to the deep copy in C++.

To put it bluntly, the original node points to an original node, then the random of the copy node must point to the copy node of an original node.

The next of the original 7 points to the original 13, that is, the copied 7 points to the copied 13, and the random of the original 7 points to NULL, that is, the random of the copied 7 points to NULL. The random of the original 13 points to the original 7, that is, the random of the copy 13 points to the copy 7.

Copy 7, 13, 11, 10, 1, and NULL to make it easy to connect with next. But how do they point randomly?

Diagram:

Connect the new linked list with the copied linked list: 

We only have the head pointer of the original linked list. We know that the random of the original 7 points to NULL, so it is easy to copy the random of 7 to point to NULL. The random of the original 13 points to 7, and we only know the address space that random points to.

That is, we only know 0x11 31 24 31, and this space is unrelated to the newly copied space, which does not help us copy 13 which address of the copy linked list should point to.

All the better way is to link the original linked list with the new copy linked list.

Diagram:

Reference Code:

    Node* cur=head;
    //在原节点后拷贝节点并连接起来。
    while(cur)
    {
        Node* newnode=(Node*)malloc(sizeof(Node));
        newnode->val=cur->val;
        newnode->next=cur->next;
        cur->next=newnode;
        cur=newnode->next;
    }

The random of the allocated copy linked list points to:

When we know that the random of the original 7 points to NULL, then the copy 7 also points to NULL.

Knowing that the random of the original 13 points to the original 7, then the random of the copy 13 should point to the copy 7, that is, to the next of the original 7.

Reference Code:

    //给拷贝节点random指针分配指向。
    while(cur)
    {
        copynode=cur->next;
        if(cur->random==NULL)
        {
            copynode->random=NULL;
        }
        else
        {
            copynode->random=cur->random->next;
        }
        cur=cur->next->next;
    }

Unwrap the original linked list and copy the linked list:

We insert the head of the copy linked list into another linked list by inserting the head of the copy linked list.

Reference Code:

 cur=head;
    Node* newhead=NULL;
    Node* newtail=NULL;
    while(cur)
    {
        copynode=cur->next;
        if(newhead==NULL)
        {
            newhead=newtail=copynode;
        }
        else
        {
            newtail->next=copynode;
            newtail=copynode;
        }
        cur->next=copynode->next;
        cur=copynode->next;
    }

Complete code reference:

typedef struct Node Node;
struct Node* copyRandomList(struct Node* head) 
{
    Node* cur=head;
    //在原节点后拷贝节点并连接起来。
    while(cur)
    {
        Node* newnode=(Node*)malloc(sizeof(Node));
        newnode->val=cur->val;
        newnode->next=cur->next;
        cur->next=newnode;
        cur=newnode->next;
    }
    cur=head;
    Node* copynode=NULL;
    //给拷贝节点random指针分配指向。
    while(cur)
    {
        copynode=cur->next;
        if(cur->random==NULL)
        {
            copynode->random=NULL;
        }
        else
        {
            copynode->random=cur->random->next;
        }
        cur=cur->next->next;
    }
    //断开连接
    cur=head;
    Node* newhead=NULL;
    Node* newtail=NULL;
    while(cur)
    {
        copynode=cur->next;
        if(newhead==NULL)
        {
            newhead=newtail=copynode;
        }
        else
        {
            newtail->next=copynode;
            newtail=copynode;
        }
        cur->next=copynode->next;
        cur=copynode->next;
    }
    return newhead;
}

Summary of ideas:

This problem is solved in three steps:

The first step to connect:

Connect the original linked list and the copied linked list in turn, and connect each copy node to the back of the corresponding original node.

The second step allocates the random pointer of the copied linked list node:

If the random pointer of the original linked list node is NULL, the random pointer of the corresponding copy node is also empty. If the random pointer of the original linked list node is another non-empty node of the original linked list, then the copy node random of the corresponding copy points to another original linked list. The copy node of the non-empty node in the linked list, that is, another non-empty node in the original linked list -> next.

The third step is to unpack the original linked list and the copied linked list:

Restore the original linked list and connect the copy node.

Fourth, the last

I hope this article can help you, if you have any questions, please comment or private message me, I will reply in time, thank you!

Every day without dancing is a disappointment to life!

 

Guess you like

Origin blog.csdn.net/m0_62171658/article/details/123704456