字符算法 包括字符旋转 包含等问题

using System;
using System.Collections.Generic;

using System.Text;

using System.Collections;
namespace ConsoleApplication2
{
    class StringSuan
    {
        //字符串是否包含问题 A中的字符是否在B中出现
        public static void Contains()
        {
            string str1 = "ggwahrah";
            string str2 = "gwha";

            // 开辟一个辅助数组并清零 
            int[] hash = new int[100];

            // num为辅助数组中元素个数 
            int num = 0;

            // 扫描短字符串 
            for (int j = 0; j < str2.Length; j++)
            {
                // 将字符转换成对应辅助数组中的索引 
                int index = str2[j] - 'A';

                // 如果辅助数组中该索引对应元素为0,则置1,且num++; 
                if (hash[index] == 0)
                {
                    hash[index] = 1;
                    num++;
                }
            }

            // 扫描长字符串 
            for (int k = 0; k < str1.Length; k++)
            {
                int index = str1[k] - 'A';

                // 如果辅助数组中该索引对应元素为1,则num--;为零的话,不作处理(不写语句)。 
                if (hash[index] == 1)
                {
                    hash[index] = 0;
                    num--;
                    if (num == 0)    //m==0,即退出循环。 
                        break;
                }
            }

            // num为0说明长字符串包含短字符串内所有字符 
            if (num == 0)
                Console.WriteLine("TRUE");
            else
                Console.WriteLine("FALSE");

        }

       
        // 字符串左旋转
        public static void Reverse(StringBuilder sb, int index, int n)
        {

            for (int i = 0; i < n / 2; i++)
            {
                char temp = sb[index + i];
                sb[index + i] = sb[index + n - i - 1];
                sb[index + n - i - 1] = temp;
            }

        }
        public static void LeftRotate(StringBuilder sb, int index)
        {
            if (index > sb.Length)
            {
                Console.WriteLine("please insert smaller number");
                return;
            }
            Reverse(sb, 0, index);
            Reverse(sb, index, sb.Length - index);
            Reverse(sb, 0, sb.Length);
            Console.WriteLine(sb);
        }
        /

        //获得字符串中最大的连续数字get the longest number from a string
        public static void LongNumber(string n)
        {
            char[] str = n.ToCharArray();
            int max = 0, j = 0;
            int start = 0;
            bool set = false;
            int length = str.Length;
            int output = 0;
            for (int i = 0; i < length; i++)
            {
                if (char.IsNumber(str[i]))
                {
                    if (set == false)
                        start = i;
                    j++;
                    set = true;
                }

                else
                {
                    if (j > max)
                    {
                        output = start;
                        max = j;
                    }
                    set = false;
                    j = 0;
                }
            }
            if (j > max)
            {
                output = start;
                max = j;
            }


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

                Console.Write(str[output + i]);
        }

        // 字符反转
        static string Reverse1(string original)
        {
            char[] arr = original.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);

        }
        static void ver(char[] str)
        {
            char temp;
            int len;
            len = str.Length;
            for (int i = 0; i < len / 2; i++)
            {
                temp = str[i];
                str[i] = str[len - i - 1];
                str[len - i - 1] = temp;

            }
            Console.WriteLine(str);
        }

        //字符串中第一个只出现一次的字符“abcdabc”  output:d
        public static char? Print(string str)
        {
            if (str.Length == 0 || str == null)
                return null;

            bool[] marks = new bool[str.Length];

            for (int i = 0; i < str.Length; i++)
            {
                if (marks[i] != true)
                {
                    marks[i] = true;

                    int count = 0;

                    for (int j = i; j < str.Length; j++)
                    {
                        if (str[j] == str[i])
                        {
                            count++;
                            marks[j] = true;
                        }
                    }

                    if (count == 1)
                    {
                        return str[i];
                    }
                }
            }

            return null;
        } 
    }
}

转载于:https://www.cnblogs.com/dasydong/p/3280948.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值