C# 移动第一个重复字符到前面

news/2024/7/3 10:22:45

例如 abcdbbfg 变成 bbbacdfg,要求时间复杂度为N,空间复杂度为1,写了两个方法都未达到要求 :(

 

View Code
        static void MoveDupCharToFront(char[] input)
        {
            if (input == null|| input.Length <=1)
            {
                throw new Exception("input can't be empty or less than 2 length");
            }
            int dupCount = 0;
            int x = 0;
            char dupChar = FindFirstDupChar(input);
            char[] result = new char[input.Length];
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == dupChar)
                {
                    dupCount++;
                }
            }

            while (dupCount >0)
            {
                result[x++] = dupChar;
                dupCount--;
            }
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i]!=dupChar)
                {
                    result[x++] = input[i];
                }
            }
            input = result;
        }

        static void MoveDupCharToFront2(char[] input)
        {
            if (input == null|| input.Length <=1)
            {
                throw new Exception("input can't be empty or less than 2 length");
            }
            char dupChr = FindFirstDupChar(input);
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i]== dupChr)
                {
                    for (int j = i; j >0; j--)
                    {
                        input[j] = input[j - 1];
                    }
                    input[0] = dupChr;
                }
            }
        }

        static char FindFirstDupChar(char[] input)
        {
            if (input == null || input.Length ==0)
            {
                throw new Exception("input can't be empty .");
            }
            Hashtable ht = new Hashtable();
            for (int i = 0; i < input.Length; i++)
            {
                if (ht[input[i]] == null)
                {
                    ht.Add(input[i], 1);
                }
                else
                {
                    return input[i];
                }
            }
            throw new Exception("No dup char.");
        }

 

 

转载于:https://www.cnblogs.com/Ligeance/archive/2012/11/15/2770891.html


http://www.niftyadmin.cn/n/3628392.html

相关文章

XenDesktop多用户不同时间使用同一个发布的物理机桌面

XenDesktop多用户不同时间使用同一个发布的物理机桌面 Citrix XenDesktop 5.5发布一台物理机的OS桌面时&#xff08;非XenServer、VMware-ESX以及Hyper-V等虚拟系统&#xff09;&#xff0c;默认情况下&#xff0c;就算在创建桌面时将这个桌面赋予多个用户&#xff0c;当其中一…

Python迭代器和生成器(改编自知乎相关文章)

Python迭代器和生成器&#xff08;改编自知乎相关文章&#xff09; 1.迭代器 有一些Python对象&#xff0c;我们可以从中按一定次序提取出其中的元素。这些对象称之为可迭代对象。比如&#xff0c;字符串、列表、元组都是可迭代对象。 我们回忆一下从可迭代对象中提取元素的过程…

第15周阅读程序(3)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 阅读程序 *输入描述 :无 *程序输出 : */#include<algorithm> #include<functional> #include<vector&g…

第15周阅读程序(4)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 阅读程序 *输入描述 :无 *程序输出 : */#include<algorithm> #include<functional> #include<iostream…

lightoj 1140 - How Many Zeroes?(数位DP)

1140 - How Many Zeroes? PDF (English)StatisticsForum Time Limit: 2 second(s)Memory Limit: 32 MB Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down? Input Inp…

JS对象继承(6种)

原型链(父类的实例)借用构造函数(调用超类构造函数)组合继承(原型链和借用构造函数)原型式继承(借助原型基于已有的对象上创建新对象)寄生式继承(创建一个仅用于封装继承过程的函数)寄生组合式继承(解决父类属性重写)ECMAScript无法实现接口继承&#xff0c;只支持实现继承&…

第15周阅读程序(5)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 阅读程序 *输入描述 :无 *程序输出 : */#include<iterator> #include<algorithm> #include<functional…

Java的String字符串内容总结

String--字符串 获取字符串的长度 使用Sring类的length()方法可获取字符串对象的长度&#xff0c;例&#xff1a; str.length(); str代表指定的字符串对象;返回值为返回指定字符串的长度。例&#xff1a; 获取字符串中指定字符的索引位置 String类提供了indexOf()和lastIndexOf…