Data base structure (C) + happy number

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){   //是0则右移到非0位置,但不能超过边界       
                if(ptr2>=nums.size()-1)
                    break;
                ptr2++;
            }
            if(nums[ptr1]==0&&nums[ptr2]!=0){   //是0则交换
                nums[ptr1] = nums[ptr2];
                nums[ptr2] = 0;
            }
            ptr1++;
            if(ptr1>=ptr2)	//ptr1超过ptr2唯一的情况就是前面全是非0数
                ptr2 = ptr1 + 1;
        }
    }
};

Although is poorly, but is 4ms, logic is also considered to be very clear.

List

The list is stored in each node is dynamically determined, and each node has a pointer to a node to the next, since the storage is non-continuous dynamic, so we can take at the time of insertion and deletion node O (1) excellent effect.

Chain essential elements: a node pointer (may be two, i.e. doubly linked list)

Some important points:

Check ring: using the speed pointers

The first point of intersection of two lists: from the tail to the head search to find points of disagreement

The following is a list flipped, very simple, but must pay attention to a good point

/**
 * 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;  //直接从第二跳开始,避免死循环
        ListNode* temp = head;  //永远指向前一跳,原先的head位置永远都是前一跳!!!
        while(myhead!=NULL){
            temp->next = myhead->next; //前一跳直接指向下一跳
            myhead-> next = head; //将该节点放在头部
            head = myhead;  //头部变换地址
            myhead = temp->next; //换成之前的下一跳
        }
        return head;
    }
};

The following is a list of rotation of the code, the key is cut:

/**
 * 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;//链表长度
        ListNode* end = head;//倒数
        while(end->next!=NULL){
            size+=1;
            end=end->next;
        }
        k = size-k%size; //确认切割位点在第几位
        ListNode* end_k = head;//倒数第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 mapping relationship, we can hash data by a wide range of the reduction in the scope of the development of the bucket can also be mapped to the content key, to find the target in the O (1) time. In simple terms, visit the array is the most simple one to one mapping of hash.

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 the reality does not use it. . . .

to sum up

Call the method c ++ hash library:

unordered_set<int> hashset;

hashset.insert(2);

hashset.erase(2);

hashset.count(2)

Empty vector method of vector <int> () swap (nums).;

Published 23 original articles · won praise 0 · Views 556

Guess you like

Origin blog.csdn.net/CSDN_Yong/article/details/104882553