LeetCode日记20200201

LeetCode日记2020.2.1


继续链表

817 链表组件(mid)

线性扫描,利用set减少查找时间。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int numComponents(ListNode* head, vector<int>& G) {
        set<int> gSet;
        for(const auto& g:G)
            gSet.insert(g);

        int cnt = 0;
        bool flag = false;
        while(head!=NULL)
        {
            if(gSet.find(head->val)!=gSet.end())
            {
                if(!flag)
                {
                    flag = true;
                    ++cnt;
                }
            }
            else
                flag = false;
            
            head = head -> next;
        }
        return cnt;
    }
};

86 分隔链表(mid)

注意初始化迭代过程中小于x和大于等于x的两个子链表的末端指针,否则迭代无法正确进行。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode* dummyH = new ListNode(-1);
        dummyH->next = head;

        //初始化两个子链表的末端指针
        ListNode* bigEnd = dummyH;
        while(bigEnd->next!=NULL&&bigEnd->next->val < x)
            bigEnd=bigEnd->next;

        ListNode* smallEnd = bigEnd;
        ListNode* smallEndNext = bigEnd->next;

        while(bigEnd->next != NULL)
        {
            if(bigEnd->next->val < x)
            {
                smallEnd->next = bigEnd->next;
                bigEnd->next = bigEnd->next->next;
                smallEnd->next->next = smallEndNext;
                smallEnd = smallEnd -> next;
            }
            else
                bigEnd = bigEnd->next;
        }
        return dummyH->next;
    }
};

143 重排链表(mid)

尝试利用递归解法解决以锻炼思维。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        if(head==NULL)
            return;

        fast = head;
        slow = head;
        recursion(head);
        End->next = NULL;
    }

    void recursion(ListNode* head)
    {
        if(fast!=NULL&&fast->next!=NULL)
        {
            fast = fast->next->next;
            slow = slow->next;
            recursion(head->next);
            auto temp = head->next;
            head->next = slow;
            slow=slow->next;
            head->next->next = temp;
        }
        else
        {
            End = slow;
            if(fast!=NULL)
                slow=slow->next;
        }
    }

    ListNode *fast, *slow, *End;
};

445 两数相加2(mid)

可以用栈避免反转链表,也可以在一定程度上代替链表递归,编程更简单不容易出错。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        stack<int> s1, s2;
        while(l1!=NULL)
        {
            s1.push(l1->val);
            l1 = l1->next;
        }
        while(l2!=NULL)
        {
            s2.push(l2->val);
            l2=l2->next;
        }
        int carry = 0;
        ListNode* dummyH = new ListNode(-1);
        while(!s1.empty()&&!s2.empty())
        {
            int sum = s1.top()+s2.top()+carry;
            s1.pop();
            s2.pop();
            carry = sum/10;
            sum%=10;
            auto temp = dummyH->next;
            dummyH->next = new ListNode(sum);
            dummyH->next->next = temp;
        }
        stack<int>& s = s1.empty()?s2:s1;
        while(!s.empty())
        {
            int sum=s.top()+carry;
            s.pop();
            carry = sum/10;
            sum%=10;
            auto temp = dummyH->next;
            dummyH->next = new ListNode(sum);
            dummyH->next->next = temp;
        }
        if(carry!=0)
        {
            auto temp = dummyH->next;
            dummyH->next = new ListNode(carry);
            dummyH->next->next = temp;
        }
        return dummyH->next;
    }

};

725 分割链表(mid)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
        int total =0;
        auto tempRoot = root;
        while(tempRoot!=NULL)
        {
            ++total;
            tempRoot=tempRoot->next;
        }
        int avr = total / k;
        vector<int> sizes(k,avr);
        int rem = total%k;
        if(rem!=0)
        {
            for(int i=0;i<rem;++i)
                ++sizes[i];
        }
        vector<ListNode*> heads;
        for(int i=0;i<k;++i)
        {
            heads.push_back(root);
            ListNode* End=root;
            for(int j=0;j<sizes[i];++j)
            {
                if(j==sizes[i]-1)
                    End = root;

                root = root->next;
            }
            if(End!=NULL)
                End->next = NULL;
        }
        return heads;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值