• 数据结构与算法学习系列第一天


    线性表系列

    题目1:两个数组A,B 实现A与B的交集

    算法思路:以A中的元素为基本,遍历B中的所有元素,判断:如果B中的元素在A中不存在则插入A中

    图解:

     C#实现代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _1线性表之两个集合的并集
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strA = "abcd";
                string strB = "cdef";
                int lengthA = strA.Length;
                int lengthB = strB.Length;
                char[] charB= strB.ToCharArray();
                try
                {
                    for (int i = 0; i < lengthB; i++)
                    {
                        bool b = strA.Contains(charB[i]);
                        if (!b)
                        {
                            strA += charB[i].ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine(strA);
                Console.ReadKey();
            }
        }
    }
    

     题目2:两个数组A,B,实现A与B的交集

    算法思路:以A为基础,遍历B中的所有元素,判断:如果B中的元素存在于A中则将B中存在的元素存到另一个数组中

    C#实现代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _2线性表之两个集合的并集
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strA = "abcd";
                string strB = "cdef";
                StringBuilder strTemp=new StringBuilder();
                int lengthB = strB.Length;
                char[] charB = strB.ToCharArray();
                try
                {
                    for (int i = 0; i < lengthB; i++)
                    {
                        bool b = strA.Contains(charB[i]);
                        //如果为真的话说明strB中存在strA中拥有的元素
                        if (b)
                        {
                            strTemp.Append(charB[i].ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine(strTemp.ToString());
                Console.ReadKey();
            }
        }
    }

    好啦今天就学到这里回寝室睡觉去了。大伙明天见1

  • 相关阅读:
    冒泡排序
    CFURLCreateStringByAddingPercentEscapes
    AESCrypt加密与解密
    关于Xcode 的SDK与系统版本理解
    nginx 安全稳定版本
    bcom 遇到的那些问题
    nginx 配置404错误页面
    AES 对称加密解密
    SpringCloud stream连接RabbitMQ收发信息
    springboot1.5 和 2.0 引入 redis 并封装工具类
  • 原文地址:https://www.cnblogs.com/luodao1991/p/2998771.html
Copyright © 2020-2023  润新知