冒泡算法、二叉搜索树转双向链表、字符串第一个只出现一次字符C#实现

1.冒泡排序
static void Swap(ref int a,ref int b)
        {
            a = a + b;
            b = a - b;
            a = a - b;
        }
        static void BubbleSort(int[] array)
        {
            if (array == null || array.Length == 0)
                return;
            for(int i=0;i<array.Length-1;i++)//i从0到length-1
            {
                for(int j= i+1;j<array.Length;j++)//j从i+1到length
                {
                    if (array[i] > array[j])
                        Swap(ref array[i], ref array[j]);
                }
            }
        }

2.二叉搜索树转双向链表
 class BinaryTreeNode
    {
        public int Value;
        public BinaryTreeNode LeftNode;
        public BinaryTreeNode RightNode;
        public BinaryTreeNode(int value)
        {
            this.Value = value;
        }

    }
    class BinaryTree
    {
        public BinaryTreeNode rootNode;
        public BinaryTree()
        {
            //rootNode = new BinaryTreeNode(5);
            //rootNode.LeftNode = new BinaryTreeNode(4);
            //rootNode.RightNode = new BinaryTreeNode(6);
        }
        public static BinaryTreeNode ConvertToList(BinaryTreeNode rootNode)
        {
            if (rootNode == null)
                return null;
            BinaryTreeNode lastNodeInList = null;
            Convert(rootNode,ref lastNodeInList);
            BinaryTreeNode headNodeInList = lastNodeInList;
            while (headNodeInList.LeftNode != null)
                headNodeInList = headNodeInList.LeftNode;
            return headNodeInList;

        }
        private static void Convert(BinaryTreeNode CurrNode,ref BinaryTreeNode lastNodeInList)
        {
            if (CurrNode == null)
                return;
            if (CurrNode.LeftNode != null)
                Convert(CurrNode.LeftNode,ref lastNodeInList);
            CurrNode.LeftNode = lastNodeInList;
            if (lastNodeInList != null)
                lastNodeInList.RightNode = CurrNode;
            lastNodeInList = CurrNode;
            if (CurrNode.RightNode != null)
                Convert(CurrNode.RightNode,ref lastNodeInList);
        }
    }

3.第一个只出现一次的字符
public static char FindFirstCharFromStr(string str)
        {
            if (str == null || str == "")
            {
                Console.WriteLine("字符串不能为空");
                return '\0';
            }
            char[] charArray = str.ToCharArray();
            int[] hashTable = new int[256];
            int index = -1;
            for(int i=0;i<hashTable.Length;i++)
            {
                hashTable[i] = 0;
            }
            foreach(char c in charArray)
            {
                hashTable[(int)c] += 1;
            }
            for(index=0;index<hashTable.Length;index++)
            {
                if (hashTable[index] == 1)
                    break;
            }
            if (index >= 256 || index == -1)
            {
                Console.WriteLine("字符串中不存在只出现一次的字符");
                return '\0';
            }
            else
                return (char)index;
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值