《剑指Offer》面试题的C#解法

本文详细介绍了《剑指Offer》中的一些经典面试题的C#解法,包括单例模式、查找数组中重复数字、二维数组搜索、字符串替换、链表逆序打印、二叉树遍历等,还探讨了动态规划在解决某些问题中的应用,如剪绳子求最大乘积。同时,提出用两个栈实现队列和用两个队列实现栈的思路。
摘要由CSDN通过智能技术生成

面试题2:单例模式

    class SingleTon
	{
		public SingleTon Instance()
		{
			return Nested.instance;
		}
		
		private class  Nested
		{
			internal static readonly SingleTon instance = new SingleTon();
		}
	}

要点:静态变量/函数只会在程序运行时执行一次。


面试题3:找出数组中重复的数字

        public void Problem3()
        {
            HashSet<int> result = new HashSet<int>();
            int[] input = {2,6,5,7,1,2,3,4,8};
            for (int i = 0; i < input.Length; i++)
			{
                int value = input[i];
                while (input[i] != i)
                {
                    if (input[value] != value)
                    {
                        int temp = input[i];
                        input[i] = input[value];
                        input[value] = temp;
                        value = input[i];
                    }
                    else
                    {
                        result.Add(value);
                        break;
                    }
                }
            }
            foreach (int value in result)
            {
                Console.WriteLine(value);
            }
        }

面试题3-2:在不改动原数组的前提下,找出重复的数字

        public void Problem3_2()
		{
			int? result = null;
			int num = 0;
			int[] array = { 2, 3, 5, 4, 3, 2, 6, 7 };
			int start = 0, end = array.Length - 1;
			while (end >= start)
			{
				int mid = (end - start)>>1 + start;
				int count = GetCount(array, start, mid);
				if (end == start)
				{
					if (count > 1)
					{
						result = array[start];
						num = count;
					}
					break;
				}
				if (count > (mid - start + 1))
				{
					end = mid;
				}
				else
				{
					start = mid + 1;
				}
			}
			if (result == null)
			{
				Console.WriteLine("没有重复的数字");
			}
			else
			{
				Console.WriteLine(result + "重复了{0}次", num);
			}
		}

		private int GetCount(int[] array, int start, int end)
		{
			int count = 0;
			for (int i = 0; i < array.Length; i++)
			{
				for (int j = start; j <= end; j++)
				{
					if (array[i] == array[j])
					{
						count++;
					}
				}
			}
			return count;
		}

面试题4:在二维数组中查找数字

        public bool Problem4(int target)
		{
			int[,] input = {
				{ 1,2,3},
				{ 2,4,6},
				{ 3,6,9},
			};
			bool result = false;
			int col = input.GetUpperBound(0) + 1;
			int row = input.GetLength(0);
			//从右上开始遍历
			int c = col - 1;//列
			int r = 0;//行
			while (c < 0 || r > row - 1)
			{
				int value = input[c, r];
				if (value == target)
				{
					result = true;
					break;
				}
				else if (value > target)
				{
					c--;
				}
				else
				{
					r++;
				}
			}
			Console.WriteLine(result);
			return result;
		}

要点:从右上角或者左下角开始比较,才可以在每次比较中缩小查询的区域。


面试题5:将字符串中的空格提替换成指定字符

        public string Problem5(string input, string target)
		{
			if (input.Length == 0)
			{
		
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值