zoukankan      html  css  js  c++  java
  • 三色旗问题

    题目:只有0,1,2组成的数组,将其按0..0..1...1..2...2排序。(leetcode75)

    分析:

    非常有意思的一道题目。由 Dijkstra 提出

    快速排序其实就是不断地调用partition分区函数,快速排序只需要分2个区,用了两个指针。

    这里3个分区就需要三个指针。

    主要看中间指针p2,

    若p2=0,交换p1,p2,两者加加;

    若p2=1,p2++;

    若p2=2,交换p2,p3,p3--.

        void sortColors(vector<int>& nums) {
            int p1 = 0, p2 = 0, p3 = nums.size()-1;
            while(p2 <= p3)  // 3 1 2
            {
                if(nums[p2] == 0)
                {
                    swap(nums[p1], nums[p2]);
                    p1++;
                    p2++;
                }
                else if(nums[p2] == 1)
                {
                    p2++;
                }
                else
                {
                    while(p3 > p2 && nums[p3] == 2)  p3--; //可写可不写
                    swap(nums[p2], nums[p3]);
                    p3--;
                }
            }
            for(int x : nums)  printf("%d ", x);
        }

    题目:有序数组去重(leetcode26)

    用一个指针互异元素的最后一个,另外一个指针遍历

        int removeDuplicates(vector<int>& nums) {
            int k = 0;
            for(int i = 1;i < nums.size();i++)
            {
                if(nums[i] != nums[k])
                {
                    k++;
                    swap(nums[k], nums[i]);
                }
            }
            return k+1;
        }

    题目:移除数组中所有与给定值相同的数。(leetcode27)

    思路:与26题方法完全一致。

        int removeElement(vector<int>& nums, int val) {
            int k = 0;
            for(int i = 0;i < nums.size();i++)
            {
                if(nums[i] != val)  nums[k++] = nums[i];
            }
            return k;
        }

    题目:将数组中所有零元素移到最后。(leetcode283)

    思路:一个指针指向最后一个非零元素,一个指针遍历,注意赋值完后置为0.

        void moveZeroes(vector<int>& nums) {
            int k = 0;
            for(int i = 0;i < nums.size();i++)
            {
                if(nums[i])
                {
                    nums[k++] = nums[i];
                    if(i >= k) nums[i] = 0;
                }
            }
        }

    题目:移除有序链表中重复的元素(leetcode83)

    思路:跳过即可

    //非递归

        ListNode* deleteDuplicates(ListNode* head) {
            if(head == NULL)  return head;
            ListNode* p = head;
            while(p->next != NULL)
            {
                if(p->val == p->next->val) p->next = p->next->next;
                else p = p->next;
            }
            return head;
        }

    //递归

        ListNode* deleteDuplicates(ListNode* head) {
            if(head == NULL || head->next == NULL)  return head;
            ListNode* p = deleteDuplicates(head->next);
            if(p->val != head->val)
            {
                head->next = p;
                return head;
            }
            else
            {
                return p;
            }

    题目:删除有序数组中重复出现的元素(leetcode82)

    //非递归

    // 递归

        ListNode* deleteDuplicates(ListNode* head) {
            if(head == NULL || head->next == NULL)  return head;
            ListNode* p = head;
            bool flag = false;
            while((p != NULL) && (p->next != NULL) && (p->val == p->next->val))
            {
                flag = true;
                p = p->next;
            }
            if(flag) return deleteDuplicates(p->next);
            else
            {
                p->next = deleteDuplicates(p->next);
                return p;
            }
        }

     参考链接:https://blog.csdn.net/liyinan11/article/details/71036227

  • 相关阅读:
    MFC Windows 程序设计>WinMain 简单Windows程序 命令行编译
    AT3949 [AGC022D] Shopping 题解
    CF643D Bearish Fanpages 题解
    CF643C Levels and Regions 题解
    CF241E Flights 题解
    CF671C Ultimate Weirdness of an Array 题解
    CF1592F Alice and Recoloring 题解
    GYM 102452E 题解
    CF494C Helping People 题解
    P5556 圣剑护符
  • 原文地址:https://www.cnblogs.com/lfri/p/12468616.html
Copyright © 2011-2022 走看看