Data structure basis + happy number

Keywords: Mobile

Mobile zero

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int ptr1 = 0,ptr2 = 1;
        if(nums.size()<=1){
            return;
        }
        while(ptr2<nums.size()){
            while(nums[ptr2]==0){   //If it is 0, move right to a non-0 position, but not beyond the boundary       
                if(ptr2>=nums.size()-1)
                    break;
                ptr2++;
            }
            if(nums[ptr1]==0&&nums[ptr2]!=0){   //Exchange if 0
                nums[ptr1] = nums[ptr2];
                nums[ptr2] = 0;
            }
            ptr1++;
            if(ptr1>=ptr2)	//The only case where ptr1 exceeds ptr2 is that all the preceding numbers are non-zero
                ptr2 = ptr1 + 1;
        }
    }
};

Although it is poorly written, it is 4ms and the logic is clear.

Linked list

Linked list is that the storage of each node is determined dynamically, and each node has a pointer to the next node. Because the storage is dynamic and discontinuous, we can get the excellent effect of O (1) when inserting and deleting nodes.

Necessary elements of linked list: node, pointer (there can be two, namely, two-way linked list)

Some important points:

Check looping: use speed pointer

The first intersection of two linked lists: search from the end to the end to find the bifurcation point

The following is a flip list. It's very simple, but you must pay attention to the direction

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==NULL)
            return NULL;
        ListNode* myhead = head->next;  //Start directly from the second jump to avoid a dead cycle
        ListNode* temp = head;  //Always point to the previous jump, the original head position is always the previous jump!!!
        while(myhead!=NULL){
            temp->next = myhead->next; //The previous jump points directly to the next
            myhead-> next = head; //Put the node in the head
            head = myhead;  //Head change address
            myhead = temp->next; //Change to the next jump before
        }
        return head;
    }
};

The following is the code of the rotating list, the key is cutting:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head==NULL||k==0||head->next==NULL)
            return head;
        int size = 1;//Length of linked list
        ListNode* end = head;//Reciprocal
        while(end->next!=NULL){
            size+=1;
            end=end->next;
        }
        k = size-k%size; //Confirm the position of cutting site
        ListNode* end_k = head;//Countdown k
        int i = 0;
        while(++i<k)
            end_k = end_k->next;
        end->next = head;
        head = end_k->next;
        end_k->next = NULL;
        return head;
    }
};

HASH

Hash is a kind of mapping relationship. We can use hash to shrink a large range of data into a bucket of a given range, map the content into key values, and find the target in O (1) time. Simply put, the visit array is the simplest one-to-one hash mapping.

class MyHashSet {
private:
    bool a[1000100];
public:
    /** Initialize your data structure here. */
    MyHashSet() {
        for(int i=0;i<1000100;i++)
            a[i] = false;
    }
    
    void add(int key) {
        a[key] = true;
    }
    
    void remove(int key) {
        a[key] = false;
    }
    
    /** Returns true if this set contains the specified element */
    bool contains(int key) {
        return a[key];
    }
};

The above is the simplest, but it will not be used in practice....

summary

c++ hash library call method:

unordered_set<int> hashset;

hashset.insert(2);

hashset.erase(2);

hashset.count(2)

Vector emptying method vector < int > (). Swap (nums);

Published 23 original articles, won praise 0, visited 554
Private letter follow

Posted by classifieds100 on Sun, 15 Mar 2020 03:37:41 -0700