美文网首页
腾讯面试题(除掉N个整数中重复数)解题(线性时间,原地置换排序算

腾讯面试题(除掉N个整数中重复数)解题(线性时间,原地置换排序算

作者: 最穷码农 | 来源:发表于2019-04-05 17:49 被阅读0次

将写内容过程中比较好的一些内容片段备份一次,下边内容内容是关于腾讯面试题(除掉N个整数中重复数)解题(线性时间,原地置换排序算法)(C#)的内容,希望能对码农们也有好处。

        private void BitSortAndDelRepeatorsA(int[] A)

        {

            int theN = A.Length;

            for (int i = 31; i >= 1; i--)

            {

                int thePrvCB = A[0] >> (i)  ;

                int theS = 0;

                int theI = theS;

                int theAxNum = A[theI];

                int theBase = 1 << (i-1);

                for (int j = 1; j < theN; j++)

                {

                    int theTmpPrvCB = A[j] >> (i);

                    if (theTmpPrvCB != thePrvCB)

                    {

                        A[theS] = A[theI];

                        A[theI] = theAxNum;

                        theS = j;

                        theI = theS;

                        theAxNum = A[theI];

                        theAxBit = A[theI] & theBase;

                        thePrvCB = theTmpPrvCB;

                        continue;

                    }

                    if (theAJ <= theAxBit)

                    {

                        theI++;

                        int theTmp = A[j];

                        A[j] = A[theI];

                        A[theI] = theTmp;

                    }

                }

                A[theS] = A[theI];

                A[theI] = theAxNum;

            }

        }

除掉重复数:只要对上述排序结果进行一次遍历处理即可.

private int[] DeleteRepeatedInt(int[] A)

        {

            int N = A.Length;

            for (int i = 1; i <= 32; i++)

            {

                CountSort2(A, i);

            }

            int thePreNum = int.MinValue;

            List<int> theRet = new List<int>();

            for (int i = 0; i < N; i++)

            {

                if (A[i] != thePreNum)

                {

                    theRet.Add(A[i]);

                    thePreNum = A[i];

                }

            }

            return theRet.ToArray();

        }

===================================================排序算法修正部分

        private void BitSortAndDelRepeatorsA(int[] A)

        {

            int theN = A.Length;

            for (int i = 31; i >= 1; i--)

            {

                int thePrvCB = A[0] >> (i)  ;

                int theS = 0;

                int theI = theS-1;

                int theBase = 1 << (i-1);

                int theAxBit = 0;

                for (int j = 0; j < theN; j++)

                {

                    int theTmpPrvCB = A[j] >> (i);

                    if (theTmpPrvCB != thePrvCB)

                    {

                        theS = j;

                        theI = theS - 1;

                        theAxBit = 0;

                        thePrvCB = theTmpPrvCB;

                        continue;

                    }

                    if (theI < theS)

                    {

                        if (theAJ == 0)

                        {

                            continue;

                        }

                        continue;

                    }

                    if (theAJ <= theAxBit)

                    {

                        int theTmp = A[j];

                        A[j] = A[theI];

                        A[theI] = theTmp;

                        theI++;

                    }

                }

            }

        }

相关文章

  • 腾讯面试题(除掉N个整数中重复数)解题(线性时间,原地置换排序算

    将写内容过程中比较好的一些内容片段备份一次,下边内容内容是关于腾讯面试题(除掉N个整数中重复数)解题(线性时间,原...

  • 2018-07-03

    线性时间选择 给定线性序集中n个元素和一个整数k,1<=k<=n,要求找出这n个元素中第k小的元素,即如果将这n个...

  • 128. 最长连续序列

    解题思路 排序+去重 那么这个题目的时间复杂度是O(N^2) 首先去重, 这里使用unordered_set<> ...

  • 冒泡/插入/归并/快速 排序

    冒泡排序 平均时间复杂度 O(n2)稳定排序原地排序 插入排序 平均时间复杂度 O(n2)稳定排序原地排序 冒泡排...

  • 线性排序

    时间复杂度为线性( O(n) )的排序方式叫做线性排序。常见的线性排序有桶排序、计数排序、基数排序。 桶排序 (1...

  • LeetCode 172. 阶乘后的零

    题目 给定一个整数 n,返回 n! 结果尾数中零的数量。 解题思路

  • 数位DP题目:LeetCode1015. 至少有 1 位重复的数

    题目链接 给定正整数 N,返回小于等于 N 且具有至少 1 位重复数字的正整数。 解答:这是一道数位DP模板题,算...

  • 线性时间选择

    一、概念 给定线性序集中n个元素和一个整数k,,要求找出这n个元素中第k小的元素。 二、特殊情况(堆排序) 当或时...

  • 线性排序

    一、线性排序算法介绍 线性排序算法包括桶排序、计数排序、基数排序。 线性排序算法的时间复杂度为O(n)。 此3种排...

  • 桶排序、计数排序、基数排序

    一、线性排序算法介绍 线性排序算法包括桶排序、计数排序、基数排序。 线性排序算法的时间复杂度为O(n)。 此3种排...

网友评论

      本文标题:腾讯面试题(除掉N个整数中重复数)解题(线性时间,原地置换排序算

      本文链接:https://www.haomeiwen.com/subject/uyrkiqtx.html