Skip to content

Commit

Permalink
feat: add solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzcdev committed Jan 25, 2023
1 parent 85a6619 commit f5343de
Show file tree
Hide file tree
Showing 744 changed files with 23,963 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
*.exe
*.out
*.app

.DS_Store
tmp/
.vscode/
35 changes: 35 additions & 0 deletions Data_Structures_and_Algorithms_(English)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

## 函数题 (16/17)
- [6-1. Deque](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-1%20Deque.c)
- [6-2. Two Stacks In One Array](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-2%20Two%20Stacks%20In%20One%20Array.c)
- [6-3. Add Two Polynomials](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-3%20Add%20Two%20Polynomials.c)
- [6-4. Reverse Linked List](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-4%20Reverse%20Linked%20List.c)
- [6-5. Evaluate Postfix Expression](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-5%20Evaluate%20Postfix%20Expression.c)
- [6-6. Level-order Traversal](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-6%20Level-order%20Traversal.c)
- [6-7. Isomorphic](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-7%20Isomorphic.c)
- [6-8. Percolate Up and Down](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-8%20Percolate%20Up%20and%20Down.c)
- [6-9. Sort Three Distinct Keys](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-9%20Sort%20Three%20Distinct%20Keys.c)
- [6-11. Shortest Path [1]](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-11%20Shortest%20Path%20%5B1%5D.c)
- [6-12. Shortest Path [2]](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-12%20Shortest%20Path%20%5B2%5D.c)
- [6-13. Topological Sort](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-13%20Topological%20Sort.c)
- [6-14. Count Connected Components](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-14%20Count%20Connected%20Components.c)
- [6-15. Iterative Mergesort](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-15%20Iterative%20Mergesort.c)
- [6-16. Shortest Path [3]](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-16%20Shortest%20Path%20%5B3%5D.c)
- [6-17. Shortest Path [4]](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E5%87%BD%E6%95%B0%E9%A2%98/6-17%20Shortest%20Path%20%5B4%5D.c)

