当前位置: 首页 > news >正文

(一)算法


文章目录

  • 项目地址
  • 一、题目
    • 1.1 基础题
      • 1. 反转字符串
      • 2. 判断回文字符串
      • 3. 两个数组的交集
      • 4. 字符串中第一个不重复的字符
      • 5. LRU 缓存机制 (经典 OOD 题)
      • 6. 写一个线程安全的 有界队列
      • 7. 实现一个 LINQ 的 Where 扩展方法
      • 8. 并发请求控制
    • 9. 设计 TinyUrl 系统


项目地址

  • 教程作者:
  • 教程地址:
  • 代码仓库地址:
  • 所用到的框架和插件:
dbt 
airflow

一、题目

1.1 基础题

1. 反转字符串

输入 “hello”,输出 “olleh”。
要求:不能直接用 Array.Reverse。

string ReverseString(string input)
{if (string.IsNullOrEmpty(input)) return input;char[] arr = input.ToCharArray();int left = 0, right = arr.Length - 1;while (left < right){(arr[left], arr[right]) = (arr[right], arr[left]);left++;right--;}return new string(arr);
}

2. 判断回文字符串

输入 “abba” 返回 true,输入 “abc” 返回 false。

bool IsPalindrome(string input)
{if (string.IsNullOrEmpty(input)) return true;int left = 0, right = input.Length - 1;while (left < right){while (left < right && !char.IsLetterOrDigit(input[left])) left++;while (left < right && !char.IsLetterOrDigit(input[right])) right--;if (char.ToLower(input[left]) != char.ToLower(input[right]))return false;left++;right--;}return true;
}

3. 两个数组的交集

输入 nums1 = [1,2,2,1],nums2 = [2,2] → 输出 [2,2]。

int[] Intersect(int[] nums1, int[] nums2)
{var dict = new Dictionary<int, int>();foreach (var n in nums1){if (!dict.ContainsKey(n)) dict[n] = 0;dict[n]++;}var result = new List<int>();foreach (var n in nums2){if (dict.ContainsKey(n) && dict[n] > 0){result.Add(n);dict[n]--;}}return result.ToArray();
}

4. 字符串中第一个不重复的字符

输入 “leetcode” → 输出 l
输入 “aabb” → 输出 null 或特殊符号。

char? FirstUniqueChar(string s)
{var dict = new Dictionary<char, int>();foreach (var ch in s){if (!dict.ContainsKey(ch)) dict[ch] = 0;dict[ch]++;}foreach (var ch in s){if (dict[ch] == 1) return ch;}return null;
}

5. LRU 缓存机制 (经典 OOD 题)

要求:
Get O(1)
Put O(1)
超出容量时移除最近最少使用的元素。

public class LRUCache
{private readonly int _capacity;private readonly Dictionary<int, LinkedListNode<(int Key, int Value)>> _cache;private readonly LinkedList<(int Key, int Value)> _list;public LRUCache(int capacity){_capacity = capacity;_cache = new Dictionary<int, LinkedListNode<(int, int)>>();_list = new LinkedList<(int, int)>();}public int Get(int key){if (!_cache.TryGetValue(key, out var node))return -1;_list.Remove(node);_list.AddFirst(node);return node.Value.Value;}public void Put(int key, int value){if (_cache.TryGetValue(key, out var node)){_list.Remove(node);}else if (_cache.Count >= _capacity){var last = _list.Last;if (last != null){_cache.Remove(last.Value.Key);_list.RemoveLast();}}var newNode = new LinkedListNode<(int, int)>((key, value));_list.AddFirst(newNode);_cache[key] = newNode;}
}

6. 写一个线程安全的 有界队列

public class BlockingQueue<T>
{private readonly Queue<T> _queue = new();private readonly int _capacity;private readonly object _lock = new();public BlockingQueue(int capacity) => _capacity = capacity;public void Enqueue(T item){lock (_lock){while (_queue.Count >= _capacity)Monitor.Wait(_lock);_queue.Enqueue(item);Monitor.PulseAll(_lock);}}public T Dequeue(){lock (_lock){while (_queue.Count == 0)Monitor.Wait(_lock);var item = _queue.Dequeue();Monitor.PulseAll(_lock);return item;}}
}

7. 实现一个 LINQ 的 Where 扩展方法

  • 模拟实现 IEnumerable.Where
public static class MyLinqExtensions
{public static IEnumerable<T> MyWhere<T>(this IEnumerable<T> source,Func<T, bool> predicate){foreach (var item in source){if (predicate(item))yield return item;}}
}

8. 并发请求控制

写一个方法,接收一组 Func,限制同时最多执行 N 个任务,直到全部完成。

public async Task RunWithMaxConcurrency(IEnumerable<Func<Task>> tasks, int maxConcurrency)
{using SemaphoreSlim semaphore = new(maxConcurrency);var runningTasks = tasks.Select(async task =>{await semaphore.WaitAsync();try{await task();}finally{semaphore.Release();}});await Task.WhenAll(runningTasks);
}

9. 设计 TinyUrl 系统

输入长链接 “https://example.com/abc/def”
输出短链接 “http://tinyurl.com/xyz123”
要求短链接能还原为长链接。

public class TinyUrlService
{private readonly Dictionary<string, string> _map = new();private readonly string _baseUrl = "http://tinyurl.com/";private readonly Random _random = new();public string Encode(string longUrl){var key = Guid.NewGuid().ToString("N").Substring(0, 6);_map[key] = longUrl;return _baseUrl + key;}public string Decode(string shortUrl){var key = shortUrl.Replace(_baseUrl, "");return _map[key];}
}
http://www.dtcms.com/a/439246.html

相关文章:

  • 23ICPC济南站补题
  • 商务网站建设ppt模板培训网站排名
  • 南阳市宛城区建设局网站设计本质
  • nacos使用指南
  • 中山AI搜索哪家好?GEO优化与传统SEO深度解析
  • MySQL优化----非查询SQL优化
  • 影视网站代理徐州市经济技术开发区建设局网站
  • 基于单片机的三相逆变电源设计
  • Python基础入门例程92-NP92 公式计算器
  • MyCat实现分库分表
  • 宿迁网站建设公司宣传海报制作
  • 中铁中基建设集团网站品牌形象网站源码
  • 中医院网站源码传智黑马培训机构
  • python全栈(基础篇)——day01:环境准备(python解释器安装+pycharm专业版的下载+vscode编辑器+汉化设置)
  • CodeForces Round 1054(div.3)C题
  • 南宁网站建设制作定制seo搜索引擎优化招聘
  • 3.java常用类知识点
  • 费马小定理证明
  • 建设中网站源码微信小程序怎么注册申请
  • iis7.5 没有默认网站彩票网站代理
  • 汇编语言Debug工具与常用指令完全指南
  • wordpress 托管建站有了公网ip如何做网站
  • 网站的费用多少合肥网站开发建设
  • 零基础学习做网站蚌埠做网站建设费用
  • 麒麟系统安装后添加自启动
  • 网站底部备案号悬挂佛山木工机械厂骏域网站建设专家
  • 顺序表专题
  • 网站 备案 中国 名字微网站模板代码
  • ASP.NET Core Web API 中控制器操作的返回类型及Swagger
  • AI模型测评平台工程化实战十二讲(第六讲:大模型测评系统:智能模型管理模块的设计与实现)