Skip to content

Commit

Permalink
Merge pull request wangzheng0822#3 from wangzheng0822/master
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
leexingliang authored Mar 1, 2019
2 parents 8653ba1 + 0c71e0e commit bc649e3
Show file tree
Hide file tree
Showing 49 changed files with 1,610 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*.tar.gz
*.rar
*.DS_Store
*.exe

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
Expand Down
76 changes: 72 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
# 数据结构和算法之美
# [https://time.geekbang.org/column/intro/126](https://time.geekbang.org/column/intro/126)
# 数据结构和算法必知必会的50个代码实现

# Java rate limiting library/framework
# https://github.com/wangzheng0822/ratelimiter4j
## 数组
* 实现一个支持动态扩容的数组
* 实现一个大小固定的有序数组,支持动态增删改操作
* 实现两个有序数组合并为一个有序数组

## 链表
* 实现单链表、循环链表、双向链表,支持增删操作
* 实现单链表反转
* 实现两个有序的链表合并为一个有序链表
* 实现求链表的中间结点

##
* 用数组实现一个顺序栈
* 用链表实现一个链式栈
* 编程模拟实现一个浏览器的前进、后退功能

## 队列
* 用数组实现一个顺序队列
* 用链表实现一个链式队列
* 实现一个循环队列

## 递归
* 编程实现斐波那契数列求值f(n)=f(n-1)+f(n-2)
* 编程实现求阶乘n!
* 编程实现一组数据集合的全排列

## 排序
* 实现归并排序、快速排序、插入排序、冒泡排序、选择排序
* 编程实现O(n)时间复杂度内找到一组数据的第K大元素

## 二分查找
* 实现一个有序数组的二分查找算法
* 实现模糊二分查找算法(比如大于等于给定值的第一个元素)

## 散列表
* 实现一个基于链表法解决冲突问题的散列表
* 实现一个LRU缓存淘汰算法

## 字符串
* 实现一个字符集,只包含a~z这26个英文字母的Trie树
* 实现朴素的字符串匹配算法

## 二叉树
* 实现一个二叉查找树,并且支持插入、删除、查找操作
* 实现查找二叉查找树中某个节点的后继、前驱节点
* 实现二叉树前、中、后序以及按层遍历

##
* 实现一个小顶堆、大顶堆、优先级队列
* 实现堆排序
* 利用优先级队列合并K个有序数组
* 求一组动态数据集合的最大Top K

##
* 实现有向图、无向图、有权图、无权图的邻接矩阵和邻接表表示方法
* 实现图的深度优先搜索、广度优先搜索
* 实现Dijkstra算法、A*算法
* 实现拓扑排序的Kahn算法、DFS算法

## 回溯
* 利用回溯算法求解八皇后问题
* 利用回溯算法求解0-1背包问题

## 分治
* 利用分治算法求一组数据的逆序对个数