## 编程题 (15/33)
- [7-1. Maximum Subsequence Sum](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-1%20Maximum%20Subsequence%20Sum.cpp)
- [7-2. Reversing Linked List](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-2%20Reversing%20Linked%20List.cpp)
- [7-3. Pop Sequence](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-3%20Pop%20Sequence.cpp)
- [7-4. List Leaves](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-4%20List%20Leaves.cpp)
- [7-5. Tree Traversals Again](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-5%20Tree%20Traversals%20Again.cpp)
- [7-6. Root of AVL Tree](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-6%20Root%20of%20AVL%20Tree.cpp)
- [7-7. Complete Binary Search Tree](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-7%20Complete%20Binary%20Search%20Tree.cpp)
- [7-8. File Transfer](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-8%20File%20Transfer.cpp)
- [7-12. How Long Does It Take](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-12%20How%20Long%20Does%20It%20Take.cpp)
- [7-13. Insert or Merge](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-13%20Insert%20or%20Merge.cpp)
- [7-14. Insertion or Heap Sort](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-14%20Insertion%20or%20Heap%20Sort.cpp)
- [7-16. Sort with Swap(0, i)](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-16%20Sort%20with%20Swap%280%2C%20i%29.cpp)
- [7-20. Binary Search Tree](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-20%20Binary%20Search%20Tree.cpp)
- [7-21. Counting Leaves](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-21%20Counting%20Leaves.cpp)
- [7-27. QQ Account Management](https://github.com/jinzcdev/PTA/blob/main/Data_Structures_and_Algorithms_%28English%29/%E7%BC%96%E7%A8%8B%E9%A2%98/7-27%20QQ%20Account%20Management.cpp)
59 changes: 59 additions & 0 deletions Data_Structures_and_Algorithms_(English)/函数题/6-1 Deque.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// https://pintia.cn/problem-sets/16/problems/696
Deque CreateDeque(){
PtrToNode header = (PtrToNode)malloc(sizeof(struct Node));
header -> Next = header -> Last = NULL;
Deque deque = (Deque)malloc(sizeof(struct DequeRecord));
deque -> Front = deque -> Rear = header;
return deque;
}

int Push( ElementType X, Deque D ){
PtrToNode newNode = (PtrToNode)malloc(sizeof(struct Node));
newNode -> Element = X;
if(D -> Front == D -> Rear){
D -> Front -> Next = newNode;
newNode -> Last = D -> Front;
newNode -> Next = NULL;
D -> Rear = newNode;
}else{
newNode -> Next = D -> Front -> Next;
D -> Front -> Next -> Last = newNode;
newNode -> Last = D -> Front;
D -> Front -> Next = newNode;
}
return 1;
}

ElementType Pop( Deque D ){
if(D -> Front == D -> Rear) return ERROR;
PtrToNode first = D -> Front -> Next;
int v;
if(D -> Front -> Next == D -> Rear){
D -> Front -> Next = NULL;
D -> Rear = D -> Front;
}else{
D -> Front -> Next = first -> Next;
first -> Next -> Last = D -> Front;
}
v = first -> Element;
free(first);
return v;
}

int Inject( ElementType X, Deque D ){
PtrToNode newNode = (PtrToNode)malloc(sizeof(struct Node));
newNode -> Element = X;
D -> Rear -> Next = newNode;
newNode -> Last = D -> Rear;
newNode -> Next = NULL;
D -> Rear = newNode;
return 1;
}

ElementType Eject( Deque D ){
if(D -> Front == D -> Rear) return ERROR;
PtrToNode end = D -> Rear;
D -> Rear = end -> Last;
D -> Rear -> Next = NULL;
return end -> Element;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://pintia.cn/problem-sets/16/problems/706
void ShortestDist(LGraph Graph, int dist[], Vertex S) {
int INF = 0x7FFFFFFF;
for (int i = 0; i < Graph->Nv; i++) dist[i] = INF;
bool vis[MaxVertexNum] = {false};
dist[S] = 0;
while (S != -1) {
vis[S] = true;
PtrToAdjVNode p = Graph->G[S].FirstEdge;
while (p != NULL) {
if (vis[p->AdjV] == false && dist[p->AdjV] > dist[S] + 1) {
dist[p->AdjV] = dist[S] + 1;
}
p = p->Next;
}
S = -1;
int MIN = INF;
for (Vertex v = 0; v < Graph->Nv; v++) {
if (vis[v] == false && dist[v] < MIN) {
S = v;
MIN = dist[v];
}
}
}
for (Vertex v = 0; v < Graph->Nv; v++)
if (dist[v] == 0x7FFFFFFF) dist[v] = -1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://pintia.cn/problem-sets/16/problems/707
void ShortestDist(MGraph Graph, int dist[], Vertex S) {
for (int i = 0; i < Graph->Nv; i++) dist[i] = INFINITY;
dist[S] = 0;
bool vis[MaxVertexNum] = {false};
while (1) {
int u = -1, MIN = INFINITY;
for (Vertex v = 0; v < Graph->Nv; v++) {
if (vis[v] == false && dist[v] < MIN) {
u = v;
MIN = dist[v];
}
}
if (u == -1) break;
vis[u] = true;
for (Vertex v = 0; v < Graph->Nv; v++) {
if (vis[v] == false && Graph->G[u][v] != INFINITY) {
if (dist[u] + Graph->G[u][v] < dist[v]) {
dist[v] = dist[u] + Graph->G[u][v];
}
}
}
}
for (Vertex v = 0; v < Graph->Nv; v++) {
if (dist[v] == INFINITY) dist[v] = -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://pintia.cn/problem-sets/16/problems/708
bool TopSort(LGraph Graph, Vertex TopOrder[]) {
int front = 0, tail = 0, cnt = 0, indegree[MaxVertexNum] = {0};
for (int i = 0; i < Graph->Nv; i++) {
PtrToAdjVNode p = Graph->G[i].FirstEdge;
while (p != NULL) {
indegree[p->AdjV]++;
p = p->Next;
}
}
for (int i = 0; i < Graph->Nv; i++) {
if (indegree[i] == 0) {
TopOrder[tail++] = i;
}
}
while (front < tail) {
PtrToAdjVNode p = Graph->G[TopOrder[front++]].FirstEdge;
while (p != NULL) {
if (--indegree[p->AdjV] == 0) {
TopOrder[tail++] = p->AdjV;
}
p = p->Next;
}
cnt++;
}
return Graph->Nv == cnt ? true : false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://pintia.cn/problem-sets/16/problems/962
bool vis[MaxVertexNum] = {false};
void dfs(LGraph Graph, int u) {
vis[u] = true;
for (PtrToAdjVNode p = Graph->G[u].FirstEdge; p != NULL; p = p->Next) {
if (vis[p->AdjV] == false) {
dfs(Graph, p->AdjV);
}
}
}
int CountConnectedComponents(LGraph Graph) {
int cnt = 0;
for (int u = 0; u < Graph->Nv; u++) {
if (vis[u] == false) {
dfs(Graph, u);
cnt++;
}
}
return cnt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// https://pintia.cn/problem-sets/16/problems/1046
void swap(int *x, int *y) {
int t = *x;
*x = *y;
*y = t;
}

void sort(ElementType *arr, int start, int end) {
if (start >= end) return;
int mid = arr[end];
int left = start, right = end - 1;
while (left < right) {
while (arr[left] < mid && left < right) left++;
while (arr[right] >= mid && left < right) right--;
swap(&arr[left], &arr[right]);
}
if (arr[left] >= arr[end])
swap(&arr[left], &arr[end]);
else {
left++;
swap(&arr[left], &arr[end]);
}
if (left) sort(arr, start, left - 1);
sort(arr, left + 1, end);
}

void merge_pass(ElementType list[], ElementType sorted[], int N, int length) {
for (int i = 0; i < N; i++) sorted[i] = list[i];
length *= 2;
int i = 0;
for (; i + length < N; i += length) {
sort(sorted, i, i + length - 1);
}
if (i <= N) {
sort(sorted, i, N - 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// https://pintia.cn/problem-sets/16/problems/1085
int known[MaxVertexNum];

void ShortestDist(MGraph Graph, int dist[], int count[], Vertex S) {
for (int i = 0; i < Graph->Nv; i++) dist[i] = INFINITY, count[i] = 0;
dist[S] = 0, count[S] = 1;

while (S != -1) {
known[S] = 1;
for (int i = 0; i < Graph->Nv; i++) {
if (!known[i]) {
if (dist[i] > Graph->G[S][i] + dist[S])
dist[i] = Graph->G[S][i] + dist[S], count[i] = count[S];
else if (dist[i] == Graph->G[S][i] + dist[S])
count[i] += count[S];
}
}

S = -1;
for (int i = 0, v_min = INFINITY; i < Graph->Nv; i++)
if (!known[i] && v_min > dist[i])
v_min = dist[i], S = i;
}

for (int i = 0; i < Graph->Nv; i++) {
if (dist[i] == INFINITY) count[i] = 0, dist[i] = -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// https://pintia.cn/problem-sets/16/problems/1086
void ShortestDist(MGraph Graph, int dist[], int path[], Vertex S) {
for (int i = 0; i < Graph->Nv; i++) dist[i] = INFINITY;
dist[S] = 0;
path[S] = -1;
bool vis[MaxVertexNum] = {false};
while (S != -1) {
vis[S] = true;
for (Vertex v = 0; v < Graph->Nv; v++) {
if (vis[v] == false && Graph->G[S][v] != INFINITY) {
if (dist[S] + Graph->G[S][v] < dist[v]) {
dist[v] = dist[S] + Graph->G[S][v];
path[v] = S;
}
}
}
S = -1;
for (Vertex v = 0, MIN = INFINITY; v < Graph->Nv; v++) {
if (vis[v] == false && dist[v] < MIN) {
S = v;
MIN = dist[v];
}
}
}
for (Vertex v = 0; v < Graph->Nv; v++) {
if (dist[v] == INFINITY) {
dist[v] = path[v] = -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// https://pintia.cn/problem-sets/16/problems/697
Stack CreateStack( int MaxElements )
{
Stack stack =(Stack)malloc(sizeof(struct StackRecord));
stack->Array = (ElementType *)malloc(sizeof(ElementType)*MaxElements);
stack->Capacity=MaxElements;
stack->Top1=-1;
stack->Top2=MaxElements;
return stack;
}
int IsEmpty( Stack S, int Stacknum )
{
if(Stacknum==1)
{
return S->Top1==-1;
}
if(Stacknum==2)
{
return S->Top2==S->Capacity;
}
}
int IsFull( Stack S )
{
return S->Top2-S->Top1==1;
}
int Push( ElementType X, Stack S, int Stacknum )
{
if(IsFull(S)) return 0;
if(Stacknum==1)
{
S->Array[++S->Top1]=X;
}
else
{

S->Array[--S->Top2]=X;
}
return 1;
}
ElementType Top_Pop( Stack S, int Stacknum )
{
if(IsEmpty(S,Stacknum)) return ERROR;
if(Stacknum==1)
{
return S->Array[S->Top1--];
}
if(Stacknum==2)
{
return S->Array[S->Top2++];
}
}
Loading

0 comments on commit f5343de

Please sign in to comment.