## 动态规划
* 0-1背包问题
* 最小路径和
* 编程实现莱文斯坦最短编辑距离
* 编程实现查找两个字符串的最长公共子序列
* 编程实现一个数据序列的最长递增子序列
2 changes: 1 addition & 1 deletion csharp/05-array/Array.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace _05_array
namespace algo05_array
{
public sealed class Array<T> where T : IComparable<T>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
5 changes: 2 additions & 3 deletions csharp/06-linkedlist/LRUWithArray.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text.RegularExpressions;
using _05_array;
using algo05_array;

namespace _06_linked_list
namespace algo06_linked_list
{
/// <summary>
/// 使用数组实现LRU缓存淘汰算法
Expand Down
2 changes: 1 addition & 1 deletion csharp/06-linkedlist/LRUWithLinkedList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace _06_linked_list
namespace algo06_linked_list
{
/// <summary>
/// 使用单链表实现LRU缓存淘汰算法
Expand Down
2 changes: 1 addition & 1 deletion csharp/06-linkedlist/SingleLinkedList.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace _06_linked_list
namespace algo06_linked_list
{
/// <summary>
/// 单链表的插入、删除、清空、查找
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\05-array\_05_array.csproj" />
<ProjectReference Include="..\05-array\algo05_array.csproj" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions csharp/07-linkedlist/_07_linkedlist/SingleLinkedListAlgo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using _06_linked_list;
using algo06_linked_list;

namespace _07_linkedlist
namespace algo07_linkedlist
{
/// <summary>
/// 单链表常用算法操作
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\06-linkedlist\_06_linked_list.csproj" />
<ProjectReference Include="..\..\06-linkedlist\algo06_linked_list.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions csharp/08-stack/algo08_stack/ArrayStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;

namespace algo08_stack
{
public class ArrayStack<T>
{
private readonly int _capacity;

private readonly T[] _data;

private int _top = -1; // 指向栈顶元素,当为-1时表示栈为空

public ArrayStack(int capacity)
{
_capacity = capacity;

_data = new T[capacity];
}

public int Count => _top + 1;

public void Push(T val)
{
if (Count == _capacity) throw new InvalidOperationException("Stack full.");

_top++;

_data[_top] = val;
}

public T Pop()
{
if (_top == -1) throw new InvalidOperationException("Stack empty.");

T val = _data[_top];
_top--;

return val;
}
}
}
51 changes: 51 additions & 0 deletions csharp/08-stack/algo08_stack/LinkedStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;

namespace algo08_stack
{
public class LinkedStack<T>
{
private StackListNode<T> _top;

public int Count { get; private set; }

public void Push(T val)
{
var newNode = new StackListNode<T>(val);
newNode.Next = _top;
_top = newNode;

Count++;
}

public T Pop()
{
if (_top == null) throw new InvalidOperationException("Stack empty");

T val = _top.Value;
_top = _top.Next;

Count--;

return val;
}

public void Clear()
{
while (Count > 0)
{
Pop();
}
}
}

public class StackListNode<T>
{
public StackListNode(T nodeValue)
{
Value = nodeValue;
}

public T Value { get; set; }
public StackListNode<T> Next { get; set; }
}
}
39 changes: 39 additions & 0 deletions csharp/08-stack/algo08_stack/LinkedStackBrowser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace algo08_stack
{
/// <summary>
/// 利用链栈实现浏览器怎么进后退
/// </summary>
public class LinkedStackBrowser
{
private readonly LinkedStack<string> _backStack = new LinkedStack<string>();
private readonly LinkedStack<string> _forwardStack = new LinkedStack<string>();

public void Open(string url)
{
_backStack.Push(url);

_forwardStack.Clear();
}

public string Backward()
{
if (_backStack.Count == 0) return string.Empty;

string url = _backStack.Pop();

_forwardStack.Push(url);

return url;
}

public string Forward()
{
if (_forwardStack.Count == 0) return string.Empty;

string url = _forwardStack.Pop();
_backStack.Push(url);

return url;
}
}
}
7 changes: 7 additions & 0 deletions csharp/08-stack/algo08_stack/algo08_stack.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion csharp/Tests/_05_array_tests/Array.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using algo05_array;
using Xunit;
using Xunit.Abstractions;
using _05_array;

namespace _05_array_tests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\05-array\_05_array.csproj" />
<ProjectReference Include="..\..\05-array\algo05_array.csproj" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions csharp/Tests/_06_linkedlist_tests/BaseLinkedListTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using _06_linked_list;
using algo06_linked_list;

namespace _06_linkedlist_tests
namespace algo06_linkedlist_tests
{
public class BaseLinkedListTests
{
Expand Down
6 changes: 3 additions & 3 deletions csharp/Tests/_06_linkedlist_tests/LRUWithArray.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using algo05_array;
using algo06_linked_list;
using Xunit;
using Xunit.Abstractions;
using _05_array;
using _06_linked_list;

namespace _06_linkedlist_tests
namespace algo06_linkedlist_tests
{
public class LRUWithArrayTests
{
Expand Down
4 changes: 2 additions & 2 deletions csharp/Tests/_06_linkedlist_tests/LRUWithLinkedList.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Xunit;
using Xunit.Abstractions;
using _06_linked_list;
using algo06_linked_list;

namespace _06_linkedlist_tests
namespace algo06_linkedlist_tests
{
public class LRUWithLinkedListTests : BaseLinkedListTests
{
Expand Down
4 changes: 2 additions & 2 deletions csharp/Tests/_06_linkedlist_tests/SingleLinkedList.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Xunit;
using Xunit.Abstractions;
using _06_linked_list;
using algo06_linked_list;

namespace _06_linkedlist_tests
namespace algo06_linkedlist_tests
{
public class SingleLinkedListTests : BaseLinkedListTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>_06_linkedlist_tests</RootNamespace>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -14,8 +13,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\05-array\_05_array.csproj" />
<ProjectReference Include="..\..\06-linkedlist\_06_linked_list.csproj" />
<ProjectReference Include="..\..\05-array\algo05_array.csproj" />
<ProjectReference Include="..\..\06-linkedlist\algo06_linked_list.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Xunit;
using _06_linkedlist_tests;
using _06_linked_list;
using _07_linkedlist;
using algo06_linkedlist_tests;
using algo06_linked_list;
using algo07_linkedlist;

namespace _07_linkedlist_tests
namespace algo07_linkedlist_tests
{
public class SingleLinkedListAlgoTests : BaseLinkedListTests
{
Expand Down
Loading

0 comments on commit bc649e3

Please sign in to comment.